I have an inventory file like:
[my_domain]
255.255.255.255
[production:children]
my_domain
[load_balancer:children]
my_domain
[webservers:children]
my_domain
I have a playbook.yml
like:
---
- hosts: webservers
gather_facts: no
tasks:
- debug:
msg: Hello webservers
- hosts: load_balancer
gather_facts: no
tasks:
- debug:
msg: Hello load_balancer
When running the following command it runs both the plays in the playbook, i.e. it prints "Hello webservers" and "Hello load_balancer":
ansible-playbook ./playbook.yml --limit "webservers:&production"
I can get it to only run the webservers play by adding a tag to it and using tags
on the command line such as:
...
- hosts: webservers
tags: ['webservers']
# or if using roles then:
# roles:
# - { role: provision_webserver, tags: [ 'provision_webservers' ] }
...
And use:
ansible-playbook ./playbook.yml --limit "webservers:&production" --tags "webservers"
I was hoping the limit
arg would work not only on the inventory file but also on the hosts
value of each play. The docs seem to suggest this is possible though they give the example using ansible
and a one off command rather than ansible-playbook
and a playbook:
This can mean what hosts to communicate with, but in terms of Playbooks it actually means what hosts to apply a particular configuration or IT process to.
I think the answer is that I am using playbooks wrong and should only have one play per playbook which necessarily means it will only have one hosts
and you select which plays you want to run by changing the playbook<-abc>.yml
you are using. However that last link also suggests you can just use --limit
to set which hosts. Very confused.
ansible_host=...
. For the moment I'll stick with the current multi play playbook and tags. Thank you for the rapid answer. – Photochromy