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目录。

相关推荐
晓梦.14 分钟前
Linux
linux·运维·服务器
运维帮手大橙子22 分钟前
Linux如何安装使用Rust指南
linux·运维·rust
千百元33 分钟前
centos怎么查看磁盘是机械还是固态
linux·运维·centos
LoneEon35 分钟前
CentOS 7 系统安装教程
linux·运维·centos
ssswywywht1 小时前
搭建本地时间同步服务器
linux·运维·服务器
你想考研啊1 小时前
自动化部署脚本
linux·服务器·自动化
超Pro1 小时前
MAC M芯片安装配置VMware+Ubuntu
linux·ubuntu
熙xi.2 小时前
Linux I²C 总线驱动开发:从架构到实战的完整指南
linux·c语言·驱动开发
---学无止境---2 小时前
Linux中页面写回初始化page_writeback_init函数实现
linux
FJW0208142 小时前
Linux编辑神器——vim工具的使用
linux·运维·vim