tags标签 
1. 概述 
- 如果你有一个超大的playbook剧本文件,有时并不想运行剧本文件中所有的任务,这个时候tags标签可以解决这个问题,通过tags标签可以运行或者忽略(execute or skip)剧本中的部分任务。
- 你需要给任务添加标签,然后在运行剧本时选项相应的标签。
- 详细可参考官方文档 Tags
2. 官方示例 
- 你可以给任务添加一个或多个标签。
- 你可以在剧本中添加标签,也可以在task任务文件,或者role角色中添加标签。
以下示例是不同任务设置不同的标签:
tasks:
- name: Install the servers
  ansible.builtin.yum:
    name:
    - httpd
    - memcached
    state: present
  tags:
  - packages
  - webservers
- name: Configure the service
  ansible.builtin.template:
    src: templates/src.j2
    dest: /etc/foo.conf
  tags:
  - configuration2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 你也可以给不同的任务设置相同的标签信息。
以下示例中,有3个任务被设置成了相同的标签ntp:
---
# file: roles/common/tasks/main.yml
- name: Install ntp
  ansible.builtin.yum:
    name: ntp
    state: present
  tags: ntp
- name: Configure ntp
  ansible.builtin.template:
    src: ntp.conf.j2
    dest: /etc/ntp.conf
  notify:
  - restart ntpd
  tags: ntp
- name: Enable and run ntpd
  ansible.builtin.service:
    name: ntpd
    state: started
    enabled: true
  tags: ntp
- name: Install NFS utils
  ansible.builtin.yum:
    name:
    - nfs-utils
    - nfs-util-lib
    state: present
  tags: filesharing2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
如果你在运行剧本时使用--tags ntp参数,Ansible只会运行标签为ntp的三个任务,第4个任务Install NFS utils没有ntp标签,将会被忽略,不会执行。
- 你也可以为多个任务组成的块设置标签:
# myrole/tasks/main.yml
- name: ntp tasks
  tags: ntp
  block:
  - name: Install ntp
    ansible.builtin.yum:
      name: ntp
      state: present
  - name: Configure ntp
    ansible.builtin.template:
      src: ntp.conf.j2
      dest: /etc/ntp.conf
    notify:
    - restart ntpd
  - name: Enable and run ntpd
    ansible.builtin.service:
      name: ntpd
      state: started
      enabled: true
- name: Install NFS utils
  ansible.builtin.yum:
    name:
    - nfs-utils
    - nfs-util-lib
    state: present
  tags: filesharing2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
以上示例中,在剧本顶层使用block定义一个块,并且使用tags: ntp来定义块标签为ntp。
- 也可以为整个剧本设置一个标签,可参考官方示例。
- 其他关于角色中配置标签的示例,可参考官方文档。
3. 使用剧本 
请看以下剧本示例,定义三个任务,每个任务设置一个标签:
- hosts: node1
  tasks:
    - name: The first task
      ansible.builtin.debug:
        msg: "executed the tag1 task"
      tags: tag1
    - name: The second task
      ansible.builtin.debug:
        msg: "executed the tag2 task"
      tags: tag2
    - name: The third task
      ansible.builtin.debug:
        msg: "executed the tag3 task"
      tags: tag3
    - name: The fourth task
      ansible.builtin.debug:
        msg: "executed the last task that no tag"2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
查看标签相关的命令行帮助信息:
[ansible@master ~]$ ansible-playbook --help|grep tags
                        [--skip-tags SKIP_TAGS] [-C] [--syntax-check] [-D]
                        [--list-tags] [--step] [--start-at-task START_AT_TASK]
  --list-tags           list all available tags
  --skip-tags SKIP_TAGS
                        only run plays and tasks whose tags do not match these
  -t TAGS, --tags TAGS  only run plays and tasks tagged with these values
[ansible@master ~]$2
3
4
5
6
7
8
可以看到:
- --list-tags会列出所有有效的标签。
- --skip-tags仅运行不匹配指定标签的任务。
- -t或- --tags仅运行匹配指定标签的任务。
3.1 列出所有标签 
使用--list-tags可以列出所有的标签。
[ansible@master ansible_playbooks]$ ansible-playbook --list-tags tags.yml
playbook: tags.yml
  play #1 (node1): node1	TAGS: []
      TASK TAGS: [tag1, tag2, tag3]
