以前在公司推廣使用了ansible,這也使用一段時間了,分享下有關ansible模塊的使用相關介紹html
ansible 默認提供了不少模塊來供咱們使用。在 Linux 中,咱們能夠經過 ansible-doc -l 命令查看到當前 ansible 都支持哪些模塊,經過 ansible-doc -s 模塊名 又能夠查看該模塊有哪些參數可使用。linux
下面介紹比較經常使用的幾個模塊:git
copy模塊 file模塊 cron模塊 group模塊 user模塊 yum模塊 service模塊 script模塊 ping模塊 command模塊 raw模塊 get_url模塊 synchronize模塊
copy模塊:bash
目的:把主控端/root目錄下的a.sh文件拷貝到到指定節點上服務器
命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/'運維
執行效果:ssh
file模塊:ide
目的:更改指定節點上/tmp/t.sh的權限爲755,屬主和屬組爲rooturl
命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"spa
執行效果:
cron模塊:
目的:在指定節點上定義一個計劃任務,每隔3分鐘到主控端更新一次時間
命令:ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
執行效果:
group模塊:
目的:在全部節點上建立一個組名爲nolinux,gid爲2014的組
命令:ansible all -m group -a 'gid=2014 name=nolinux'
執行效果:
user模塊:
目的:在指定節點上建立一個用戶名爲nolinux,組爲nolinux的用戶
命令:ansible 10.1.1.113 -m user -a 'name=nolinux groups=nolinux state=present'
執行命令:
補充:刪除用戶示例
yum模塊:
目的:在指定節點上安裝 lrzsz 服務
命令:ansible all -m yum -a "state=present name=httpd"
執行效果:
service模塊:
目的:啓動指定節點上的 puppet 服務,並讓其開機自啓動
命令:ansible 10.1.1.113 -m service -a 'name=puppet state=restarted enabled=yes'
執行效果:
script模塊:
目的:在指定節點上執行/root/a.sh腳本(該腳本是在ansible控制節點上的)
命令:ansible 10.1.1.113 -m script -a '/root/a.sh'
執行效果:
ping模塊:
目的:檢查指定節點機器是否還能連通
命令:ansible 10.1.1.113 -m ping
執行效果:
command模塊:
目的:在指定節點上運行hostname命令
命令:ansible 10.1.1.113 -m command -a 'hostname'
執行效果:
raw模塊:
目的:在10.1.1.113節點上運行hostname命令
命令:ansible 10.1.1.113 -m raw-a 'hostname|tee'
執行效果:
get_url模塊:
目的:將http://10.1.1.116/favicon.ico文件下載到指定節點的/tmp目錄下
命令:ansible 10.1.1.113 -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
執行效果:
synchronize模塊:
目的:將主控方/root/a目錄推送到指定節點的/tmp目錄下
命令:ansible 10.1.1.113 -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
執行效果:
delete=yes 使兩邊的內容同樣(即以推送方爲主)
compress=yes 開啓壓縮,默認爲開啓
--exclude=.git 忽略同步.git結尾的文件
因爲模塊,默認都是推送push。所以,若是你在使用拉取pull功能的時候,能夠參考以下來實現
mode=pull 更改推送模式爲拉取模式
目的:將10.1.1.113節點的/tmp/a目錄拉取到主控節點的/root目錄下
命令:ansible 10.1.1.113 -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
執行效果:
因爲模塊默認啓用了archive參數,該參數默認開啓了recursive, links, perms, times, owner,group和-D參數。若是你將該參數設置爲no,那麼你將中止不少參數,好比會致使以下目的遞歸失敗,致使沒法拉取
其它相關的參數解釋:
dest_port=22 # 指定目的主機的ssh端口,ansible配置文件中的 ansible_ssh_port 變量優先級高於該 dest_port 變量 rsync_path # 指定 rsync 命令來在遠程服務器上運行。這個參考rsync命令的--rsync-path參數,--rsync-path=PATH # 指定遠程服務器上的rsync命令所在路徑信息 rsync_timeout # 指定 rsync 操做的 IP 超時時間,和rsync命令的 --timeout 參數效果同樣
OK!
以上暫且列舉這些平常運維中常常會用到的一些模塊,更多的模塊信息後期會繼續完善,你們也能夠去官網查看更多的信息。