sjm
sjm

Reputation: 465

Ansible limiting tasks based on tags in included playbooks and roles

I am managing quite a large website fairly complex. We are using ansible for deployments; majority of the deployments are fine we can just include the playbooks and roles in a master playbook and it works like a charm.

Master playbooks looks like something below

The issue we are having is that we can't pass on the tags while including the playbooks in master playbook. Something like

This work absolutely fine if it's called from the command line, without using tags in master playbok

ansible-playbook -i host master_playbook.yml -t t1

Any suggestions for a possible solutions would be helpful

Upvotes: 1

Views: 3378

Answers (2)

Samuel Rogers
Samuel Rogers

Reputation: 11

To answer Konstantin question as to why you would want to do it that way; would be that the best practice would be to include different task files and include them in main.yml. With in a task file you could have two+ tasks and although you could/should tag each task there is a strong case that you would want to run all the tasks in a file not just one task in that file. Being able to tag a task include file provides granularity to be able to run/test your playbook.

$ ansible-playbook -u a_user -i inventory/ts_host.yml playbook-test.yml -t test:template_file

playbook-test.yml

  • hosts: crash_n_burn_poc roles:
    • test_role

test_role

main.yml

tasks file for testing

  • include: copy_test_file.yml
    tags: ["test","test:copy_file"]
  • include: template_test_file.yml tags: ["test","test:template_file"]

Below is a output of the playbook.

$ansible-playbook -u a_user -i inventory/ts_host.yml playbook-test.yml -t test:template_file

PLAY [crash_n_burn_poc] ******************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************* ok: [linuxdev.nut]

TASK [test_role : template] ************************************************************************************************************** changed: [linuxdev.nut]

PLAY RECAP ******************************************************************************************************************************* linuxdev.nut : ok=2 changed=1 unreachable=0 failed=0

Upvotes: 1

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

This is not something that can be easily achieved.

Simple way: you can refactor your roles to have separate tasks files like:

tasks/main.yml (that import job1 and job2)
tasks/job1.yml
tasks/job2.yml

And use this

- include_role:
    name: myrole
    tasks_from: job1.yml

to include just job1 tasks.

Hard way: you can make a callback plugin that modifies execution context on the fly taking required tags from variables with a combination of set_play_context and v2_playbook_on_play_start handlers. There's a post about this here, but it's in Russian.

Upvotes: 1

Related Questions