5 copy 模块
ansible中的copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content则是通过指定信息内容生成目标文件。
[root@promote ~]# ansible-doc -s copy #查看copy模块指令
- name: Copies files to remote locations
copy:
attributes: # Attributes the file or directory should have. To get
supported flags look
at the man page for
`chattr' on the target
system. This string
should contain the
attributes in the same
order as the one
displayed by `lsattr'.
`=' operator is
assumed as default,
otherwise `+' or `-'
operators need to be
included in the
string.
下面我将本地文件/etc/fstab复制到被管理主机上的/opt/fstab.bk,所有者设置为root,权限设置为640
[root@promote ~]# ansible web -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644'
192.168.199.130 | CHANGED => {
"changed": true,
"checksum": "a8b8566b1d9f28b55823c8f61f88d35d81014418",
"dest": "/opt/fstab.bk",
"gid": 0,
"group": "root",
"md5sum": "f25dda38d8c7bb5988c8607bc2a9a17b",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 595,
"src": "/root/.ansible/tmp/ansible-tmp-1540220785.51-128147354820010/source",
"state": "file",
"uid": 0
}
[root@web ~]# ll /opt/fstab.bk
-rw-r--r--. 1 root root 595 10月 22 23:06 /opt/fstab.bk
接着我将"hello"写入“/opt/fstab.bk”
[root@promote ~]# ansible web -m copy -a 'content="hello!" dest=/opt/fstab.bk'
192.168.199.130 | CHANGED => {
"changed": true,
"checksum": "8f7d88e901a5ad3a05d8cc0de93313fd76028f8c",
"dest": "/opt/fstab.bk",
"gid": 0,
"group": "root",
"md5sum": "5a8dd3ad0756a93ded72b823b19dd877",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 6,
"src": "/root/.ansible/tmp/ansible-tmp-1540221051.34-78743719487515/source",
"state": "file",
"uid": 0
}
[root@web ~]# cat /opt/fstab.bk
hello!
6 file 模块
在ansible中使用file模块来设置文件属性。其中使用path指定文件路径,使用src定义源文件路径,使用name或dest来替换创建文件的符号链接。
下面我将web服务器中的fstab.bk文件属主设为mysql,属组设为mysql,权限设为666
[root@promote ~]# ansible web -m file -a 'path=/opt/fstab.bk owner=mysql group=mysql mode=666'
192.168.199.130 | CHANGED => {
"changed": true,
"gid": 306,
"group": "mysql",
"mode": "0666",
"owner": "mysql",
"path": "/opt/fstab.bk",
"secontext": "system_u:object_r:usr_t:s0",
"size": 6,
"state": "file",
"uid": 306
}
[root@web ~]# ll /opt/fstab.bk
-rw-rw-rw-. 1 mysql mysql 6 10月 22 23:10 /opt/fstab.bk
下面我为/opt/fstab.bk/创建一个链接文件