1
votes

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

  • Master PlayBook
    • includes deployment playbook that have vars/config specific for deploying latest release
    • deployment playbooks includes specific roles related to the SW
      • roles have tasks that are tagged appropriately

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

  • include: task1.yml tags: t1

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

2
Why would you want it this way? - Konstantin Suvorov
Particular deployments just use couple of tasks from multiple the roles which are called from the deployment playbooks passing on various parameters, so instead of running the whole role we just need to pass on the tags to limit the tasks that we only require. - sjm

2 Answers

1
votes

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.

1
votes

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