ansible怎么使用fetch (ansible模块执行环境)

copy模块

copy模块是把本机的文件拷贝到远程机器,跟scp命令有点类似

常用参数如下:

src参数: 本地文件路径。必须参数

dest参数: 远程机器路径。必须参数

backup参数: 是否备份,若值是yes表示备份,如果不需要备份省略该参数。省略该参数后,若远程机器有同名的文件,远程机器上的文件会直接覆盖

mode参数: 复制后的文件权限,比如:"0755","0644"。如果没有该参数则复制后的文件与源文件权限相同

例1:把本地拷贝到远程机器不做备份,

[root@localhost ~]# ansible 192.168.233.167 -m copy -a "src=/root/test dest=/tmp/test"
192.168.233.167 | CHANGED => {
  "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
   }, 
  "changed": true, 
  "checksum": "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83", 
  "dest": "/tmp/test", 
  "gid": 0, 
  "group": "root", 
  "md5sum": "d8e8fca2dc0f896fd7cb4cb0031ba249", 
  "mode": "0644", 
  "owner": "root", 
  "size": 5, 
  "src": "/root/.ansible/tmp/ansible-tmp-1611939995.84-4445-83659486471571/source", 
  "state": "file", 
  "uid": 0
}

例2: 把本地文件拷贝到远程机器并备份

[root@localhost ~]# ansible 192.168.233.167 -m copy -a "src=/root/test dest=/tmp/test  backup=yes"
192.168.233.167 | CHANGED => {
  "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
   }, 
  "backup_file": "/tmp/test.2953.2021-01-29@17:08:14~", 
  "changed": true, 
  "checksum": "1b7d20fc0014599208b7ccea75f6f0c959af283b", 
  "dest": "/tmp/test", 
  "gid": 0, 
  "group": "root", 
  "md5sum": "80b9455d8672a7e9a2c5120462b2963d", 
  "mode": "0644", 
  "owner": "root", 
  "size": 10, 
  "src": "/root/.ansible/tmp/ansible-tmp-1611940092.92-4528-54138861592249/source", 
  "state": "file", 
  "uid": 0
}

此时,返回值会多一个"back_file"

如果本地文件内容和远程文件一样,则不会复制。返回内容也是深绿色的 ansible在复制文件的时候会判断文件内容是否相同

关于ansible执行返回的颜色简单说明一下:

绿色: 表示执行成功但是没做任何修改

黄色:表示执行成功并做了修改

红色:表示执行失败

浅绿色:表示跳过此次操作

有一个参数需要拿出来单独说一下,用到的可能不多,但可能会用到。

remote_src参数:这个参数表示操作都是在远程机器上操作

此时的src表示的也是远程机器的路径,而不是本地的。dest也是远程机器的路径.

例1: 远程机器上的文件拷贝

[root@localhost ~]# ansible 192.168.233.167 -m copy -a "src=/tmp/dirtest dest=/root/dirtest backup=yes remote_src=yes"
192.168.233.167 | CHANGED => {
  "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
   }, 
  "changed": true, 
  "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
  "dest": "/root/dirtest", 
  "gid": 0, 
  "group": "root", 
  "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
  "mode": "0644", 
  "owner": "root", 
  "size": 0, 
  "src": "/tmp/dirtest", 
  "state": "file", 
  "uid": 0
}

上面示例因为remote_src=yes, 所以src=/tmp/dirtestdest=/root/dirtest的两个路径都是在远程机器上。

fetch模块

copy模块是把本机的文件拷贝到远程机器,而fetch模块是把远程机器的文件拉取到本地

fetch模块刚好和copy模块的操作是相反的。因此参数的含义也是相反的

src参数: 远程机器上的文件或文件夹

dest参数: 保存到本地的目录

例1:拷贝远程文件到本地

[root@localhost ~]# ansible 192.168.233.167 -m fetch -a "src=/etc/hosts dest=/home/"
192.168.233.167 | CHANGED => {
  "changed": true, 
  "checksum": "68991e742192b6cc45ad7b95eb88ea289658f65c", 
  "dest": "/home/192.168.233.167/etc/hosts", 
  "md5sum": "32d544b36aefb5a7f800e75cca57ce8b", 
  "remote_checksum": "68991e742192b6cc45ad7b95eb88ea289658f65c", 
  "remote_md5sum": null
}

在本地/home目录下会生成远程机器IP的文件夹,拉取的文件就在该目录下。并且保留目录结构

[root@localhost ~]# tree /home/192.168.233.167/
/home/192.168.233.167/
└── etc
   └── hosts

这个模块很简单,就不过多赘述。