I am trying to call an ansible v2.1 playbook (notifications.yml) from another playbook using an include statement. I would like to specify a tag as well as pass a parameter to the called playbook (from within the playbook which has the included playbook). I was able to get this to work without the tag but cannot seem to get it to work with the tag. Is this even possible (without command line parameters) and if so, what is the syntax/correct way of achieving this:
main.yml
- include: playbooks/notifications.yml
tags: ['slack']
slack_msg: "test"
notifications.yml
---
- connection: local
hosts: 127.0.0.1
tasks:
- name: Send notification message via Slack
tags:
- slack
local_action:
module: slack
domain: changed.slack.com
token: "{{ slack_token }}"
msg: "{{ slack_msg }}"
channel: "{{ slack_channel }}"
username: "{{ slack_username }}"
- name: Send notification message via Email
tags:
- email
local_action:
module: mail
host: "smtp.gmail.com"
port: "587"
username: '[email protected]'
password: 'changed'
to: "changed <[email protected]>"
subject: 'Ansible-report'
body: 'System {{ ansible_hostname }} has been successfully provisioned. User {{ ansible_user_id }}'
I will first discuss a little about how tags actually work when we use include/roles.
Tags has to be specified while running the playbook using --tags and all tasks coming under the specified tags will be executed irrespective of whether they are defined in the main file or the roles/included files.However there are some interesting facts to be noted:
main.yml
---
- hosts: all
tasks:
- include: includeFile.yml tags=t1
- debug: msg="Inside main file"
tags:
- t2
includeFile.yml
- debug: msg="task 1 in includeFile"
tags:
- t1
- debug: msg="task 2 in includeFile"
tags:
- t2
- debug: msg="task 3 in includeFile"
tags:
- t3
1. ansible-playbook -i hosts main.yml --tags "t1"
Now since this tag is being used by the first task in main.yml which is including a file, so all the tasks defined in includeFile.yml will be executed without further checking of tags in includeFile.yml.
Note that a task in includeFile.yml is also using this tag but that does not mean that only that particular task will be executed.
2. ansible-playbook -i hosts main.yml --tags "t2"
In this case, since first task (include) in main.yml is not using this tag. that does not mean that no task of includeFile.yml will be executed, includeFile.yml will further be searched for the mentioned tag. And hence second task of main.yml and second task of includeFile.yml will be executed.
3. ansible-playbook -i hosts main.yml --tags "t3"
Now in this case, only third task of includeFile.yml will be executed (same logic as explained in second point).
Now coming back to your question:
We can mention tags only at time of ansible-playbook command execution using --tags. So for now we can not pass tags while including file.
However if you mention some tag (which is present in include file ) while running ansible-playbook you are through... but then only the tasks(in main file) which are using that particular tag will be executed. So this option doesn't look feasible in your case because you will have to tag all the tasks present in the main file to make you things work.
So as we discussed in the comments, its better to pass a dummy variable to that include file and run the tasks conditionally. But you may use the tags as well in case your problem statement is set according to what we discussed above.
Note: Tags can still be useful while dealing with roles/includes. When you want not to run some tasks that may be present in main playbook or in the role/include attached, tags fits perfectly in this case (we use --skip-tags while running ansible-playbook to achieve this).
Final Note: The main purpose of using tags is to have dynamic control on which tasks needs to executed in that particular run. If you will hard-code the tag (passing to include), the purpose is lost then.