介紹:
node
ansible是基於Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優勢nginx
具備批量系統配置、批量程序部署、批量運行命令等功能。git
是基於模塊工做的,自己沒有批量部署的能力。真正具備批量部署的是ansible所運行的模塊,ansible只是提供一種框架。github
操做:web
如下經過實際演練來更好的理解ansible的工做原理redis
使用三臺機器組建ansible使用環境shell
注意:三臺主機要網絡同步時間,101主機做爲ansible控制機,106和107主機做爲普通服務器centos
yum -y install ansible
bash
#安裝ansible服務器
基於密鑰進行ssh認證
配置/etc/ansible/hosts
ssh-keygen -t rsa -P ''
for i in 106 107;do ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.$i ;done
ansible -help -m 指定調用的模塊 -a 每一個模塊都有本身的專有的參數,指定此模塊參數 -f 指定每批處理多少臺主機,默認5臺 #更多的參數可自行了解 eg: ansible all --list-hosts #列出當前處於同一模式的全部主機 ansible webservers --list-hosts
ping模塊: ansible all -m ping #測試ansible到各個服務器的連通性 192.168.1.106 | SUCCESS => { #狀態是成功的,即連通性是沒問題 "changed": false, #修改狀態是失敗,說明咱們未作任何修改操做 "ping": "pong" #ansible發出ping驗證,對方返回pong告知鏈接正常 } 192.168.1.107 | SUCCESS => { "changed": false, "ping": "pong" }
ansible-doc -h #查看ansible文檔的幫助信息 ansible-doc -l #列出ansible支持的全部模塊信息,如上文只能中的ping模 ansible-doc -s command #查看指定模塊的參數信息,command是一種經常使用的模塊,默認不指定模塊時就是使用command
command模塊:
[root@node1 ~]#ansible 192.168.1.106 -m command -a 'pwd chdir=/app' #在執行命令pwd以前,先cd進入chdir指定的目錄,因此結果顯示目錄是/app而不是root的家目錄 #-m指定模塊,-a指定參數(基本都是鍵值對),只是這裏的參數其實是兩臺命令一個是pwd,一個是chdir,而這兩個命令又有前後之分 ansible 192.168.1.106 -m command -a 'mkdir dir chdir=/app creates=dir' #建立目錄dir,creates表示若是dir已經存在就再也不執行mkdir操做,直接跳過。此過程實現冪等性 #冪等:執行一次和執行屢次的結果都是同樣的 #creates表示指定路徑文件不存在才執行指令,而removes則相反,表示指定路徑文件存在才執行指令 #creates和removes含義相反,注意區分,並且他們都是針對指定目錄下的文件而不針對用戶或進程操做 command模塊是不能是被shell語法的,全部在用到如bash類的shell時可使用shell模塊 ansible all -m shell -a "echo centos| passwd --stdin user1" #shell模塊與command模塊基本相同,只是此模塊支持shell類型的語法 #若是換成command模塊,則結果是echo後的內容所有做爲字符串輸出,即command是不識別管道和passwd等shell語言的
user和group模塊:
group模塊: ansible webservers -m group -a 'name=group1 system=yes state=present' #使用group組模塊,向webservers中建立組gruop1,而且是系統組,present是建立此組的含義,absent是刪除此組的意思 192.168.1.106 | SUCCESS => { "changed": true, "gid": 983, "name": "group1", "state": "present", "system": true } #組id、組名稱、狀態、系統組,操做成功 user模塊: ansible webservers -m user -a "name=tomm groups=group1 state=present uid=20000 shell=/bin/csh" #建立tomm用戶,指定輔助組、uid和shell類型,建立present, 192.168.1.106 | SUCCESS => { #上述操做成功 "changed": true, "comment": "", "createhome": true, #建立家目錄 "group": 20000, # "groups": "group1", "home": "/home/tomm", #未指定用戶家目錄,則採用默認 "name": "tomm", "shell": "/bin/csh", "state": "present", "system": false, #表示不是系統用戶 "uid": 20000 } #此時webservers組內的成員都建立了用戶tomm
copy模塊:
將文件複製到遠程位置 ansible webservers -m copy -a 'src=/root/aaa dest=/app/ owner=daemon group=nobody mode=664' #使用copy模塊,將本地的aaa文件複製到遠程主機webservers組中,並修改用戶/組和權限 192.168.1.106 | SUCCESS => { "changed": true, "checksum": "7272fa0670a2f6d5cf0f5c1e6f31641fad625bf8", "dest": "/app/aaa", "gid": 99, "group": "nobody", "md5sum": "890b22b2dc6ff3f00f2374dce5634526", "mode": "0664", "owner": "daemon", "secontext": "system_u:object_r:default_t:s0", "size": 1493, "src": "/root/.ansible/tmp/ansible-tmp-1510928315.78-72588321449487/source", #生成臨時文件,而不是直接將源文件複製過去 "state": "file", "uid": 2 } #此時對aaa文件進行修改後在執行此操做,會發現changed字段依然是true,緣由是aaa修改後的hash值改變了 ansible webservers -m copy -a "content='hello,ansible' dest=/app/bbb owner=daemon group=nobody mode=664" #本機沒有源文件而只是指定字符串,到目標webservers組,即遠程主機上就會生成bbb文件,並設置成相應的所屬用戶權限等信息 #content只是修改文件內容,若是針對文件自己屬性進行操做,可使用file模塊
file模塊:
此模塊僅設置文件屬性,不改變文件內容 ansible webservers -m file -a "path=/app/testdir state=directory owner=nobody mode=770" #在遠程主機上建立目錄,state後能夠指定目錄、文件和軟硬連接,並設置屬性 ansible webservers -m file -a "path=/app/testfile state=touch owner=nobody mode=666" #state用法比較奇特,除了指定文件類型外還能夠建立文件,即state爲touch ansible webservers -m file -a "path=/app/filelink src=/app/testfile state=link" #link表示符號連接,若是是hard表示硬連接 ansible webservers -m file -a "path=/app/filelink state=absent" #absent表示path指定文件刪除文件
get_url模塊:
從互聯網獲取文件到本節點,可經過http、https和ftp實現
ansible webservers -m get_url -a "url=http://download.redis.io/releases/redis-4.0.2.tar.gz dest=/app/" #url指定下載的資源路徑,dest指定下載到指定的目錄,此外還能夠指定下載文件的屬主信息以及校驗信息如sha256sum
cron模塊:
計劃任務 ansible webservers -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1' minute='*/5'" #建立熱舞計劃,name指定計劃任務的名稱,job指定任務計劃的操做,minute表示執行的時間點即每五分鐘執行一次同步時間操做 ansible webservers -m cron -a "name='timesync' state=absent" #刪除計劃任務 ansible webservers -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1' minute='*/5' disabled=true" #disabled表示雖然建立計劃任務可是不啓用,要啓用的話將true改成false
yum模塊:
ansible webservers -m yum -a "name=nginx state=latest" #安裝nginx,最新版本,安裝過程當中的信息會所有輸出值屏幕端
service模塊:
管理服務 ansible webservers -m service -a "name=nginx enabled=true state=started" #開啓nginx服務,並設置成開機自啓
git模塊:
ansible webservers -m git -a "repo=https://github.com/magro/memcached-session-manager.git dest=/app/git" #repo指定git文件路徑,dest指定git文件的存放位置,存放的目錄不能有內容 #前提是遠程主機須要提早安裝git