Kaushik Vijayakumar
Kaushik Vijayakumar

Reputation: 835

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

Upvotes: 3

Views: 8027

Answers (2)

Shubham Srivastava
Shubham Srivastava

Reputation: 33

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’

Upvotes: 3

Moon
Moon

Reputation: 3037

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:
    ...

Upvotes: 4

Related Questions