Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling an ansible playbook with tag and parameter

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 }}'
like image 951
ali haider Avatar asked Oct 21 '16 16:10

ali haider


People also ask

How do you pass tags in Ansible playbook?

Adding tags to includes If you add mytag to a dynamic include, then run that playbook with --tags mytag , Ansible runs the include itself, runs any tasks within the included file or role tagged with mytag , and skips any tasks within the included file or role without that tag.

How do I use playbook with tags?

Execute a specific task in a playbook The tags are specified using the tags label at the end of each task. In this playbook, we have three tags: hello, welcome, and enjoy. As mentioned earlier, you can use tags to control the execution of Ansible playbooks. To specify which task to execute use the -t or –tags flag.

Where can you use tags in Ansible?

A tag is an attribute that you can set to an Ansible structure (plays, roles, tasks), and then when you run a playbook you can use –tags or –skip-tags to execute a subset of tasks. Now to see the effect of tags we can –list-tasks applying tags filters to know which tasks will run.

How do tags work Ansible?

Tags are metadata that you can attach to the tasks in an Ansible playbook. They allow you to selectively target certain tasks at runtime, telling Ansible to run (or not run) certain tasks.


1 Answers

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.

like image 159
Shasha99 Avatar answered Oct 11 '22 12:10

Shasha99