Search code examples
ansibleansible-2.xansible-inventory

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

Solution

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