I’m working on an ansible role and instead of putting all tasks in tasks/main.yml, I’d like to have separate files and use them sort of like functions. One of my files has all the possible tasks for file manipulation in a separate file. The thought would be to use tags to execute one of the tasks in that file.
In the example below I’ve put all the file manipulation tasks in a file called 'reboot_me_file.yml and would like to call just one task in main.yml when I need it. For example, ‘reboot_me_file.yml’ has three tagged tasks (create a file, stat a file, delete a file) and during the main.yml play, there will be a task, that includes say the ‘create a file’ task. I’ve looked at the following URL and it sounds like I can call a tagged task in the ‘reboot_me_file.yml’, but when I run the playbook, it executes all the tasks in ‘reboot_me_file.yml’
https://docs.ansible.com/ansible/latest/modules/include_tasks_module.html#include-tasks-module (It says this should be available in ansible 2.7 but not which version of 2.7)
Anyone tried anything like this?
– ansible version: ansible 2.7.10
– tasks/main.yml
- name: Checking for an old ‘reboot_me’ file
include_tasks:
file: reboot_me_file.yml
apply:
tags:
- check_reboot_file
– tasks/reboot_me_file.yml
I'm working on an ansible role and instead of putting all tasks in
tasks/main.yml, I'd like to have separate files and use them sort of like
functions. One of my files has all the possible tasks for file manipulation
in a separate file. The thought would be to use tags to execute one of the
tasks in that file.
In the example below I've put all the file manipulation tasks in a file
called 'reboot_me_file.yml and would like to call just one task in main.yml
when I need it. For example, 'reboot_me_file.yml' has three tagged tasks
(create a file, stat a file, delete a file) and during the main.yml play,
there will be a task, that includes say the 'create a file' task. I've
looked at the following URL and it sounds like I can call a tagged task in
the 'reboot_me_file.yml', but when I run the playbook, it executes all the
tasks in 'reboot_me_file.yml'
https://docs.ansible.com/ansible/latest/modules/include_tasks_module.html#include-tasks-module
(It says this should be available in ansible 2.7 but not which version of
2.7)
It doesn't, it says "will be applied to the tasks within the include."
Anyone tried anything like this?
Many have, all have failed, since it doesn't work like that.
All tags in a file is always added to the task, at this level it is not a filter.
To choose which tags to run is done at the command line with -t/--tags
OK. I’ll just break out the tasks into separate files.
Thanks for the reply,