四、playbook任务标签
1.标签的作用
默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务,Ansible的标签(tag)功能可以给单独任务甚至整个playbook打上标签,
然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务。
2.打标签的方式
1.对一个task打一个标签 2.对一个task打多个标签 3.对多个task打一个标签
3.标签配置
#对一个task打一个标签 - name: Config Nginx Server copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/root/nginx.conf", dest: "/etc/nginx/" } - { src: "/root/wordpress.conf", dest: "/etc/nginx/conf.d" } notify: restart_nginx #触发器判断上面的文件是否发生改变 tags: reload_nginx #对一个task打多个标签 - name: Config Nginx Server copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/root/nginx.conf", dest: "/etc/nginx/" } - { src: "/root/wordpress.conf", dest: "/etc/nginx/conf.d" } notify: restart_nginx #触发器判断上面的文件是否发生改变 tags: - reload_nginx - reconfig_nginx #对多个name打一个标签 - name: Config Nginx Server copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/root/nginx.conf", dest: "/etc/nginx/" } - { src: "/root/wordpress.conf", dest: "/etc/nginx/conf.d" } notify: restart_nginx #触发器判断上面的文件是否发生改变 tags: - reload_nginx - reconfig_nginx - name: Tar PHP and Wordpress Package unarchive: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/root/php.tar.gz", dest: "/tmp/" } - { src: "/root/blog.tar.gz", dest: "/" } notify: restart_nginx tags: reload_nginx
4.标签使用方式
#查看所有的标签 [root@m01 web]# ansible-playbook lnmp.yml --list-tags playbook: lnmp.yml play #1 (all): all TAGS: [] TASK TAGS: [] play #2 (web01): web_01 TAGS: [] TASK TAGS: [reload_nginx] play #3 (db01): db01 TAGS: [] TASK TAGS: [] #指定标签执行name的内容 [root@m01 web]# ansible-playbook lnmp.yml -t reload_nginx #指定多个标签执行name的内容 [root@m01 web]# ansible-playbook lnmp.yml -t reload_nginx #指定多个标签执行name的内容 [root@m01 web]# ansible-playbook lnmp.yml -t reload_nginx,reconfig_nginx #排除标签执行其他name (跳过标签名字为chown_dir) [root@m01 web]# ansible-playbook lnmp.yml --skip-tags chown_dir
五、playbook复用
在之前写playbook的过程中,我们发现,写多个playbook没有办法一键执行,这样我们还要单个playbook挨个去执行,很鸡肋。
所以在playbook中有一个功能,叫做include用来动态调用task任务列表。
1.只调用task内容
[root@m01 web]# vim install_nginx.yml - name: Install Nginx Server yum: name: nginx state: present [root@m01 web]# vim config_nginx.yml - name: Config Nginx copy: src: /root/wordpress.conf dest: /etc/nginx/conf.d/ notify: reload_nginx [root@m01 web]# vim main.yml - hosts: web01 tasks: # 从上到下执行 - include_tasks: ./install_nginx.yml - include_tasks: ./config_nginx.yml handlers: - name: reload_nginx systemed: name: nginx state: restarted #执行 [root@m01 web]# ansible-playbook main.yml
2.直接调用写好的yml文件
[root@m01 web]# vim main.yml - import_playbook: ./lnp.yml - import_playbook: ./mariadb.yml #执行 [root@m01 web]# ansible-playbook main.yml
六、playbook忽略错误
- name: Get PHP Install Status shell: "rpm -qa | grep php" ignore_errors: yes #获取状态,加上忽略错误(否则非0报错,不往下执行) register: get_php_install_status #调用变量结果,rc值不等于0时执行安装命令 - name: Install PHP Server shell: "yum localinstall -y /tmp/*.rpm" when: get_php_install_status.rc != 0 #rc就是$?返回
七、playbook 错误处理
当task执行失败时,playbook将不再继续执行,包括如果在task中设置了handler也不会被执行。 但是我们可以采取强制措施...
1.强制调用handlers
如果在主机上的执行的任务失败时,则此任务对应的处理程序不再运行 此时可以通过force_handlers: yes实现,即使notify对应的响应任务执行失败,仍继续执行noftify对应的处理程序(即使任务失败了也要调用notify触发handlers) force_handlers与tasks同层级
- name: httpd hosts: web force_handlers: yes tasks: - name: install httpd yum: name: httpd state: present notify: - start httpd handlers: - name: start httpd service: name: httpd state: started
2.一直change
#当被控端没有发生实际改变时,可以使用changed_when将状态强行变绿 - hosts: web01 tasks: - name: Get PHP Install Status shell: "rpm -qa | grep php" ignore_errors: yes register: get_php_install_status changed_when: false # 把状态黄色变为绿色
标签:name,dest,29,nginx,ansible,playbook,错误处理,root,yml From: https://www.cnblogs.com/ludingchao/p/18131849