[ansible@master ansible_playbooks]$2
3
4
5
6
7
此时,可以看到,任务一共有三个标签,tag1、tag2和tag3。
在列表标签时,也可以使用--list-tasks列出标签对应的任务名称:
[ansible@master ansible_playbooks]$ ansible-playbook --list-tags --list-tasks tags.yml
playbook: tags.yml
  play #1 (node1): node1	TAGS: []
    tasks:
      The first task	   TAGS: [tag1]
      The second task	TAGS: [tag2]
      The third task	   TAGS: [tag3]
      TASK TAGS: [tag1, tag2, tag3]2
3
4
5
6
7
8
9
10
可以看到,标签对应的任务名称也显示了出来!
3.2 执行单个标签对应的任务 
- -t或- --tags仅运行匹配指定标签的任务。
我们来尝试运行指定标签对应的任务。
# 执行tag1标签对应的任务
[ansible@master ansible_playbooks]$ ansible-playbook --tags=tag1 tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The first task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag1 task"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$
# 执行tag2标签对应的任务
[ansible@master ansible_playbooks]$ ansible-playbook --tags=tag2 tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The second task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag2 task"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
# 执行tag3标签对应的任务
[ansible@master ansible_playbooks]$ ansible-playbook --tags=tag3 tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The third task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag3 task"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
3.3 执行多个标签对应的任务 
如执行tag1和tag2标签对应的任务:
[ansible@master ansible_playbooks]$ ansible-playbook --tags=tag1,tag2 tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The first task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag1 task"
}
TASK [The second task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag2 task"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$ ansible-playbook --tags tag1,tag2 tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The first task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag1 task"
}
TASK [The second task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag2 task"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
3.4 忽略某个标签对应的任务 
- --skip-tags仅运行不匹配指定标签的任务。也就是可以忽略某些标签对应的任务。
[ansible@master ansible_playbooks]$ ansible-playbook --skip-tags="tag1" tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The second task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag2 task"
}
TASK [The third task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag3 task"
}
TASK [The fourth task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the last task that no tag"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
忽略tag1对应的任务时,The first task任务没有被执行。The second task、The third task和The fourth task执行了,虽然第4个任务The fourth task没有标签名,但也被执行了,即--skip-tags只是忽略其匹配的任务,其他任务都是会执行的。
3.5 忽略多个标签对应的任务 
也可以一次性忽略多个标签:
[ansible@master ansible_playbooks]$ ansible-playbook --skip-tags="tag1,tag2,tag3" tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The fourth task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the last task that no tag"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
可以看到,此时仅The fourth task第4个任务执行了,其他任务被忽略了!
3.6 执行有标签的任务 
可以使用--tags tagged来运行有标签的任务。
[ansible@master ansible_playbooks]$ ansible-playbook --tags=tagged tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The first task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag1 task"
}
TASK [The second task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag2 task"
}
TASK [The third task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag3 task"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
可以看到,有标签的The first task、The second task和The third task三个任务执行了,第4个任务The fourth task没有标签,没有执行!
3.7 执行没有标签的任务 
可以使用--tags untagged来运行没有标签的任务。
[ansible@master ansible_playbooks]$ ansible-playbook --tags=untagged tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The fourth task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the last task that no tag"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
可以看到,仅第4个任务The fourth task没有标签,执行了。其他三个任务有标签没有执行!
3.8 其他特殊标签 
- 当使用--tags all时,表示运行所有所有任务,忽略标签。这是Ansible默认的行为。
- always标签,是一直会执行的标签。
- never标签,是一直不会执行的标签!
[ansible@master ansible_playbooks]$ ansible-playbook --tags=all tags.yml
PLAY [node1] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [node1]
TASK [The first task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag1 task"
}
TASK [The second task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag2 task"
}
TASK [The third task] **************************************************************************************************
ok: [node1] => {
    "msg": "executed the tag3 task"
}
TASK [The fourth task] *************************************************************************************************
ok: [node1] => {
    "msg": "executed the last task that no tag"
}
PLAY RECAP *************************************************************************************************************
node1                      : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[ansible@master ansible_playbooks]$2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
可以看到,所有任务都执行了!与没有指定--tag all的效果一样!