简单使用ansible-playbook
1.使用以下命令給客戶端安裝httpd服務(wù):
[root@server ~]# ansible testhost -m yum -a "name=httpd" 192.168.77.128 | SUCCESS => {"changed": true, "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.cn99.com\n * epel: mirrors.tongji.edu.cn\n * extras: mirrors.aliyun.com\n * updates: mirrors.163.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-67.el7.centos.6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-67.el7.centos.6 updates 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n Verifying : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-67.el7.centos.6 \n\nComplete!\n"] }2.執(zhí)行以下命令啟動(dòng)httpd服務(wù):
[root@server ~]# ansible testhost -m service -a "name=httpd state=started enabled=yes"## 然后會(huì)輸出一堆狀態(tài)信息,只要第一句為SUCCESS則代表啟動(dòng)成功注:這里的name是centos系統(tǒng)里的服務(wù)名,可以通過chkconfig --list查看到。
其他控制服務(wù)的命令:
# 停止服務(wù) [root@server ~]# ansible testhost -m service -a "name=httpd state=stopped" # 重新啟動(dòng)服務(wù) [root@server ~]# ansible testhost -m service -a "name=httpd state=restarted" # 重載服務(wù) [root@server ~]# ansible testhost -m service -a "name=httpd state=reloaded"3.在name后面還可以加上state=installed或removed,加上removed的話,表示卸載這個(gè)服務(wù),如果不指定state的值默認(rèn)是installed:
[root@server ~]# ansible testhost -m yum -a "name=httpd state=removed" 192.168.77.128 | SUCCESS => {"changed": true, "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror, langpacks\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-67.el7.centos.6 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.4.6-67.el7.centos.6 @updates 9.4 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n Verifying : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.4.6-67.el7.centos.6 \n\nComplete!\n"] } [root@server ~]#然后到客戶端上通過rpm -qa httpd命令查看是否已卸載成功:
[root@client ~]# rpm -qa httpd [root@client ~]#Ansible文檔的使用:
1.列出所有可用的模塊命令:
ansible-doc -l
2.查看指定模塊的文檔,例如我要查看cron模塊的文檔,可使用以下命令:
ansible-doc cron
ansible-doc后面跟模塊名就可以查看該模塊的文檔。
24.22 使用ansible playbook
playbook相當(dāng)于可以把模塊命令都寫入到配置文件里面,這樣就可以直接執(zhí)行配置文件了,有點(diǎn)腳本的意思:
[root@server ~]# vim /etc/ansible/test.yml --- - hosts: testhostremote_user: roottasks:- name: test_playbookshell: touch /tmp/test.txt文件格式說明:
- 第一行需要有三個(gè)杠,hosts參數(shù)指定了對(duì)哪些主機(jī)進(jìn)行參作,如果是多臺(tái)機(jī)器可以用逗號(hào)作為分隔,也可以使用主機(jī)組,在/etc/ansible/hosts里定義;
- user參數(shù)指定了使用什么用戶登錄遠(yuǎn)程主機(jī)操作;
- tasks指定了一個(gè)任務(wù),其下面的name參數(shù)同樣是對(duì)任務(wù)的描述,在執(zhí)行過程中會(huì)打印出來,shell是ansible模塊名字
編輯完成之后,使用ansible-playbook命令執(zhí)行該文件:
[root@server ~]# ansible-playbook /etc/ansible/test.ymlPLAY [testhost] ***********************************************************************************************************TASK [Gathering Facts] **************************************************************************************************** ok: [192.168.77.128]TASK [test_playbook] ******************************************************************************************************[WARNING]: Consider using file module with state=touch rather than running touchchanged: [192.168.77.128]PLAY RECAP **************************************************************************************************************** 192.168.77.128 : ok=2 changed=1 unreachable=0 failed=0 [root@server ~]#然后到客戶端上看看是否有創(chuàng)建test.txt文件:
[root@client ~]# ls -l /tmp/test.txt -rw-r--r-- 1 root root 0 1月 30 11:58 /tmp/test.txt [root@client ~]#如上,代表執(zhí)行成功。
24.23 playbook里的變量
我們通過一個(gè)創(chuàng)建用戶的例子,來演示一下playbook里的變量使用方式:
[root@server ~]# vim /etc/ansible/create_user.yml # 編輯內(nèi)容如下 --- - name: create_userhosts: testhostuser: rootgather_facts: falsevars:- user: "test"tasks:- name: create useruser: name="{{ user }}"說明:
- name參數(shù)對(duì)該playbook實(shí)現(xiàn)的功能做一個(gè)概述,后面執(zhí)行過程中,會(huì)打印 name變量的值 ,可以省略;
- gather_facts參數(shù)指定了在以下任務(wù)部分執(zhí)行前,是否先執(zhí)行setup模塊獲取主機(jī)相關(guān)信息,如果需要在后面的tasks里獲取setup收集到的信息,就需要把這個(gè)參數(shù)設(shè)置為True;
- vars參數(shù),指定了變量,這里聲明了一個(gè)user變量,其值為test ,需要注意的是,變量值一定要用引號(hào)引住;
- user提定了調(diào)用user模塊,name是user模塊里的一個(gè)參數(shù),而增加的用戶名字調(diào)用了上面user變量的值。
執(zhí)行該文件:
[root@server ~]# ansible-playbook /etc/ansible/create_user.ymlPLAY [create_user] ********************************************************************************************************TASK [create user] ******************************************************************************************************** changed: [192.168.77.128]PLAY RECAP **************************************************************************************************************** 192.168.77.128 : ok=1 changed=1 unreachable=0 failed=0 [root@server ~]#到客戶端上看看用戶是否已創(chuàng)建:
[root@client ~]# id test uid=1003(test) gid=1003(test) 組=1003(test) [root@client ~]#24.24 playbook里的循環(huán)
playbook除了有變量,還有循環(huán)語句,以下通過一個(gè)簡(jiǎn)單的例子來演示一下循環(huán)的使用方式:
[root@server ~]# vim /etc/ansible/while.yml --- - hosts: testhostuser: roottasks:- name: change mode for filesfile: path=/tmp/{{ item }} state=touch mode=600with_items:- 1.txt- 2.txt- 3.txt說明:
- file模塊可以對(duì)文件進(jìn)行相關(guān)的操作,例如創(chuàng)建文件或者更改文件權(quán)限等,具體可以查看該模塊的文檔
- with_items為循環(huán)的對(duì)象,相當(dāng)于是一個(gè)數(shù)組或集合,寫在下面的1.txt、2.txt以及3.txt是該集合的元素。而item則表示的是遍歷出來的元素,也就是說item指代的是1.txt、2.txt以及3.txt。
- state的值設(shè)置為touch表示如果該文件不存在就進(jìn)行創(chuàng)建
- path表示文件的路徑
- mode設(shè)置權(quán)限
執(zhí)行該文件:
[root@server ~]# ansible-playbook /etc/ansible/while.yml PLAY [testhost] ***********************************************************************************************************TASK [Gathering Facts] **************************************************************************************************** ok: [192.168.77.128]TASK [change mode for files] ********************************************************************************************** changed: [192.168.77.128] => (item=1.txt) changed: [192.168.77.128] => (item=2.txt) changed: [192.168.77.128] => (item=3.txt)PLAY RECAP **************************************************************************************************************** 192.168.77.128 : ok=2 changed=1 unreachable=0 failed=0 [root@server ~]#到客戶端上看看文件是否已創(chuàng)建:
[root@client ~]# ll /tmp/*.txt -rw------- 1 root root 0 1月 30 15:54 /tmp/1.txt -rw------- 1 root root 0 1月 30 15:54 /tmp/2.txt -rw------- 1 root root 0 1月 30 15:54 /tmp/3.txt [root@client ~]#24.25 playbook里的條件判斷
我們都知道在腳本中循環(huán)和條件判斷是必不可少的語句,所以在playbook里這兩種語句也是有的,循環(huán)我們已經(jīng)介紹完了,接下來我們通過一個(gè)簡(jiǎn)單的創(chuàng)建文件的例子演示一下條件判斷語句的使用方式。
我們一般以setup模塊收集到的主機(jī)信息,來作為判斷條件。所以在編寫代碼之前,我們需要先獲取相應(yīng)的信息,例如我要以ip地址來作為判斷條件,那么我就得先從setup里獲取主機(jī)ip的相關(guān)信息。
執(zhí)行以下命令可以查看到setup收集到的所有的facter信息,輸出的信息是JSON格式的:
ansible testhost -m setup
編寫文件內(nèi)容如下:
[root@server ~]# vim /etc/ansible/when.yml --- - hosts: testhostuser: rootgather_facts: Truetasks:- name: use whenshell: touch /tmp/when.txtwhen: ansible_eno16777736.ipv4.address == "192.168.77.128"說明:
- ansible_eno16777736是一個(gè)數(shù)組存儲(chǔ)著網(wǎng)卡相關(guān)信息,ipv4屬于該數(shù)組的子元素,但是ipv4也是一個(gè)數(shù)組,而address則是ipv4數(shù)組的子元素。我們需要使用address 來作為判斷條件。
- 所以要訪問address就需要使用這樣的格式:ansible_eno16777736.ipv4.address,address表示的是鍵,而"192.168.77.128"則是值,when為判斷語句相當(dāng)于if,所以其判斷條件為:該鍵的值為"192.168.77.128"時(shí)就執(zhí)行shell模塊里定義的語句。
執(zhí)行該文件:
[root@server ~]# ansible-playbook /etc/ansible/when.yml PLAY [testhost] ***********************************************************************************************************TASK [Gathering Facts] **************************************************************************************************** ok: [192.168.77.128]TASK [use when] ***********************************************************************************************************[WARNING]: Consider using file module with state=touch rather than running touchchanged: [192.168.77.128]PLAY RECAP **************************************************************************************************************** 192.168.77.128 : ok=2 changed=1 unreachable=0 failed=0 [root@server ~]#到客戶端上看看文件是否已創(chuàng)建:
[root@client ~]# ll /tmp/when.txt -rw-r--r-- 1 root root 0 1月 30 16:33 /tmp/when.txt [root@client ~]#24.26 playbook中的handlers
有一種情況就是執(zhí)行了tasks里面的內(nèi)容之后,服務(wù)器發(fā)生了變化,這時(shí)我們可能需要執(zhí)行一些相關(guān)的操作。例如我們修改了某個(gè)服務(wù)的配置文件后,則需要重啟一下服務(wù)。而handlers就是完成這樣的事情的,它相當(dāng)于編程中的回調(diào)函數(shù),當(dāng)tasks里的內(nèi)容執(zhí)行成功后,就會(huì)執(zhí)行handlers里定義的內(nèi)容。也類似于shell腳本中的&&符號(hào),例如 cat 1.txt && rm -f 1.txt ,當(dāng)cat 1.txt命令執(zhí)行成功之后就會(huì)執(zhí)行rm -f 1.txt命令,否則不執(zhí)行。
下面用一個(gè)簡(jiǎn)單的例子來演示一下handlers的使用方式:
[root@server ~]# vim /etc/ansible/handlers.yml --- - name: handlers testhosts: testhostuser: roottasks:- name: copy filecopy: src=/etc/passwd dest=/tmp/test_passwd.txtnotify: test handlershandlers:- name: test handlersshell: echo "This is a test string" >> /tmp/test_passwd.txt說明:
- 只有copy模塊執(zhí)行成功后,才會(huì)去調(diào)用下面的handlers里定義的內(nèi)容。也就是說如果/etc/passwd和/tmp/test_passwd.txt內(nèi)容是一樣的話,就不會(huì)去執(zhí)行handlers里面的shell相關(guān)命令,因?yàn)閏opy沒有被執(zhí)行。 這種比較適合配置文件發(fā)生更改后,重啟服務(wù)的操作。
- notify用于指定handlers的name參數(shù)的值,因?yàn)閔andlers可以定義多個(gè),所以需要使用notify來進(jìn)行指定調(diào)用哪一個(gè)。
執(zhí)行該文件:
[root@server ~]# ansible-playbook /etc/ansible/handlers.yml PLAY [handlers test] ******************************************************************************************************TASK [Gathering Facts] **************************************************************************************************** ok: [192.168.77.128]TASK [copy file] ********************************************************************************************************** changed: [192.168.77.128]RUNNING HANDLER [test handlers] ******************************************************************************************* changed: [192.168.77.128]PLAY RECAP **************************************************************************************************************** 192.168.77.128 : ok=3 changed=2 unreachable=0 failed=0 [root@server ~]#到客戶端上看看文件末尾的那一行是否是我們echo進(jìn)去的那一行內(nèi)容:
[root@client ~]# tail -n1 /tmp/test_passwd.txt This is a test string[root@client ~]#
本文轉(zhuǎn)自 ZeroOne01 51CTO博客,原文鏈接:http://blog.51cto.com/zero01/2066913,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的简单使用ansible-playbook的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 启动手机浏览器,php实现读取手
- 下一篇: java修饰方法有哪些,探讨Java语言