include_tasks with tags, regression of feature?

942 views
Skip to first unread message

Mike Klebolt

unread,
Jan 19, 2018, 10:58:05 AM1/19/18
to Ansible Project
I'm trying to dynamically include tasks along with using tags.  The issue I'm running into is that yes, its including the tasks file, but its not executing any of the tasks within that file.  After reading the Creating Reusable Playbooks guide, it appears this is by design for include_tasks.  My question is why?  I'm now left having to add a tag to each individual task within the dynamically included tasks file.  This seems less than ideal.  Is there a better way I should be going about this?

Example:

playbook.yml
---
- name: Test
  hosts: localhost
  gather_facts: False
  connection: local
  tasks:
   - include_tasks: "{{ dynamic_variable }}.yml"
     tags: test


dynamic_variable.yml
---
- ping:



Playbook run without tag, tasks from dynamic_variable are executed
ansible-playbook playbook.yml
Vault password:

PLAY [Test] ***********************************************************************************************************************************************************************************************************

TASK [include_tasks] **************************************************************************************************************************************************************************************************
included: /etc/ansible/dynamic_variable.yml for localhost

TASK [ping] ***********************************************************************************************************************************************************************************************************
ok: [localhost]

PLAY RECAP ************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0



Playbook run with tag, dynamic_variable file is included, none of the tasks contained within are executed due to not having tag.

 ansible-playbook playbook.yml -t test
Vault password:

PLAY [Test] ***********************************************************************************************************************************************************************************************************

TASK [include_tasks] **************************************************************************************************************************************************************************************************
included: /etc/ansible/dynamic_variable.yml for localhost

PLAY RECAP ************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0



The reason this is a problem for me is because within one of my dynamic tasks files I have 20-30 tasks.   I'd prefer not to have to add a tag to each individual task as that is how ansible used to function with the now deprecated include module.  It appears that if I roll back to before commit ebf971f, my issues go away.


So back to my original question.   Is there a better way I should be going about this?  If so, how?

Mike Klebolt

unread,
Jan 19, 2018, 11:03:22 AM1/19/18
to Ansible Project
I've also tried wrapping all the tasks in the dynamically included file in a block and adding a tag to the block.  This achieves what I'm trying to accomplish but doesn't feel like the proper approach.

Matt Martz

unread,
Jan 19, 2018, 11:18:42 AM1/19/18
to ansible...@googlegroups.com
The explanation listed at https://github.com/ansible/ansible/issues/35065#issuecomment-358998670 should hopefully answer your questions.

If you want an attribute to be inherited on the tasks, you should use `import_tasks` as opposed to `include_tasks`.  Attributes on `include_tasks` should apply to the include only, whereas attributes on `import_tasks` will be inherited by the imported tasks.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6368451d-ed81-47f6-b2d9-8946cfca3844%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Mike Klebolt

unread,
Jan 19, 2018, 11:53:43 AM1/19/18
to Ansible Project
I won't be able to use 2.5 since import_tasks with a dynamically assigned variable with fail.  Is there a way around this?

---
- name: Test
  hosts: localhost
  gather_facts: False
  connection: local
  tasks:
   - set_fact:
      dynamic_variable: dynamic
   - import_tasks: "{{ dynamic_variable }}.yml"


ansible-playbook playbook.yml
Vault password:
ERROR! Error when evaluating variable in include name: {{ dynamic_variable }}.yml.

When using static includes, ensure that any variables used in their names are defined in vars/vars_files
or extra-vars passed in from the command line. Static includes cannot use variables from inventory
sources like group or host vars.





Matt Martz

unread,
Jan 19, 2018, 12:00:15 PM1/19/18
to ansible...@googlegroups.com
As you have discovered, you will need to use the `block` method if you want to "easily" apply a tag or set of tags to a group of tasks without tagging each one individually.

Attributes applied to a dynamic include (include_*) apply to the include and not to the tasks within.  This is the primary purpose of dynamic includes.  To allow the attributes applied to the include to affect whether or not the include is processed.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mike Klebolt

unread,
Jan 22, 2018, 9:32:57 AM1/22/18
to Ansible Project
Thanks Matt. I was able to get everything functioning using the `block` method.  Do you know if this will be revisited in a future release because I grew to like the include functionality prior to ebf971f?  
Reply all
Reply to author
Forward
0 new messages