Interview Prep/Ansible/Inventory and Targeting

You need to run a playbook on only the web servers in Mumbai that are not in the canary group. How?

BeginnerScenarioInventory and Targeting
Answer

Situation read → A set-algebra host pattern: intersect two groups, exclude a third.

What I'd do →

ansible-playbook site.yml --limit 'web:&mumbai:!canary'
ansible-playbook site.yml --limit 'web:&mumbai:!canary' --list-hosts

Always run --list-hosts first to see what the pattern resolves to.

Why → :& is intersection and :! is exclusion; quoting prevents the shell from eating ! and &.

Interview tip → Know that --limit narrows the play's hosts:; it cannot add hosts the play never targeted.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting