I have an inventory file
example:
[groupa]
10.102.16.100
10.102.16.101
[groupa:vars]
app=testapp-a
env=staging
[groupb]
10.102.16.102
10.102.16.103
[groupb:vars]
app=testapp-b
env=production
Now if I run
ansible-playbook -i ./example playbook-x.yml
This will run for all the hosts. Is there any way I can specify the group name in the command line itself.
I'm expecting something like
ansible-playbook -i ./example --group-name groupb playbook-x.yml
You can use --limit to specify the group like so,
ansible-playbook -i ./example playbook-x.yml --limit groupa
hosts field of playbook can also be used to target a specific group like,
- hosts: groupa
tasks:
...
I think you want to target inventory groups dynamically; a simple approach is to pass the hosts as variable in the playbook.
---
hosts: “{{target}}”
tasks:
While running the playbook pass the target variable like ‘ansible-playbook playbook-x.yml -i ./example -e target=groupa’
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With