Eu criei um playbook simples do Ansible:
---
- hosts: all
tasks:
- name: Install Icinga2 on Windows
include_role:
name: my.icinga2.role
apply:
tags:
- install-icinga2
A função contém este arquivo de tarefas:
---
- 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']
[...]
Este é o resultado quando executo o playbook:
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
Por que o arquivo de tarefa de função configure.yml está incluído, pois deve ser incluído somente se eu aplicar a tag install-icinga2-stack e estiver aplicando a tag install-icinga2 ?
Além disso, percebo que o arquivo de tarefa de função ido-install.yml não está incluído apenas porque a variável icinga2_ido_enabletrue
não está neste playbook (e seu padrão é false
), não porque uma de suas tags não é aplicada (que deve ser o que eu quero) .
Onde estou errado?
A aplicação de tags em include_role significa que as tags
Em outras palavras, as tarefas na função incluída herdarão as tags aplicadas. É um mal-entendido esperar que as tags aplicadas selecionem as tarefas. Para selecionar as tarefas, use
--tags
e--skip-tags
na linha de comando ou nas definições de configuração do Ansible, use as opçõesTAGS_RUN
eTAGS_SKIP
.Um fato importante não é mencionado explicitamente na documentação de include_role . O parâmetro apply tags funciona apenas se toda a tarefa for
tags: always
. Isso é mostrado apenas nos exemplos .Exemplo
Vamos ter role1 com as 2 tarefas
e uma cartilha
Se executarmos o playbook sem nenhuma opção, todas as tarefas serão incluídas
Veja outras variações abaixo
Nota . A listagem de tags não funciona como esperado.
(Para o registro: include_role com as tags de aplicação não funciona #52063 )