Is possible to write 1 inv file with severl groups and run ansible-playbook for 1 group?

Hi, I have an inventory file as follows and would like to know if it is possible to run the playbook but for 1 group in the inv file

web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant username=ansible_user 
db1 ansible_ssh_host=192.168.33.30 

[webservers]
web1

[dbservers]
db1

[datacenter:children]
webservers
dbservers

[datacenter:vars]
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant

When I run
ansible-playbook -i inv webserves myplaybook.yml

I get an error indicating that webservers is not a playbook.

I tried

ansible-playbook webserves  -i inv webserves myplaybook.yml
ansible webserves  -i inv webserves myplaybook.yml

but did not help
Thanks for your help.

You can specify targets in the playbook via hosts field if you want to do it permanently.

- hosts: webservers
  tasks:
    - ...

If you want to limit it via cli use –limit option, like so:

ansible-playbook -i inv --limit webservers myplaybook.yml

See Patterns: targeting hosts and groups — Ansible Community Documentation for more info

1 Like