How to call roles & specify tags for those roles

9,166 views
Skip to first unread message

Anfield

unread,
Aug 16, 2017, 9:17:37 PM8/16/17
to Ansible Project
Trying to run an example from the ansible docs, and it seems to always run both the tagged portions of the playbook...

Am I missing how calling a role with tags is supposed to be used?

Ansible docs -

http://docs.ansible.com/ansible/latest/playbooks_tags.html
roles:
  - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }

My playbook and role call below ->

Role call

---
- hosts: 10.10.0.5
become: yes
roles:
- {role: apache, tags: [ 'config_apache']}

tasks/main.yml

- name: Install apache
  yum:
   name: httpd
   state: latest
  tags: apache_install

- name: Config apache
  lineinfile:
    path: /etc/httpd/conf/httpd.conf
    line: penguin
  tags: config_apache


Mike Klebolt

unread,
Aug 17, 2017, 4:07:32 PM8/17/17
to Ansible Project
If you are adding a tag to a role, there is no need to use the same tag on any of the tasks within the role.

So running your playbook with -t config_apache will include both tasks.  Running it with -t apache_install will only run the Install apache task.

Anfield

unread,
Aug 17, 2017, 4:21:36 PM8/17/17
to Ansible Project
Ok..Not sure I follow. So what would be the point then in calling a role with tags at all, if you cannot define which tag to use?


Mike Klebolt

unread,
Aug 17, 2017, 4:33:04 PM8/17/17
to Ansible Project
I use tags at the role level when I have a play that contains multiple roles.  If all you're wanting to do is run the config apache task, remove the config_apache tag from the role and leave it on the task.  Hopefully I'm understanding your question correctly.

---
- hosts: 10.10.0.5
become: yes
roles:
    - {role: test1, tags: [ 'test1']}
    - {role: test2, tags: [ 'test2']}

Anfield

unread,
Aug 18, 2017, 11:13:20 AM8/18/17
to Ansible Project
If I remove the tag from the role and leave it on the task, then it will run both tasks? Both tasks are tagged


Mike Klebolt

unread,
Aug 18, 2017, 11:26:25 AM8/18/17
to Ansible Project
What is your desired result from using the tags?

Kai Stian Olstad

unread,
Aug 18, 2017, 4:56:14 PM8/18/17
to ansible...@googlegroups.com
On 17. aug. 2017 03:17, Anfield wrote:
> Trying to run an example from the ansible docs, and it seems to always run
> both the tagged portions of the playbook...
>
> Am I missing how calling a role with tags is supposed to be used?
>
> Ansible docs -
>
> http://docs.ansible.com/ansible/latest/playbooks_tags.html
I guess you did not read that link carefully enough :-)

From the link:
"You can ONLY filter tasks based on tags from the command line with
–tags or –skip-tags."

--
Kai Stian Olstad

Anfield

unread,
Aug 22, 2017, 11:45:44 AM8/22/17
to Ansible Project
Good question Mike, no requirement, just learning this incase it comes up in the cert. exam. Based on that link from the docs I still dont know why you would call a role with tags



Reply all
Reply to author
Forward
0 new messages