Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing host group name in ansible-playbook command line

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
like image 570
Kaushik Vijayakumar Avatar asked Oct 26 '25 07:10

Kaushik Vijayakumar


2 Answers

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:
    ...
like image 67
Moon Avatar answered Oct 29 '25 08:10

Moon


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’

like image 39
2 revsShubham Srivastava Avatar answered Oct 29 '25 08:10

2 revsShubham Srivastava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!