[Ansible系列]ansible tag介绍

目录

简介

task      tag 

1.  执行指定tag的task 

2.  排除指定tag的task

3.   查看playbook中的所有tag

4.  打tag的方式

4.1   一个task一个tag

4.2  一个task多个tag

4.3  给一个playbook打tag

ansible内置tag

1.  always

2.  never

3.  tagged

4.   untagged

5.   all

 总结


简介

         在大型项目当中,通常一个playbook会有非常多的task。而我们每次执行这个playbook时,都会将 所有task运行一遍。而事实上,在实际使用过程中,我们可能只是想要执行其中的一部分任务而已, 并不想把整个playbook完整跑一遍。这个时候就需要用到tags。(在playbook的调试过程中使用的非常频繁)

        通过tags,我们可以给playbook中的某一些任务打上“标签”,而在执行playbook的时候,我们可 以通过选定标签的方式指定只执行哪一些任务或者不执行哪一些任务。

task      tag 

         给task打上tag(标签),这章的内容不多,我们从一个示例开始:

示例1: 安装httpd,同时定义三个tags:install_httpd 、 conf_httpd 以及 start_httpd

- hosts: 192.168.194.129
  gather_facts: no

  tasks:
    - name: install http
      yum:
        name: httpd
        state: present
      tags: install_httpd

    - name: configuration httpd 
      template:
        src: /root/ansible_test/ansible_2/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: apache
        group: apache
        backup: yes
      notify:
        - restart httpd
      tags: config_httpd

    - name: start httpd
      service:
        name: httpd
        state: started
        enabled: yes
      tags: start_httpd

  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted

执行结果:

[root@clinet ansible_2]# ansible-playbook yum_file/tag/tag_test.yml 

PLAY [192.168.194.129] **********************************************************************************************************************************************************************************

TASK [install http] *************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]

TASK [configuration httpd] ******************************************************************************************************************************************************************************
changed: [192.168.194.129]

TASK [start httpd] **************************************************************************************************************************************************************************************
changed: [192.168.194.129]

RUNNING HANDLER [restart httpd] *************************************************************************************************************************************************************************
changed: [192.168.194.129]

PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129            : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]# 
[root@clinet ansible_2]# 

1.  执行指定tag的task 

  运行一个tag:

[root@clinet ansible_2]# ansible-playbook --tags="start_httpd" yum_file/tag/tag_test.yml 

PLAY [192.168.194.129] **********************************************************************************************************************************************************************************

TASK [start httpd] **************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]

PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129            : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]#

运行多个tag:

[root@clinet ansible_2]# ansible-playbook --tags="install_httpd,start_httpd" yum_file/tag/tag_test.yml 

PLAY [192.168.194.129] **********************************************************************************************************************************************************************************

TASK [install http] *************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]

TASK [start httpd] **************************************************************************************************************************************************************************************
ok: [192.168.194.129]

PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]#

2.  排除指定tag的task

         排除指定了tag的task,即除了指定tag的task不执行,其他task都执行

[root@clinet ansible_2]# ansible-playbook --skip-tags="install_httpd" yum_file/tag/tag_test.yml 

PLAY [192.168.194.129] **********************************************************************************************************************************************************************************

TASK [configuration httpd] ******************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]

TASK [start httpd] **************************************************************************************************************************************************************************************
ok: [192.168.194.129]

PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]# 

3.   查看playbook中的所有tag

        通过 ‐‐list‐tags 参数列出指定的playbook中所有的tag 

[root@clinet ansible_2]# ansible-playbook --list-tags yum_file/tag/tag_test.yml 

playbook: yum_file/tag/tag_test.yml

  play #1 (192.168.194.129): 192.168.194.129    TAGS: []
      TASK TAGS: [config_httpd, install_httpd, start_httpd]
[root@clinet ansible_2]# 
[root@clinet ansible_2]# 

4.  打tag的方式

4.1   一个task一个tag

  tasks:
    - name: install http
      yum:
        name: httpd
        state: present
      tags: install_httpd

4.2  一个task多个tag

方式一:

  tasks:
    - name: install http
      yum:
        name: httpd
        state: present
      tags:

        - install_httpd1

        - install_httpd2

方式二:

      tags: ['install_httpd1', 'install_httpd2']


方式三:

tags: install_httpd1,install_httpd2

4.3  给一个playbook打tag

         当为一个play指定一组标签后,该play下的所有task都会自动继承该标签,各task也可以自定义自己 的标签。

‐ name: configure webservers
  hosts: all
  remote_user: ansible
  tags:
    ‐ httpd

  tasks:
    ...

ansible内置tag

1.  always

         一旦某个task被打上了always的tag,则无论是playbook的完整执行,还是指定tag执 行,不管你指定的tag是啥,该任务总是会被执行。除非明确指定"--skip-tags=always"选项, 才不会执行该task。

示例1: 配置always标签,执行playbook,指定非always标签执行,查看always标签是否执行

- hosts: 192.168.194.129
  gather_facts: no

  tasks:
    - name: install http
      yum:
        name: httpd
        state: present
      tags: always

    - name: configuration httpd 
      template:
        src: /root/ansible_test/ansible_2/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: apache
        group: apache
        backup: yes
      notify:
        - restart httpd
      tags: config_httpd

    - name: start httpd
      service:
        name: httpd
        state: started
        enabled: yes
      tags: start_httpd

  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted

 示例2: 需要通过--skip-tags=always来取消always标签的始终执行

2.  never

        该标签与always正好相反,总是不会执行,除非明确指定"--tags=never"选项。 

3.  tagged

 # 所有打了tag的任务都会被执行,包含never tag的除外,没有标签的不会被执行
 

ansible‐playbook ‐‐tags tagged    yum_file/tag/tag_test.yml


# 所有打了tag的任务都不会被执行,包括always tag也不会被执行


ansible‐playbook ‐‐skip‐tags tagged    yum_file/tag/tag_test.yml

4.   untagged

 # 所有未打tag的任务都会被执行,打了always tag的也会被执行

ansibl‐playbook ‐‐tags untagged  yum_file/tag/tag_test.yml

# 所有未打tag的任务都不会被执行

ansibl‐playbook ‐‐skip‐tags untagged   yum_file/tag/tag_test.yml

5.   all

 表示所有任务都会被执行,在默认情况下,不指定任何标签,则使用的就是该标签


 总结

   task的标签功能目前用的最多就是在playbook的调试中,当执行playbook出现某个task有问题的时候,我们就会给该task打标签,用于后续修改测试,这也有效的避免了多次从头执行playbook的时间浪费。大家还有其他什么用法,欢迎指导!

猜你喜欢

转载自blog.csdn.net/qq_43714097/article/details/128785038