Ansible: Calling tags from tags from role but they are not getting executed

Quintin Par asked:

I have some tasks as shown below

- name: Add the server's domain to the hosts file
  lineinfile:
   dest: /etc/hosts
   #regexp='.*{{ item }}$'
   line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_FQDN_NAME }} {{ LOCAL_HOSTNAME }}"
   state:  present
  when: hostvars[item].ansible_default_ipv4.address is defined
  with_items: "{{ groups['cache'] }}"
  tags: [ 'never', 'hostname' ]

- name: Set the timezone for the server to be UTC
  file:
    path: /usr/share/zoneinfo/UTC
    dest: /etc/localtime
    state: link

- name: Copy the NGINX repository definition
  copy: src=nginx.repo dest=/etc/yum.repos.d/
  tags: [ 'never', 'setuprepo' ]

and I call them from my playbook as

- hosts: cache
  vars:
   LOCAL_HOSTNAME: 'web02'
  roles:
  - { role: basic-setup, tags: [ 'hostname', 'setuprepo', 'firewall' ]}

But despite calling the tags explicitly, the appropriate tasks like “Add the server’s domain to the hosts file” is not getting executed whereas “Set the timezone for the server to be UTC” is getting executed.

edit:
My command line is a simple

ansible-playbook server.yml 

Here’s how the command was executed
enter image description here

As you can see when I execute the command I don’t see any tasks for the tags I called from

  • { role: nginx, tags: [ ‘hostname’, ‘setuprepo’, ‘firewall’ ]}

What am I doing wrong here?

My answer:


But you haven’t specified any tags to use!

You gave your command as:

ansible-playbook server.yml 

You have not specified any tags to use.

So, anything with tags of never doesn’t get called, such as your two plays above.

To use tags, you specify the tags you want to use on the ansible-playbook command line when you give the command. For example:

ansible-playbook server.yml --tags "firewall,hostname"

You can also specify to skip tags:

ansible-playbook server.yml --skip-tags "setuprepo"

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.