stat模块:获取文件的状态信息,类似Linux的stat状态。
获取/etc/fstab文件的状态。
[root@localhost tmp]# ansible group1 -m stat -a "path=/etc/fstab"
192.168.17.106 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": false,"stat": {"atime": 1693322482.6770303,"attr_flags": "","attributes": [],"block_size": 4096,
得到了json格式的文件。key-value。对于开发的人很喜欢这种键值对的格式,开发的时候非常方便。能够通过key得到值。
template模块(拓展)
与copy模块功能几乎是一样的。
template模块是不能拷贝目录的。
文件拷贝是可以的。
[root@localhost ~]# ansible group1 -m template -a "src=/etc/hosts dest=/etc/hosts"
192.168.17.106 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": false,"checksum": "ae3c3e4f757591751a520e8a4bba45490e6c3164","dest": "/etc/hosts","gid": 0,"group": "root","mode": "0644","owner": "root","path": "/etc/hosts","size": 226,"state": "file","uid": 0
}
192.168.17.105 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": false,"checksum": "ae3c3e4f757591751a520e8a4bba45490e6c3164","dest": "/etc/hosts","gid": 0,"group": "root","mode": "0644","owner": "root","path": "/etc/hosts","size": 226,"state": "file","uid": 0
}
那我们涉及到copy文件,建议还是使用copy模块来实现。
template模块首先使用变量来渲染jinja2模板文件成普通文件,然后再复制过去。而copy不支持。(jinja2是一个基于python的模板引擎。)
fetch模块:
fetch模块跟copy模块功能类似,但是作用相反,用于把远程主机上的文件拷贝到本地。
场景:把很多服务器上的日志拷贝到本地。日志集中管理。日志分析。
PV:pageview:点击率。
将/tmp/1.txt文件拷贝到本地。根据每台主机的IP地址或者别名存放在/tmp目录下。
[root@localhost ~]# ansible group1 -m fetch -a "src=/tmp/1.txt dest=/tmp"
192.168.17.105 | CHANGED => {"changed": true,"checksum": "d2911a028d3fcdf775a4e26c0b9c9d981551ae41","dest": "/tmp/192.168.17.105/tmp/1.txt","md5sum": "0d59da0b2723eb03ecfbb0d779e6eca5","remote_checksum": "d2911a028d3fcdf775a4e26c0b9c9d981551ae41","remote_md5sum": null
}
192.168.17.106 | CHANGED => {"changed": true,"checksum": "b27fb3c4285612643593d53045035bd8d972c995","dest": "/tmp/192.168.17.106/tmp/1.txt","md5sum": "cd0bd22f33d6324908dbadf6bc128f52","remote_checksum": "b27fb3c4285612643593d53045035bd8d972c995","remote_md5sum": null
}
主要为了区分不同的agent主机。
[root@localhost tmp]# ll
total 4
drwxr-xr-x 3 root root 17 Aug 30 13:36 192.168.17.105
drwxr-xr-x 3 root root 17 Aug 30 13:36 192.168.17.106
-rwx------. 1 root root 836 Aug 29 14:56 ks-script-edvhAX
-rw-------. 1 root root 0 Aug 29 14:53 yum.log
[root@localhost tmp]# cat 192.168.17.105/tmp/1.txt
agent1
[root@localhost tmp]# cat 192.168.17.106/tmp/1.txt
agent2
如果只fetch一个文件,那么路径也是这么一个过程。
说明:需要注意的是,不能fetch目录。