ansible--copy与fetch模块

简介: copy模块--传输文件本机将test1文件传输至指定用户组的目录中,src为本机目录,dest为目标机器目录,默认传输文件为644权限,不管传输前的权限如何;[root@10-15-190-167 ~]# ll-rwxr----- 1 root root 9 Nov 21 14:...

copy模块--传输文件
本机将test1文件传输至指定用户组的目录中,src为本机目录,dest为目标机器目录,默认传输文件为644权限,不管传输前的权限如何;
[root@10-15-190-167 ~]# ll
-rwxr----- 1 root root 9 Nov 21 14:39 test1

[root@10-15-190-167 ~]# ansible nginx2 -m copy -a "src=/root/test1 dest=/tmp "
后面也可以跟上mode直接指定文件权限
10.15.100.55 | SUCCESS => {

"changed": false, 
"checksum": "14de3ab563574b5711c56ad55d8b7c0f9edba6e4", 
"dest": "/tmp/test1", 
"gid": 0, 
"group": "root", 
"mode": "0644", 
"owner": "root", 
"path": "/tmp/test1", 
"size": 9, 
"state": "file", 
"uid": 0

}
[root@10-15-100-55 tmp]# ll
-rw-r--r-- 1 root root 9 Nov 21 14:40 test1
file模块---修改文件组权限
ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
创建文件及删除文件
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
ansible webservers -m file -a "dest=/path/to/c state=absent"
[root@10-15-190-167 ~]# ansible nginx2 -m file -a "dest=/tmp/test1 mode=777"

将nginx2组机器的/tmp/test1文件权限改为777

10.15.100.55 | SUCCESS => {

"changed": true, 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"path": "/tmp/test1", 
"size": 9, 
"state": "file", 
"uid": 0

}
创建文件stater的值为:ile,directory,link,hard,touch,absent
注意:指定创建文件时必需存在真正的用户和组否则会出错,默认改到root用户755的权限
[root@10-15-190-167 ~]# ansible test -m file -a "dest=/tmp/test2 mode=744 owner=test group=app state=directory"
10.15.195.231 | FAILED! => {

"changed": false, 
"failed": true, 
"gid": 0, 
"group": "root", 
"mode": "0755", 
"msg": "chown failed: failed to look up user test", 
"owner": "root", 
"path": "/tmp/test2", 
"size": 6, 
"state": "directory", 
"uid": 0

}
10.15.66.32 | SUCCESS => {

"changed": true, 
"gid": 1000, 
"group": "app", 
"mode": "0744", 
"owner": "test", 
"path": "/tmp/test2", 
"size": 6, 
"state": "directory", 
"uid": 1002

}
以下为错误示例
[root@10-15-190-167 ~]# ansible nginx2 -m file -a "dest=/tmp/test2 mode=744 owner=test group=app state=directory"
10.15.100.55 | FAILED! => {

"changed": false, 
"failed": true, 
"gid": 0, 
"group": "root", 
"mode": "0755", 
"msg": "chown failed: failed to look up user test", 
"owner": "root", 
"path": "/tmp/test2", 
"size": 4096, 
"state": "directory", 
"uid": 0

}
删除test2目录,此删除为递归删除下面所有子文件及目录均被删除
[root@10-15-190-167 ~]# ansible test -m file -a "dest=/tmp/test2 state=absent"
10.15.66.32 | SUCCESS => {

"changed": true, 
"path": "/tmp/test2", 
"state": "absent"

}
10.15.195.231 | SUCCESS => {

"changed": true, 
"path": "/tmp/test2", 
"state": "absent"

}
fetch模块--远端复制到本机
[root@10-15-76-70 ~]# ansible test -m fetch -a "src=/root/test.txt dest=/root"
10.15.66.32 | SUCCESS => {

"changed": true, 
"checksum": "83f73ea502e8eeb539b5703ec2873b552949322e", 
"dest": "/root/10.15.66.32/root/test.txt", 
"md5sum": "ecf98f9207fc85a5537558b85e44d87d", 
"remote_checksum": "83f73ea502e8eeb539b5703ec2873b552949322e", 
"remote_md5sum": null

}
10.15.195.231 | SUCCESS => {

"changed": false, 
"file": "/root/test.txt", 
"msg": "the remote file does not exist, not transferring, ignored"

}
自动以目标机器IP地址在本地生成文件夹
[root@10-15-76-70 ~]# ls
10.15.66.32 adduser.sh kernel-4.1.0-8.el7.ucloud
4.1.0-8.el7.ucloud.x86_64.tar.gz itms.tar.gz
[root@10-15-76-70 root]# ls
test.txt
[root@10-15-76-70 root]# pwd
/root/10.15.66.32/root

目录
相关文章
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
394 0
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
279 0
|
4月前
|
缓存 Shell Linux
[ansible]常用内置模块
[ansible]常用内置模块
|
5月前
|
Shell 应用服务中间件 Linux
Ansible的常用模块
Ansible的常用模块
140 6
|
5月前
|
Shell 数据安全/隐私保护
Ansible Ad-hoc,命令执行模块
Ansible Ad-hoc,命令执行模块
59 1
|
5月前
|
运维 Linux 应用服务中间件
Linux之自动化运维工具ansible、ansible模块(2)
Linux之自动化运维工具ansible、ansible模块(2)
|
5月前
|
运维 Linux Shell
Linux之自动化运维工具ansible、ansible模块(1)
Linux之自动化运维工具ansible、ansible模块(1)
|
7月前
|
算法 安全 Linux
Ansible 中的copy 复制模块应用详解
Ansible 中的copy 复制模块应用详解
498 1
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
195 0
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
148 1