我创建了一个简单的 Ansible 剧本:
---
- hosts: all
tasks:
- name: Install Icinga2 on Windows
include_role:
name: my.icinga2.role
apply:
tags:
- install-icinga2
该角色包含此任务文件:
---
- include_tasks: vars.yml
tags: ['always']
- include_tasks: install.yml
tags: ['install-icinga2-stack', 'install-icinga2']
- include_tasks: ido-install.yml
when: icinga2_ido_enable == true
tags: ['install-icinga2-stack', 'install-icinga2-ido']
- include_tasks: configure.yml
tags: ['install-icinga2-stack']
[...]
这是我执行剧本的结果:
me@ansible:~/ansible$ ansible-playbook plays/icinga2-client-win.yml -i staging.ini --limit windows
PLAY [all] ***************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [my.windows.client]
TASK [Include variables for Icinga 2] ********************************************************************************************
ok: [my.windows.client]
TASK [set_fact] ******************************************************************************************************************
skipping: [my.windows.client]
TASK [set_fact] ******************************************************************************************************************
ok: [my.windows.client]
TASK [Install Icinga2 Client and connect it to the master server] ****************************************************************
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/vars.yml for my.windows.client
TASK [my.icinga2.role : Set default fact for mysql command] ******************************************************************
ok: [my.windows.client]
TASK [my.icinga2.role : Set fact for mysql command if auth params are given] *************************************************
skipping: [my.windows.client]
TASK [my.icinga2.role : Set Monitoring Plugins for old Debian Versions] ******************************************************
skipping: [my.windows.client]
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/install.yml for my.windows.client
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
skipping: [my.windows.client]
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
skipping: [my.windows.client]
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/install-Windows.yml for my.windows.client
TASK [my.icinga2.role : set_fact] ********************************************************************************************
ok: [my.windows.client]
TASK [my.icinga2.role : set_fact] ********************************************************************************************
skipping: [my.windows.client]
TASK [my.icinga2.role : Install Icinga 2] ************************************************************************************
changed: [my.windows.client]
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
skipping: [my.windows.client]
TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/configure.yml for my.windows.client
TASK [my.icinga2.role : Check if Icinga 2 API are already activated] *********************************************************
[ This should not be included! ]
RUNNING HANDLER [my.icinga2.role : Restart Icinga2 on Windows] ***************************************************************
to retry, use: --limit @/home/me/ansible/plays/icinga2-client-win.retry
PLAY RECAP ***********************************************************************************************************************
my.windows.client : ok=10 changed=1 unreachable=0 failed=1
为什么配置.yml包含角色任务文件,因为只有当我应用安装-icinga2-stack标签,我正在应用安装-icinga2一?
此外,我意识到ido-安装.yml角色任务文件不包括在内,只是因为icinga2_ido_enable变量不在true
此剧本中(其默认值为false
),而不是因为它的某个标签未应用(这应该是我想要的)。
我错在哪里了?
答案1
标签的应用包括角色意味着标签
将应用于包含内的任务。
换句话说,所包含角色中的任务将继承所应用的标签。期望所应用的标签将选择任务是一种误解。要选择任务,请在命令行中使用--tags
和,或在 Ansible 配置设置中使用和选项。--skip-tags
TAGS_RUN
TAGS_SKIP
一个重要的事实在文献中没有明确提及包括角色. 参数申请标签仅当整个任务tags: always
是例子。
- name: Apply tags to tasks within included file
include_role:
name: install
apply:
tags:
- install
tags:
- always
例子
让角色 1 执行 2 个任务
shell> cat roles/role1/tasks/main.yml
- debug:
msg: 'This is task 2'
tags: task2
- debug:
msg: 'This is task 3'
tags: task3
和剧本
shell> cat play1.yml
- hosts: localhost
tasks:
- debug:
msg: 'This is task 1'
tags: task1
- include_role:
name: role1
apply:
tags: role1
tags: always
如果我们运行没有任何选项的剧本,则所有任务都包括
shell> ansible-playbook play1.yml | grep msg
"msg": "This is task 1"
"msg": "This is task 2"
"msg": "This is task 3"
请参阅下面的其他变体
shell> ansible-playbook play1.yml --tags task1 | grep msg
"msg": "This is task 1"
shell> ansible-playbook play1.yml --tags task2 | grep msg
"msg": "This is task 2"
shell> ansible-playbook play1.yml --tags role1 | grep msg
"msg": "This is task 2"
"msg": "This is task 3"
shell> ansible-playbook play1.yml --skip-tags role1 | grep msg
"msg": "This is task 1"
shell> ansible-playbook play1.yml --tags role1 --skip-tags task2 | grep msg
"msg": "This is task 3"
笔记。标签列表未按预期工作。
shell> ansible-playbook play1.yml --list-tags
playbook: play1.yml
play #1 (localhost): localhost TAGS: []
TASK TAGS: [always, task1]
答案2
如果我们include_tasks
在角色中使用 --> 那么我们在标签方面就会遇到麻烦。
如果我们import_tasks
在角色中使用 --> 一切都很好
例子:
$ ansible --version
ansible [core 2.11.7]
剧本:管理员.yml
- hosts: localhost
roles:
- { role: admins}
角色: ./roles/admins/tasks/主目录
---
- name: ' create | Debian '
ansible.builtin.include_tasks: "Debian/create.yml"
/角色/管理员/任务/Debian/创建.yml
---
- name: tag2
debug:
msg: "tag2"
tags: tag2
- name: tag3
debug:
msg: "tag3"
tags: tag3
让我们列出标签:
$ ansible-playbook admins.yml --list-tags
playbook: admins.yml
play #1 (localhost): localhost TAGS: []
TASK TAGS: []
所以我们在标签方面遇到了麻烦。让我们测试一下。
$ ansible-playbook admins.yml -t "tag3"
PLAY [localhost]
TASK [Gathering Facts]
ok: [localhost]
因此零任务完成。
让我们改变主目录
---
- name: ' create | Debian '
ansible.builtin.import_tasks: "Debian/create.yml"
现在我们不再有标签方面的困扰:
$ ansible-playbook admins.yml --list-tags
playbook: admins.yml
play #1 (localhost): localhost TAGS: []
TASK TAGS: [tag2, tag3]
让我们测试一下。
$ ansible-playbook admins.yml -t "tag3"
PLAY [localhost]
TASK [Gathering Facts]
ok: [localhost]
TASK [admins : tag3] ok: [localhost] => {
"msg": "tag3"
}