Is it possible to use include_tasks to run a tagged task in another file?

17 views
Skip to first unread message

Douglas Thompson

unread,
Jun 11, 2019, 1:39:23 PM6/11/19
to Ansible Project
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

---
# file operations on /tmp/reboot_me.txt

- name: Creating the file 'reboot_me'
  file:
    path: "/tmp/reboot_me"
    state: touch
    mode: 0666
  tags:
  - create_reboot_file

- name: Checking for the file 'reboot_me'
  stat:
    path: /tmp/reboot_me
  register: file_reboot_me
  tags:
  - check_reboot_file

- name: Removing the file 'reboot_me'
  file:
    path: "/tmp/reboot_me"
    state: absent
  tags:
  - remove_reboot_file

Kai Stian Olstad

unread,
Jun 11, 2019, 1:55:41 PM6/11/19
to ansible...@googlegroups.com
On 11.06.2019 19:39, Douglas Thompson wrote:
> 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


--
Kai Stian Olstad

Douglas Thompson

unread,
Jun 11, 2019, 1:57:29 PM6/11/19
to Ansible Project
OK. I'll just break out the tasks into separate files.
Thanks for the reply,
Reply all
Reply to author
Forward
0 new messages