示例:httpd.yml
– hosts: websrvs
remote_user: root
tasks:
– name: Install httpd
yum: name=httpd state=present
– name: Install configure file
copy: src=files/httpd.conf dest=/etc/httpd/conf/
tags: conf
– name: start httpd service
tags: service
service: name=httpd state=started enabled=yes
ansible-playbook –t conf httpd.yml
如果想單獨的執行某一個動作,例如只是復制配置文件但是不重啟服務
tags就是給任務打個標簽,將來只運行標簽就可以,不要加空格
長格式 -tags 標簽名
短格式 -t 標簽名
[root@ansible ~]# vim httpd.conf
#Listen 12.34.56.78:80
Listen 82
修改httpd.yml
[root@ansible ~/ansible]# cat httpd.yml
—
– hosts: db
remote_user: root
tasks:
– name: install httpd
yum: name=httpd
– name: copy config file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart httpd
tags: copyconf
– name: start httpd
service: name=httpd state=started enabled=yes
handlers:
– name: restart httpd
service: name=httpd state=restarted
[root@ansible ~/ansible]#
測試
[root@ansible ~/ansible]# ansible-playbook -t copyfile httpd.yml -C
[root@ansible ~/ansible]# ansible-playbook –tags copyconf -C httpd.yml
PLAY [db] **********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.18.103.28]
ok: [172.18.103.29]
TASK [copy config file] ********************************************************
changed: [172.18.103.29]
ok: [172.18.103.28]
RUNNING HANDLER [restart httpd] ************************************************
changed: [172.18.103.29]
PLAY RECAP *********************************************************************
172.18.103.28 : ok=2 changed=0 unreachable=0 failed=0
172.18.103.29 : ok=3 changed=2 unreachable=0 failed=0
[root@ansible ~/ansible]#
執行
[root@ansible ~/ansible]# ansible-playbook –tags copyconf httpd.yml
PLAY RECAP *********************************************************************
172.18.103.28 : ok=2 changed=0 unreachable=0 failed=0
172.18.103.29 : ok=3 changed=2 unreachable=0 failed=0
查看執行結果
[root@ansible ~/ansible]# ansible 172.18.103.29 -m shell -a “cat /etc/httpd/conf/httpd.conf | grep ‘Listen 8′”
172.18.103.29 | SUCCESS | rc=0 >>
Listen 82
為方便使用可以添加多個標簽,支持下劃線
[root@ansible ~/ansible]# cat httpd.yml
—
– hosts: db
remote_user: root
tasks:
– name: install httpd
yum: name=httpd
tags: install
– name: copy config file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/ backup=yes
tags: copyconf
notify: restart httpd
– name: start httpd
tags: start_httpd
service: name=httpd state=started enabled=yes
handlers:
– name: restart httpd
service: name=httpd state=restarted
[root@ansible ~/ansible]#
停止服務,使用tags測試
[root@ansible ~/ansible]# ansible db -m shell -a “systemctl stop httpd”
172.18.103.28 | SUCCESS | rc=0 >>
172.18.103.29 | SUCCESS | rc=0 >>
測試
[root@ansible ~/ansible]# ansible-playbook -t start_httpd httpd.yml
PLAY [db] ************************************************************************
TASK [Gathering Facts] **********************************************************
ok: [172.18.103.29]
ok: [172.18.103.28]
TASK [start httpd] ***************************************************************
ok: [172.18.103.29]
changed: [172.18.103.28]
PLAY RECAP *********************************************************************
172.18.103.28 : ok=2 changed=1 unreachable=0 failed=0
172.18.103.29 : ok=2 changed=0 unreachable=0 failed=0
[root@ansible ~/ansible]#
查看端口
[root@ansible ~/ansible]# ansible db -m shell -a “ss -ntl | grep 82”
172.18.103.29 | SUCCESS | rc=0 >>
LISTEN 0 128 :::82 :::*
172.18.103.28 | SUCCESS | rc=0 >>
LISTEN 0 128 :::82 :::*
停止服務
[root@ansible ~/ansible]# ansible db -m service -a ‘name=httpd state=stopped’
修改端口
[root@ansible ~]# vim httpd.conf
Listen 83
執行多個標簽,復制文件,啟動服務,順序不重要,在httpd.yml已經寫好了順序
[root@ansible ~/ansible]# ansible-playbook -t start_httpd,copyconf httpd.yml
查看端口
[root@ansible ~/ansible]# ansible db -m shell -a “ss -ntl | grep 83”
172.18.103.29 | SUCCESS | rc=0 >>
LISTEN 0 128 :::83 :::*
172.18.103.28 | SUCCESS | rc=0 >>
LISTEN 0 128 :::83 :::*
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/91106