A systems administrator needs to run a playbook that installs packages on a group of managed nodes. The playbook should run only on nodes that are part of the 'web_servers' group in the inventory. Which approach is best practice?
Trap 1: Set 'hosts: all' and use '--limit web_servers' when running…
This requires remembering the limit flag each run; not idempotent in playbook definition.
Trap 2: Set 'hosts: localhost' and delegate tasks to web_servers.
localhost is the control node, not the managed nodes; delegation complicates the setup.
Trap 3: Set 'hosts: all' and use a 'when' condition to check if the node is…
Using 'hosts: all' runs on all nodes, requiring extra conditional logic that can be avoided.
- A
Set 'hosts: web_servers' in the play.
Directly targeting the group is the simplest and most readable approach.
- B
Set 'hosts: all' and use '--limit web_servers' when running ansible-playbook.
Why wrong: This requires remembering the limit flag each run; not idempotent in playbook definition.
- C
Set 'hosts: localhost' and delegate tasks to web_servers.
Why wrong: localhost is the control node, not the managed nodes; delegation complicates the setup.
- D
Set 'hosts: all' and use a 'when' condition to check if the node is in the web_servers group.
Why wrong: Using 'hosts: all' runs on all nodes, requiring extra conditional logic that can be avoided.