文章目录
- Archive Unarchive
- Setup模块
- Lineinfile Replace
🏡作者主页:点击!
🤖Linux专栏:点击!
⏰️创作时间:2025年02月23日18点11分
Archive Unarchive
Archive和Unarchive模块
需求:主机192.168.1.100中 /tmp 中创建文件 test1、test2 将它们打包为 test.zip,同时删除 test1 和 test2,再将其解压至 /tmp /file 目录中
ansible 192.168.1.100 -m file -a "path=/tmp/test1 state=touch" #创建文件 ansible 192.168.1.100 -m file -a "path=/tmp/test2 state=touch" #创建文件 ansible 192.168.1.100 -m archive -a "path=/tmp/test1,/tmp/test2 format=zip remove=yes dest=/tmp/test.zip" #打包文件删除源文件 ansible 192.168.1.100 -m unarchive -a "remote_src=yes src=/tmp/test.zip dest=/tmp/file" #此时解压缩remote_src=yes意思是压缩包来自被控制节点,也就是远端节点 ansible 192.168.1.100 -m unarchive -a "copy=no src=/tmp/test.zip dest=/tmp/file/" #进行解压缩操作,因为是在远端主机上 #解压到的路径必须是一个已经存在的目录
Setup模块
不加参数表示的是全部信息
- filter:针对数据进行收集
- gather_subset:收集全量信息的子集,范围比 filter 更广一点
- gather_timeout:限定整个收集任务的超时时间
Setup模块实践
ansible 192.168.1.100 -m setup #收集信息
Lineinfile Replace
Lineinfile Replace模块实践
ansible 192.168.1.100 -m lineinfile -a "path=/tmp/file/test regexp='^Listen' line='Listen 8080' state=present" #使用命令对文件内容进行插入,此时就会出现一个test文件内容为Listen 8080 ansible 192.168.1.100 -m replace -a "path=/tmp/file/test regexp='Listen' replace='LISTEN' backup=yes" #将文件内容 Listen 改为 LISTEN,并且备份文件内容