文章目錄
- 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,并且備份文件內容