目錄
一、handlers和notify結(jié)合使用觸發(fā)條件
1.新建httpd.yml文件
2.復(fù)制配置文件到ansible的files目錄中
3.卸載被控機(jī)已安裝的httpd
4.執(zhí)行httpd.yml腳本
5.更改httpd.conf配置文件
6.使用handlers
7.重新執(zhí)行httpd.yml腳本
8.檢查被控機(jī)的端口號是否改變
9.handlers也可以觸發(fā)多個(gè)
二、tags
1.通過指定標(biāo)簽來執(zhí)行特定的動(dòng)作
2.停止httpd服務(wù)
3.檢查是否成功停止
4.執(zhí)行啟動(dòng)服務(wù)的tags
5.重新檢查服務(wù)是否啟動(dòng)
6.也可以針對多個(gè)標(biāo)簽執(zhí)行腳本
7.多個(gè)動(dòng)作對應(yīng)一個(gè)標(biāo)簽tags
三、playbook中變量的使用
1.定義1個(gè)變量
(1)編寫腳本
(2)啟動(dòng)腳本時(shí)對腳本中的變量進(jìn)行賦值
(3)檢查程序是否成功安裝、服務(wù)是否成功開啟
2.定義2個(gè)變量
(1)編寫腳本
(2)執(zhí)行腳本
(3)查看程序是否安裝成功
(4)卸載程序
3.在腳本內(nèi)部給變量賦值
(1)編寫腳本
(2)執(zhí)行腳本
(3)檢查程序是否成功安裝
(4)卸載程序
4.在主機(jī)清單中給腳本定義變量
一、handlers和notify結(jié)合使用觸發(fā)條件
Handlers
??????? 是task列表,這些task與前述的task的task并沒有本質(zhì)上的不同,用于當(dāng)關(guān)注的資源發(fā)生變化時(shí),才會采取一定的操作。
????????Notify此action可用于在每個(gè)play的最后被觸發(fā),這樣可避免多次有改變發(fā)生時(shí)每次都執(zhí)行指定的操作,僅在所有的變化發(fā)生完成后一次性地執(zhí)行指定操作。在notify中列出的操作成為handler,也即notify中調(diào)用handler中定義的操作。
例如:更新配置文件,重新啟動(dòng)playbook,系統(tǒng)不會重啟服務(wù)。
1.新建httpd.yml文件
[root@ansible145 ansible]# vim httpd.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd
- name: copy conf file
copy: src=files/httpd.conf dest=/etc/httpd/conf/ backup=yes
- name: start service
service: name=httpd state=started enabled=yes
2.復(fù)制配置文件到ansible的files目錄中
[root@ansible145 ansible]# ll /etc/httpd/conf/httpd.conf
-rw-r--r--. 1 root root 11753 May 30 21:49 /etc/httpd/conf/httpd.conf
[root@ansible145 ansible]# cp /etc/httpd/conf/httpd.conf files/
[root@ansible145 ansible]# tree
.
├── files
│?? ├── httpd.conf
│?? ├── index.html
│?? └── test.html
├── file.yml
├── hello2.yml
├── hello.yml
├── host.sh
└── selinux
3.卸載被控機(jī)已安裝的httpd
檢查端口號,80端口已開啟,需要卸載httpd
卸載命令
[root@ansible145 ansible]# ansible all -m yum -a 'name=httpd state=absent'
檢查被控機(jī)的80端口已消失
4.執(zhí)行httpd.yml腳本
# 檢查語法
[root@ansible145 ansible]# ansible-playbook -C httpd.yml
# 語法沒問題再執(zhí)行腳本
[root@ansible145 ansible]# ansible-playbook -C httpd.yml
被控機(jī)已成功安裝并啟動(dòng)httpd服務(wù),80端口已啟動(dòng)
[root@ansible145 ansible]# ansible websrvs -m shell -a 'ss -ntl | grep :80'
192.168.22.142 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
192.168.22.141 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
5.更改httpd.conf配置文件
修改端口號為8080
[root@ansible145 ansible]# vim ./files/httpd.conf
再次執(zhí)行httpd.yml后,檢查80端口號,依然還在啟動(dòng),沒有變?yōu)?080
6.使用handlers
因此,當(dāng)我們修改程序的配置文件時(shí),重新執(zhí)行ansible腳本需要重啟服務(wù),變?yōu)樽钚碌呐渲煤头?wù),此時(shí),就需要用到handlers。
---
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd
- name: copy conf file
copy: src=files/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart service # 標(biāo)記改變的任務(wù),名字要與handlers下的name一一對應(yīng)
# 告訴handlers,當(dāng)配置文件發(fā)生改變時(shí),執(zhí)行handlers下面的重啟服務(wù)任務(wù)
- name: start service
service: name=httpd state=started enabled=yes
handlers: # 當(dāng)上面的某個(gè)或某些任務(wù)改變時(shí),執(zhí)行下面的任務(wù)
- name: restart service
service: name=httpd state=restarted
更改主控機(jī)的配置文件,因?yàn)榈诙螆?zhí)行腳本時(shí),更改后的配置文件8080端口已經(jīng)復(fù)制到被控機(jī)上了,如果不更改,就相當(dāng)于配置文件沒有變。
[root@ansible145 ansible]# vim ./files/httpd.conf
7.重新執(zhí)行httpd.yml腳本
[root@ansible145 ansible]# ansible-playbook httpd.yml
8.檢查被控機(jī)的端口號是否改變
端口號改變,服務(wù)成功重啟!
9.handlers也可以觸發(fā)多個(gè)
- hosts: websrvs
remnte user: root
tasks:
- name: add group nginx
tags: user
user: name=nginx state=present
- name: add user nginx
user: name=nginx state=present group=nginx
- name: Inslall Ngiix
yum: name=nginx state=present
- name: config
copy: src=/root/contig.txt dest=/etc/nginx/nginx.conf
notify:
- Restart Nginx
- Check Nginx Process
handlers:
- name: Restart Nginx
service: name=nginx state=restarted enabled=yes
- name: Check Nginx process
shell: killall -0 nginx > /tmp/nginx.log
二、tags
1.通過指定標(biāo)簽來執(zhí)行特定的動(dòng)作
---
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd
tags: inshttpd
- name: copy conf file
copy: src=files/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart service
- name: start service
service: name=httpd state=started enabled=yes
tags: rshttpd
handlers:
- name: restart service
service: name=httpd state=restarted
2.停止httpd服務(wù)
[root@ansible145 ansible]# ansible websrvs -m service -a 'name=httpd state=stopped'
3.檢查是否成功停止
4.執(zhí)行啟動(dòng)服務(wù)的tags
[root@ansible145 ansible]# ansible-playbook -t rshttpd httpd.yml
5.重新檢查服務(wù)是否啟動(dòng)
6.也可以針對多個(gè)標(biāo)簽執(zhí)行腳本
# 卸載httpd
[root@ansible145 ansible]# ansible websrvs -m yum -a 'name=httpd state=absent'
# 檢查是否成功卸載
[root@ansible145 ansible]# ansible websrvs -m shell -a 'rpm -q httpd'
# 執(zhí)行多個(gè)tags
[root@ansible145 ansible]# ansible-playbook -t inshttpd,rshttpd httpd.yml
# 啟動(dòng)的服務(wù)為默認(rèn)的80端口
[root@ansible145 ansible]# ansible websrvs -m shell -a 'ss -ntl | grep :80'
192.168.22.141 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
192.168.22.142 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
7.多個(gè)動(dòng)作對應(yīng)一個(gè)標(biāo)簽tags
腳本:
---
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd
tags: httpd
- name: copy conf file
copy: src=files/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart service
- name: start service
service: name=httpd state=started enabled=yes
tags: httpd
handlers:
- name: restart service
service: name=httpd state=restarted
# 卸載httpd
[root@ansible145 ansible]# ansible websrvs -m yum -a 'name=httpd state=absent'
# 檢查是否成功卸載
[root@ansible145 ansible]# ansible websrvs -m shell -a 'rpm -q httpd'
# 執(zhí)行tags
[root@ansible145 ansible]# ansible-playbook -t httpd httpd.yml
# 啟動(dòng)的服務(wù)為默認(rèn)的80端口
[root@ansible145 ansible]# ansible websrvs -m shell -a 'ss -ntl | grep :80'
192.168.22.141 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
192.168.22.142 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
查看標(biāo)簽:
[root@ansible145 ansible]# ansible-playbook httpd.yml --list-tags
playbook: httpd.yml
play #1 (websrvs): websrvs TAGS: []
TASK TAGS: [httpd]
三、playbook中變量的使用
使用變量可以對不同的被控機(jī)進(jìn)行不同的操作。
ansible websrvs -m setup | grep ansible_fqdn
ansible websrvs -m setup | grep ansible_hostname
ansible websrvs -m setup -a 'filter=ansible_fqdn'
ansible websrvs -m setup -a 'filter=ansible_hostname'
ansible websrvs -m setup -a 'filter=ansible_*'
ansible websrvs -m setup -a 'filter=*address'
ansible websrvs -m setup -a 'filter=ansible_all_ipv4_addresses'
ansible websrvs -m setup -a 'filter=ansible_eth0'
1.定義1個(gè)變量
(1)編寫腳本
vim app.yml
---
- hosts: appsrvs
remote_user: root
tasks:
- name: install package
yum: name={{ pkname }}
- name: start service
service: name={{ pkname }} state=started enabled=yes
(2)啟動(dòng)腳本時(shí)對腳本中的變量進(jìn)行賦值
[root@ansible145 ansible]# ansible-playbook -e 'pkname=vsftpd' app.yml
(3)檢查程序是否成功安裝、服務(wù)是否成功開啟
2.定義2個(gè)變量
(1)編寫腳本
---
- hosts: appsrvs
remote_user: root
tasks:
- name: install package
yum: name={{ pkname1 }}
- name: install package
yum: name={{ pkname2 }}
(2)執(zhí)行腳本
[root@ansible145 ansible]# ansible-playbook -e 'pkname1=httpd pkname2=memcached' app.yml
(3)查看程序是否安裝成功
(4)卸載程序
[root@ansible145 ansible]# ansible appsrvs -m shell -a 'yum -y remove httpd memcached vsftpd'
3.在腳本內(nèi)部給變量賦值
上面兩個(gè)腳本都是在命令行對腳本中的變量進(jìn)行賦值的,我們也可以在腳本內(nèi)部給變量賦值。
(1)編寫腳本
---
- hosts: appsrvs
remote_user: root
vars:
- pkname1: httpd
- pkname2: vsftpd
tasks:
- name: install package
yum: name={{ pkname1 }}
- name: install package
yum: name={{ pkname2 }}
(2)執(zhí)行腳本
[root@ansible145 ansible]# ansible-playbook app.yml
(3)檢查程序是否成功安裝
(4)卸載程序
[root@ansible145 ansible]# ansible appsrvs -m shell -a 'yum -y remove httpd vsftpd'
4.在主機(jī)清單中給腳本定義變量
[root@ansible145 ansible]# vim /etc/ansible/hosts
vim hostname.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: set hostname
hostname: name=www{{ http_port }}.test.com
執(zhí)行腳本
# 檢查語法
[root@ansible145 ansible]# ansible-playbook -C hostname.yml
# 執(zhí)行腳本
[root@ansible145 ansible]# ansible-playbook hostname.yml
# 查看websrvs主機(jī)名是否成功更改
[root@ansible145 ansible]# ansible websrvs -a 'hostname'
192.168.22.141 | CHANGED | rc=0 >>
www81.test.com
192.168.22.142 | CHANGED | rc=0 >>
www82.test.com
上面的修改主機(jī)名的腳本中,www{}test.com部分是公用的,也可以對其進(jìn)行修改
修改/etc/ansible/hosts文件
修改腳本
vim hostname.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: set hostname
hostname: name={{ nodename }}{{ http_port }}.{{ domainname }}
執(zhí)行腳本后查看主機(jī)名,已經(jīng)被成功修改
注意:如果在上述地方都設(shè)置后,在命令行對變量進(jìn)行重新賦值,則命令行的優(yōu)先級最高
[root@ansible145 ansible]# ansible-playbook -e 'nodename=web domainname=example.com' hostname.yml
普通變量的優(yōu)先級比分組的公共變量的優(yōu)先級高文章來源:http://www.zghlxwxcb.cn/news/detail-590842.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-590842.html
到了這里,關(guān)于自動(dòng)化運(yùn)維工具——Ansible學(xué)習(xí)(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!