3

Using Ansible ansible 2.4.2.0 I'm trying to run only a certain play from the main playbook (main.yml)

---
# main playbook for cluster deployment

  # initial configuration of OS same for all hosts
- name: Cluster Deployer playbook
  hosts: all
  roles:
    - { role: common, tags: ['initial_config'] }


  # configuration specific for loadbalancers hosts group
- name: Cluster Deployer playbook - Load Balancer setup
  hosts: loadbalancers
  roles:
    - { role: loadbalancers_setup, tags: ['setup_loadbalancer'] }

  # configuration specific for db-servers hosts group
- name: Cluster Deployer playbook - Database servers setup
  hosts: db-servers
  roles:
   - { role: db_servers_setup, tags: ['setup_db_server'] }

  # configuration specific for web-servers hosts group
- name: Cluster Deployer playbook - Web Servers setup
  hosts: web-servers
  roles:
   - { role: web_servers_setup, tags: ['setup_web_server'] }

Here's my hosts file

[loadbalancers]
192.168.99.10

[db-servers]
192.168.99.2

[web-servers]
192.168.99.3
192.168.99.4

I'm calling the playbook with the command

ansible-playbook main.yml --inventory hosts --tags initial_config

The problem is that Ansible runs all plays from that playbook even though I've requested only those with tag initial_config in the roles section. I've tried with setting the tags section under hosts but with no luck.

Is it possible to define tags on this level without the need for tagging individual tasks in roles? I'm new to Ansible so if there is a better way of writing playbooks, I'm all ears.

2
  • Could be syntax issue, try using "" : ansible-playbook main.yml --inventory hosts --tags "initial_config"
    – karthik v
    Jan 29, 2018 at 11:45
  • Same thing with the quotes @karthikvee
    – Matija B
    Jan 29, 2018 at 12:21

2 Answers 2

2

The "tag" directive is misplaced in your code.

Your code should look like so:

---
# main playbook for cluster deployment

  # initial configuration of OS same for all hosts
- name: Cluster Deployer playbook
  hosts: all
  roles:
    - common
  tags: 
    - initial_config

  # configuration specific for loadbalancers hosts group
- name: Cluster Deployer playbook - Load Balancer setup
  hosts: loadbalancers
  roles:
    - loadbalancers_setup
  tags: 
    - setup_loadbalancer

  # configuration specific for db-servers hosts group
- name: Cluster Deployer playbook - Database servers setup
  hosts: db-servers
  roles:
    - db_servers_setup
  tags: 
    - setup_db_server

  # configuration specific for web-servers hosts group
- name: Cluster Deployer playbook - Web Servers setup
  hosts: web-servers
  roles:
    - web_servers_setup
  tags:
    - setup_web_server

And your ansible command should look like so:

ansible-playbook main.yml --inventory hosts --tags "initial_config"

For more information, check Ansible's official documentation.

3
  • Still no luck, when running this playbook with ansible-playbook main.yml --inventory hosts --tags "setup_db_server" (or without quotes on the tag name) all plays are listed and executed in order they are written in the playbook..
    – Matija B
    Jan 29, 2018 at 12:20
  • @MatijaB Have a look at this answer
    – techraf
    Jan 31, 2018 at 9:01
  • Should you include the "always" tag to also gather facts? docs.ansible.com/ansible/latest/user_guide/… Sep 26, 2021 at 22:23
0

Looks like you have misconception of tag inheritance here:

Tag Inheritance

You can apply tags to more than tasks, but they ONLY affect the tasks themselves. Applying tags anywhere else is just a convenience so you don’t have to write it on every task. Ansible documentation on tag inheritance

Try something like this:

- name: Cluster Deployer playbook
  hosts: all
  tasks:
    - include_role:
        name: common
      tags:
       - inital_config

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .