5

I want to import into playbook (and execute) only part of Ansible role defined in tagged block.

E.g. I have role some_role containing 3 blocks of tasks tagged as tag1, tag2 and tag3. I could create playbook that imports whole role:

---
- hosts: some_host
  roles:
    - role: roles/some_role

And then execute it from command line specifying single tag:

$ ansible-playbook -i hosts.yml playbook.yml --tags tag1

But I want to move --tags tag1 part into playbook itself to be able to run that single block without providing tags to ansible-playbook.

anlar
  • 477
  • 1
  • 11
  • 26

4 Answers4

4

I couldn’t find any easy way to execute part of a role with specific tag from the playbook.

An way could be to break the tasks in multiple files and use a file from playbook using import_role or include_role. Say, if you create two files in role’s task directory named main.yml and other.yml then you can use other tasks like below.

- import_role:
     name: myrole
     tasks_from: other
Moon
  • 2,837
  • 1
  • 20
  • 34
-1

Never mind. THIS DOES NOT WORK. --Jack

Useful discussion in comments, so I'm leaving this up.

Never tried it, but give apply a shot with include_role:

---
- hosts: some_host
  tasks:
  - include_role:
      name: roles/some_role
    apply:
      tags:
      - tag1

https://docs.ansible.com/ansible/latest/modules/include_role_module.html

Jack
  • 5,801
  • 1
  • 15
  • 20
  • `apply` will apply the named tag(s) to the tasks of included role, not actually filter them for execution. I think OP wants to execute part of the role having a specific tag. – Moon May 30 '20 at 11:15
  • Yes, `apply` will only update imported tasks but still will import everything from role. – anlar May 30 '20 at 11:23
  • Call me confused. What use, then, is `apply`? – Jack May 30 '20 at 11:25
  • @Jack, you could import several roles, assign tags to them and then execute them selectively when running `ansible-playbook --tags ...`. – anlar May 30 '20 at 11:32
  • How is that any different from the `tags` option at the task level? – Jack May 30 '20 at 11:34
  • In fact, `include_role` doesn’t even tag underline tasks. Honestly, I didn’t have any use case so far to use this feature. – Moon May 30 '20 at 11:36
  • You could tag whole role with single command. Plus when importing same role into different playbooks you could assign different tags to it in each playbook. Not using it myself but probably could be handy in large projects with many roles and playbooks. – anlar May 30 '20 at 11:38
  • OK -- but `tags` at the play level does the same thing, yes? – Jack May 30 '20 at 11:43
  • OK -- I have figured out `apply: tags`. It appends the specified tags to the tasks' `tags:` options. So it a task in the role has the the option `tags: tag2` and the `include_role` has `apply: tags=tag1`, that task will act as though it had `tags: tag2, tag1` set on it. Thanks, guys. – Jack May 30 '20 at 11:58
-1
- import_role: 
      name: myrole 
  tags: [ web, foo ] 

- import_tasks: foo.yml 
  tags: [ web, foo ]

You can achieve it using the above code block.

Reference: ansible doc

deepanmurugan
  • 1,815
  • 12
  • 18
  • No, you can tag the task with these tags. It does not import _only_ tasks with those tags, it adds tags to the this `-import_tasks` role. – KamilCuk Nov 02 '21 at 07:18
-1
---
- hosts: some_host
  tasks:
    - include_role:
        name: some_role
      tags: tag1

some_role has to have tag1 defined in its tasks naturally. But you also need to execute it using tag1, just like you did in the question:

ansible-playbook -i hosts.yml playbook.yml --tags tag1

I've just tested it with ansible 2.10.6 following docs. Make sure you use include instead of import.

BartBiczBoży
  • 2,512
  • 2
  • 24
  • 33