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.

Matija B
  • 31
  • 1
  • 1
  • 3

2 Answers2

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.

Itai Ganot
  • 10,644
  • 29
  • 93
  • 146
  • 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 '18 at 12:20
  • @MatijaB Have a look at [this answer](https://stackoverflow.com/a/48527578/2947502) – techraf Jan 31 '18 at 09:01
  • Should you include the "always" tag to also gather facts? https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#special-tags-always-and-never – brettinternet Sep 26 '21 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
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39