Ansible学习笔记6

stat模块:获取文件的状态信息,类似Linux的stat状态。

获取/etc/fstab文件的状态。

bash 复制代码
[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模块是不能拷贝目录的。

文件拷贝是可以的。

bash 复制代码
[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目录下。

bash 复制代码
[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主机。

bash 复制代码
[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目录。

相关推荐
Apipi*2 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
天启HTTP3 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
广州浮点FLOATLIC6 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
BLUcoding6 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
AIgorithmGEEK6 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil2086 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)6 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter6256 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器
xh didida8 小时前
Linux -- 基础IO
linux·服务器·开发语言·c++
zly35009 小时前
centos7 mount设备挂载 /dev/sda1/硬盘挂载
linux·运维·服务器