Mat*_*Mat 2 ansible ansible-playbook
我创建了一个简单的 Ansible 剧本:
---
- hosts: all
tasks:
- name: Install Icinga2 on Windows
include_role:
name: my.icinga2.role
apply:
tags:
- install-icinga2
Run Code Online (Sandbox Code Playgroud)
该角色包含以下任务文件:
---
- 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']
[...]
Run Code Online (Sandbox Code Playgroud)
这是我执行剧本时的结果:
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
Run Code Online (Sandbox Code Playgroud)
为什么包含configure.yml角色任务文件,因为只有当我应用install-icinga2-stack标记并且我正在应用install-icinga2标记时才应该包含它?
此外,我意识到不包含ido-install.yml角色任务文件只是因为icinga2_ido_enable变量不在true
此剧本中(并且其默认值为false
),而不是因为未应用其标签之一(这应该是我想要的) .
我错在哪里?
标签在include_role 中的应用意味着标签
将应用于包含中的任务。
换句话说,包含角色中的任务将继承应用的标签。期望应用的标签会选择任务是一种误解。要在命令行或 Ansible 配置设置中选择使用--tags
和的任务--skip-tags
,请使用TAGS_RUN
和TAGS_SKIP
选项。
include_role的文档中没有明确提到一个重要的事实。参数apply tags 仅在整个任务为 时才起作用tags: always
。这仅在示例中显示。
- name: Apply tags to tasks within included file
include_role:
name: install
apply:
tags:
- install
tags:
- always
Run Code Online (Sandbox Code Playgroud)
例子
让我们有两个任务的角色1
shell> cat roles/role1/tasks/main.yml
- debug:
msg: 'This is task 2'
tags: task2
- debug:
msg: 'This is task 3'
tags: task3
Run Code Online (Sandbox Code Playgroud)
和剧本
shell> cat play1.yml
- hosts: localhost
tasks:
- debug:
msg: 'This is task 1'
tags: task1
- include_role:
name: role1
apply:
tags: role1
tags: always
Run Code Online (Sandbox Code Playgroud)
如果我们在没有任何选项的情况下运行剧本,则所有任务都包括在内
shell> ansible-playbook play1.yml | grep msg
"msg": "This is task 1"
"msg": "This is task 2"
"msg": "This is task 3"
Run Code Online (Sandbox Code Playgroud)
请参阅下面的其他变体
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"
Run Code Online (Sandbox Code Playgroud)
注意。标签列表未按预期工作。
shell> ansible-playbook play1.yml --list-tags
playbook: play1.yml
play #1 (localhost): localhost TAGS: []
TASK TAGS: [always, task1]
Run Code Online (Sandbox Code Playgroud)
(记录:include_role with apply 标签不起作用#52063)
归档时间: |
|
查看次数: |
5315 次 |
最近记录: |