标签的作用
1 |
默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务,Ansible的标签(tag)功能可以给单独任务甚至整个playbook打上标签,然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务。 |
打标签的方式
1 2 3 |
1.对一个task下面的一个name打一个标签 2.对一个task下面的一个name打多个标签 3.对task下面的多个name打一个标签 |
打标签
1)对一个task打一个标签
1 2 3 4 5 6 7 8 9 10 11 |
.. ... ... - name: Config nginx Server copy: src: /root/conf/linux.wp.com.conf dest: /etc/nginx/conf.d/ notify: - restart_web_nginx - get_nginx_status when: ansible_fqdn is match "web*" tags: config_web ... ... ... |
2)对一个task下面的一个name打多个标签
1 2 3 4 5 6 7 8 9 10 11 |
- name: Config nginx Server copy: src: /root/conf/linux.wp.com.conf dest: /etc/nginx/conf.d/ notify: - restart_web_nginx - get_nginx_status when: ansible_fqdn is match "web*" tags: - config_web - config_nginx |
3)对task下面的多个name打一个标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- name: Config slb Server copy: src: /root/conf/proxy.conf dest: /etc/nginx/conf.d notify: restart_slb when: ansible_fqdn == "lb01" tags: config_nginx - name: Config nginx Server copy: src: /root/conf/linux.wp.com.conf dest: /etc/nginx/conf.d/ notify: - restart_web_nginx - get_nginx_status when: ansible_fqdn is match "web*" tags: - config_web - config_nginx |
标签的使用
1 2 3 4 5 6 7 8 9 10 11 |
#查看所有的标签(也可以查看该剧本有多少 hosts ) [root@m01 ~]# ansible-playbook lnmp6.yml --list-tags #执行标签指定的动作 [root@m01 ~]# ansible-playbook lnmp2.yml -t config_web #执行指定多个标签的动作 [root@m01 ~]# ansible-playbook lnmp2.yml -t config_nginx,config_web #跳过指定的标签动作 [root@m01 ~]# ansible-playbook lnmp2.yml --skip-tags config_nginx |