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