Run ansible on specific hosts group

8.8k views Asked by At

I am trying to run ansible with the following command,

ansible-playbook provision.yml -l webserver

And my hosts file contains the following host groups,

[webclient]
172.29.4.75
[webserver]
172.29.4.76

My provision.yml also contains 2 hosts as below,

- hosts: webclient
  user: centos
  roles: 
   - nginx
   - nvm
- hosts: webserver
  user: centos
  roles: 
   - tomcat

My issue here is even thought I use "-l webserver" roles specified for webclient also runs in webclient hosts. How can I control it to run only specific host groups?

1

There are 1 answers

2
Konstantin Suvorov On

My issue here is even thought I use "-l webserver" roles specified for webclient also runs in webclient hosts. How can I control it to run only specific host groups?

This usually means that you have same host under webserver and webclient groups.

Passing -l webserver tells Ansible to use all host from inventory, that are under webserver group.
When Ansible starts this play - hosts: webclient, it searches for matches in inventory and then reduce the match with hosts from limit argument. So if you have some host that is both under webserver and webclient, Ansible will execute tasks from webclient play for them.