轉自:http://libra2068.blog.51cto.com/7075273/1652872
ansible 默認提供了很多模塊來供我們使用。在 Linux 中,我們可以通過 ansible-doc -l 命令查看到當前 ansible 都支持哪些模塊,通過 ansible-doc -s 模塊名 又可以查看該模塊有哪些參數可以使用。
下面介紹比較常用的幾個模塊:
copy模塊
file模塊
cron模塊
group模塊
user模塊
yum模塊
service模塊
script模塊
ping模塊
command模塊
raw模塊
get_url模塊
synchronize模塊
Ansible和Docker的作用和用法 http://www.linuxidc.com/Linux/2014-11/109783.htm
Ansible批量搭建LAMP環境 http://www.linuxidc.com/Linux/2014-10/108264.htm
Ansible :一個配置管理和IT自動化工具 http://www.linuxidc.com/Linux/2014-11/109365.htm
Linux下安裝部署Ansible http://www.linuxidc.com/Linux/2015-02/112774.htm
copy模塊:
目的:把主控端/root目錄下的a.sh文件拷貝到到指定節點上
命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/'
執行效果:
file模塊:
目的:更改指定節點上/tmp/t.sh的權限爲755,屬主和屬組爲root
命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"
執行效果:
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!
以上暫且列舉這些日常運維中經常會用到的一些模塊,更多的模塊信息後期會繼續完善,大家也可以去官網查看更多的信息。
官網地址:http://docs.ansible.com/synchronize_module.html