说明:
src - 是远程主机的文件,这个参数只能使用文件,不能使用目录
dest - 是本地主机,用来保存文件的目录。它会将这里的参数看成是目录。注意,在执行ansible 成功后,dest 的路径会自动将远程文件保存到 /tmp/centos/remote_host/tmp/fstab_from_centos6
上面例子的结果:
[root@centos_7 ansible]# ls /tmp/centos/example.fetch.host/tmp
fstab_from_centos6
example.fetch.host 是在 /etc/ansible/hosts 主机列表里 centos7 对应的名称
增加 flat 参数,当flat 为 yes 时,有两种情况:
(1)dest 是以"/" 结尾,怎文件直接保存在 dest 的目录下,所以现在的结果是:
fstab_from_centos6 保存在本地主机的 /tmp/fstab_from_centos6
fstab_from_centos6 被保存为 /tmp/cnetos
复制本地(或远程)文件 到远程主机
修改权限属性:
[root@centos_7 ansible]# ansible centos6 -m command -a “chdir=/tmp/new/ ls 1.txt -l”
192.168.188.109 | SUCCESS | rc=0 >>
-rw-r–r-- 1 root root 0 Jan 16 12:22 1.txt
[root@centos_7 ansible]# ansible centos6 -m file -a “path=/tmp/new/1.txt mode=0777”
192.168.188.109 | SUCCESS => {
“changed”: true,
“gid”: 0,
“group”: “root”,
“mode”: “0777”,
“owner”: “root”,
“path”: “/tmp/new/1.txt”,
“size”: 0,
“state”: “file”,
“uid”: 0
}
[root@centos_7 ansible]# ansible centos6 -m command -a “chdir=/tmp/new/ ls 1.txt -l”
192.168.188.109 | SUCCESS | rc=0 >>
-rwxrwxrwx 1 root root 0 Jan 16 12:22 1.txt
说明: 将远程主机 /tmp/new/1.txt 的文件修改权限属性为0777;
ansible 命令中,path 相当于 dest,指明了目标文件。
如果想修改所属主和所属组,可以:
ansible centos6 -m file -a “path=/tmp/new/1.txt mode=0755 ower=apple group=apple”
命令执行结果:
[root@centos_7 ansible]# ansible centos6 -m file -a “path=/tmp/new/1.txt mode=0755 owner=apple group=apple”
192.168.188.109 | SUCCESS => {
“changed”: true,
“gid”: 508,
“group”: “apple”,
“mode”: “0755”,
“owner”: “apple”,
“path”: “/tmp/new/1.txt”,
“size”: 0,
“state”: “file”,
“uid”: 508
}
[root@centos_7 ansible]# ansible centos6 -m command -a “chdir=/tmp/new/ ls 1.txt -l”
192.168.188.109 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 apple apple 0 Jan 16 12:22 1.txt
说明: 只要增加 owner 和 group 参数就可以了。
file 的 state 参数
熟悉 state 的几个参数,基本就满足我们平时对文件、目录和符号链接文件的操作了。
先来看看 state 有几个值? state 的值和我们要进行的操作,其实是一一对应的。
可以看到,在远程主机已经创建了 /tmp/testdir 目录:
[root@centos_7 ansible]# ansible test -m command -a “chdir=/tmp ls -lthr”
192.168.188.109 | SUCCESS | rc=0 >>
total 16K
-rw-r–r-- 1 root root 484 Jan 12 15:08 fstab
drwxr-xr-x 2 root root 4.0K Jan 16 12:22 new
drwxr-xr-x 2 root root 4.0K Jan 16 14:50 testdir
drwx------ 2 root root 4.0K Jan 16 14:51 ansible_IOZLxg
state=touch – 创建文件
[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/new/1.txt state=touch”
[root@centos_7 ansible]# ansible test -m command -a “chdir=/tmp/new ls -lthr”
192.168.188.109 | SUCCESS | rc=0 >>
total 0
-rw-r–r-- 1 root root 0 Jan 16 15:00 1.txt
注意: 使用 state=file ,当 path 指定的文件不存在的时候,并不能新创建文件。
重复对相同的文件使用 touch 只能改变文件的元数据属性,例如访问时间等等 stat 查看到的数据。
state=link – 创建符号链接
当stat=link 的时候,就需要使用另一个 src 的参数一起搭配使用了。
[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/new/1_link.txt src=/tmp/new/1.txt state=link”
结果:
[root@centos_7 ansible]# ansible test -m command -a “ls /tmp/new -l”
192.168.188.109 | SUCCESS | rc=0 >>
total 0
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r–r-- 1 root root 0 Jan 16 15:02 1.txt
说明: path 指定了生成的符号链接文件的路径, src 指定了 被链接的文件是什么。
[root@centos_7 ansible]# ansible test -m command -a "ls -l /tmp/new "
192.168.188.109 | SUCCESS | rc=0 >>
total 0
-rw-r–r-- 2 root root 0 Jan 16 15:02 1_hard.txt
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r–r-- 2 root root 0 Jan 16 15:02 1.txt
可以看到,1.txt 和 1_hard.txt 文件的属性都是一样的。
通俗来讲,就是在本地 file.j2 文件里的变量状态。例如在file.j2 文件里有变量: username="{{ admin_username }}" password="{{ admin_password }}"。
那么,file.j2 文件会一直保持变量状态,直到 file.j2 文件被 ansible 的 template 模块执行后,文件里的变量就会被具体的值代替,并且传送到远程主机。
例如,在roles 里面调用了 template 模块,那么ansible 会在 roles 同级目录 global_vars 里找到存储变量的文件,或者有时会在 roles 目录里的