1. 程式人生 > >ansible常用模塊的用法

ansible常用模塊的用法

RoCE sta pts oss chm www null 編輯 新建文件夾

模塊文件
ping 模塊 相對簡單,沒有參數
ansible 192.168.56.200 -m ping

command 模塊
ansible websrvs -m command -a ‘chdir=/etc/ cat centos-release‘
使用command時不支持*管道的使用$也不支持

shell 模塊
ansible websrvs -m shell -a "echo 123456 | passwd --stdin yunlong"
ansible websrvs -m shell -a ‘echo $HOSTNAME‘ #註意此處為單引號
[[email protected] ~]#ansible websrvs -m shell -a "chdir=/data echo welcome to china > haha.log; cat haha.log"

192.168.56.200 | CHANGED | rc=0 >>
welcome to china

ansible websrvs -m shell -a "sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config; cat /etc/selinux/config"

ansible websrvs -m shell -a ‘rm -rf /data/*‘ #刪除目錄下所有文件
scripts模塊
實現將本機的腳本在被控端執行
如在本機新建一個腳本vim /data/hello.sh,內容為hostname

chmod +x /data/hello.sh
ansible websrvs -m script -a ‘/data/hello.sh‘

copy 模塊:從主控端復制文件到遠程主機
如在遠程主機新建一個yum源然後再刪除該yum源
ansible websrvs -m copy -a ‘content="[test]\nbaseurl=file:///misc/cd\ngpgcheck=0" dest=/etc/yum.repos.d/test.repo‘
ansible websrvs -m shell -a ‘rm /etc/yum.repos.d/test.repo‘

fetch模塊:從遠程主機提取文件至主控端,於copy相反,目錄可先tar

ansible websrvs -m shell -a ‘tar jcvf /root/data.tar.bz2 /data/‘ #將被控端主機打包,註意j選項適用於bz2格式,J適用於xz格式,z適用於gz
ansible websrvs -m fetch -a ‘src=/root/data.tar.bz2 dest=/data‘ #將被控端打包壓縮好的文件提取至主控端/data下

file模塊,設置文件屬性(src為遠程主機內的文件,path為被控端的文件)
ansible websrvs -m file -a ‘path=/data/haha.log owner=yunlong mode=000‘
設置軟連接
ansible websrvs -m file -a ‘src=/data/haha.log name=/data/haha.log.link state=link‘
ansible websrvs -m shell -a ‘ls -l /data‘ #查看生成的軟連接
刪除軟連接
ansible websrvs -m file -a ‘src=/data/haha.log name=/data/haha.log.link state=absent‘
硬鏈接將state=hard即可
新建文件夾
ansible websrvs -m file -a ‘path=/data/dir1 state=directory‘ #新建文件夾
ansible websrvs -m shell -a ‘ls -l /data‘查看新建文件夾
新建空文件
ansible websrvs -m file -a ‘path=/data/dir1/hao.txt state=touch‘
ansible websrvs -m shell -a ‘ls -l /data/dir1/‘ #查看新建文件
刪除文件或者文件夾
ansible websrvs -m file -a ‘path=/data/dir1/hao.txt state=absent‘

hostname模塊
ansible websrvs -m hostname -a ‘name=haha.localdomain‘ #修改主機名為haha.localdomain
ansible websrvs -m shell -a ‘hostname‘ #查看修改是否生效,如果是centos6,相應的主機名存放路徑/etc/sysconfig/network下也作了相應修改,centos7中主機名存放路徑為/etc/hostname,建議主機名在相應的/etc/hosts下手動添加相應主機名

計劃任務cron模塊
時間同步在主控端centos7中
檢查是否安裝rpm -qi chrony
打開vim /etc/chrony.conf

Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 172.22.0.1 iburst    #第一步設置本地服務器為主控端同步服務器
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst

# Allow NTP client access from local network.
allow 192.168.56.0/24    #第二步在主控端設置允許哪些機器同步自己

# Serve time even if not synchronized to a time source.
local stratum 10   #第三步即使第一步中同步失敗,自身也可作為時間同步對第二部中的機器提供時間同步服務
 systemctl status chronyd      #第四步修改完後查看chronyd 服務狀態
 systemctl start chronyd       #第四步修改完後啟動chronyd 服務
 systemctl enable chronyd    #第四步修改完後設置chronyd 服務為開機啟動
 systemctl restart chronyd    #修改配置文件後重啟
     ss -ntlu                              #啟動成功後查看端口號為323 ,123
在被控端(也是192.168.56.0網段)輸入如在centos6中ntpdate 主控端IP地址 即可同步

回憶在本機設置計劃任務crontab -e   回車即可進入編輯
 ansible websrvs -m cron -a ‘name=synctime minute=*/5 job="/usr/sbin/ntpdate 主控端IP &> /dev/null"‘     #創建計劃任務name為計劃任務名稱,minute為每5分鐘同步,job為計劃任務執行內容
 ansible websrvs -m shell -a ‘crontab -l‘    #列出計劃任務列表
 ansible websrvs -m cron -a ‘name=synctime minute=*/5 job="/usr/sbin/ntpdate 主控端IP &> /dev/null" disabled=true‘     #禁用計劃任務
 ansible websrvs -m cron -a ‘name=synctime minute=*/5 job="/usr/sbin/ntpdate 主控端IP &> /dev/null" disabled=false‘     #啟用計劃任務

 ansible websrvss -m cron -a ‘name=synctime minute=*/5 job="/usr/sbin/ntpdate 主控端IP &> /dev/null" state=absent‘    #刪除計劃任務
yum包管理模塊
 註意需要提前在被控端進行yum源配置,保證能在本機通過yum源安裝
 ansible websrvs -m yum -a ‘name=httpd‘    #指定安裝包名稱,默認安裝,通過ansible-doc yum查看使用幫助
 ansible websrvs -m yum -a ‘rpm -qi httpd‘    #查詢安裝情況
在本端通過rpm -qi 安裝包,查詢安裝情況

 ansible websrvs -m yum -a ‘name=httpd state=absent‘    #卸載httpd服務
 ansible websrvs -m yum -a ‘name=bind‘   #安裝bindDNS服務
 ansible websrvs -a ‘ss -ntlu |grep 53‘     #安裝後查看是否有了53端口下面重啟服務才會有

 service模塊
     ansible websrvs -m service -a ‘name=named state=started enabled=true‘    #bind服務名為named,開機啟動
 ansible websrvs -a ‘ss -ntlu | grep 53‘    #此時端口53出現了
 ansible websrvs -m service -a ‘name=named state=stopped‘    #停止服務,端口號就沒了

ansible websrvs -a ‘service named status‘ #查看此時服務已關閉,設置了默認模塊為shell
技術分享圖片

ansible常用模塊的用法