Ansible觸發器-tag標籤-忽略錯誤
觸發器
playbook handlers
handler`用來執行某些條件下的任務,比如當配置檔案發生變化的時候,通過notify觸發handler去重啟服務。
在saltstack中也有類似的觸發器,寫法相對Ansible簡單,只需要watch,配置檔案即可。
大白話:監控某一個步驟,一旦該步驟發生了變化,則立馬觸發該步驟的觸發器,執行對應的步驟
注意: 1.無論多少個task通知了相同的handlers,handlers僅會在所有tasks結束後執行一次。 2.Handlers只有在其所在的任務被執行時,才會被執行;如果一個任務中定義了notify呼叫Handlers,但是由於條件判斷等原因,該任務未被執行,那麼Handlers同樣不會被執行。 3.Handlers只會在每一個play的末尾執行一次;如果想在一個playbook中間執行Handlers,則需要使用meta模組來實現。例如: -meta: flush_handlers。(不要強制執行) 4.如果一個play在執行到呼叫Handlers的語句之前失敗了,那麼這個Handlers將不會被執行。我們可以使用meta模組的--force-handlers選項來強制執行Handlers,即使Handlers所在的play中途執行失敗也能執行。(不要強制執行) 5.不能使用handlers替代tasks
觸發器的寫法
- hosts: web01 task: - name: Push Nginx PHP Conf copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" } - { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" } - { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" } when: ansible_fqdn is match 'web*' notify: Restart Nginx And PHP # 通過名字的方式來關聯觸發器,當配置檔案有變化的時候,就會引發觸發器 handlers: - name: Restart Nginx And PHP service: name: "{{ item }}" state: restarted with_items: - nginx - php-fpm 注意:tasks中的notify名字必須和handlers中的- name名字對應上,否則觸發器和任務沒有做任何關聯
tag標籤
預設情況下,Ansible在執行一個playbook時,會執行playbook中定義的所有任務,Ansible的標籤(tag)功能可以給單獨任務甚至整個playbook打上標籤,然後利用這些標籤來指定要執行playbook中的個別任務,或不執行指定的任務。
打標籤的方式
1.對一個task打一個標籤
我只想推送nginx的配置檔案
- name: Push Nginx PHP Conf copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" } - { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" } - { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" } when: ansible_fqdn is match 'web*' notify: Restart Nginx And PHP tags: config_nginx ## 執行: [root@m01 ansible]# ansible-playbook lnmp.yml -t config_nginx -t:執行指定的tag --skip-tags:跳過指定的tag
2.對一個task打多個標籤
有一個功能任務,我安裝nginx的時候需要建立www使用者,安裝nfs的時候,需要建立www使用者,安裝rsync的時候需要建立www使用者
建立www使用者這個功能,有多個任務都需要使用
tag: install_nginx
tag: install_nfs
tag: install_rsync
- name: Create {{ web_user_group }} Group
group:
name: "{{ web_user_group }}"
gid: 666
state: present
tags:
- install_nginx
- install_nfs
- install_rsync
[root@m01 ansible]# ansible-playbook lnmp.yml -t install_nginx
- name: Push Nginx PHP Conf
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" }
- { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" }
- { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" }
when: ansible_fqdn is match 'web*'
notify: Restart Nginx And PHP
tags:
- congfig_nginx
- install_nginx
比如在只執行部署程式碼的步驟,需要建立www使用者,只執行部署rsync的時候也需要建立www使用者,這個時候就需要把建立www使用者這個tasks加上多個標籤。
3.對多個task打一個標籤
我只想重新安裝nginx
1.安裝nginx
tag: install_nginx
2.配置nginx打一個標籤
tag: install_nginx
- name: Unarchive Nginx and PHP
unarchive:
src: /ansible/web/nginx_php.tgz
dest: /root
when: ansible_fqdn is match 'web*'
tags: install_nginx
- name: Install Nginx and PHP
yum:
name: /root/nginx_php/{{ item }}
state: present
with_items: "{{ nginx_php_packages }}"
when: ansible_fqdn is match 'web*'
tags: install_nginx
- name: Push Nginx PHP Conf
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" }
- { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" }
- { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" }
when: ansible_fqdn is match 'web*'
notify: Restart Nginx And PHP
tags:
- congfig_nginx
- install_nginx
- name: Create HTML Dir
file:
path: /code
owner: "{{ web_user_group }}"
group: "{{ web_user_group }}"
state: directory
when: ansible_fqdn is match 'web*'
tags: install_nginx
- name: Unarchive WordPress Package
unarchive:
src: /ansible/web/wordpress.tgz
dest: /code
owner: "{{ web_user_group }}"
group: "{{ web_user_group }}"
when: ansible_fqdn is match 'web*'
tags: install_nginx
- name: Start Nginx Server
service:
name: "{{ item }}"
state: started
enabled: true
with_items:
- nginx
- php-fpm
when: ansible_fqdn is match 'web*'
tags: install_nginx
- name: Mount NFS Share Directory
mount:
path: /code/wordpress/wp-content/uploads
src: 172.16.1.31:/{{ nfs_dir }}
fstype: nfs
state: mounted
when: ansible_fqdn is match 'web*'
tags: install_nginx
## 執行:
[root@m01 ansible]# ansible-playbook lnmp.yml -t install_nginx
-t:執行指定的tag
--skip-tags:跳過指定的tag
比如我只想執行安裝nginx的步驟,需要建立www使用者、建立站點目錄、加入防火牆規則等,把這些步驟打上統一標籤,然後呼叫執行這樣就可以只執行指定的相應的步驟及就可以了。
playbook的複用
只調用task:include_tasks
呼叫整個task檔案:include
(新版本:import_playbook)
在saltstack中,叫做top file
入口檔案。
示例一:
[root@m01 m01]# cat task.yml
- hosts: web_group
vars:
- http_port: 8080
tasks:
- include_tasks: task_install.yml
- include_tasks: task_configure.yml
- include_tasks: task_start.yml
handlers:
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
[root@m01 m01]# cat task_install.yml
- name: Install Http Server
yum:
name: httpd
state: present
[root@m01 m01]# cat task_configure.yml
- name: configure httpd server
template:
src: ./httpd.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
[root@m01 m01]# cat task_start.yml
- name: start httpd server
service:
name: httpd
state: started
enabled: yes
示例二
- include: httpd.yml
- include: nfs.yml
- include: rsync.yml
示例三
- import_playbook: httpd.yml
- import_playbook: nfs.yml
- import_playbook: rsync.yml
忽略錯誤
預設playbook會檢測task執行的返回狀態,如果遇到錯誤則會立即終止playbook的後續task執行,然而有些時候playbook即使執行錯誤了也要讓其繼續執行。
加入引數:ignore_errors:yes 忽略錯誤
[root@m01 ~]# cat ignore.yml
- hosts: web_group
tasks:
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file:
path: /tmp/zls.txt
state: touch