You want to run a task on every host in the `db` group and also on localhost in the same playbook.
BeginnerScenarioInventory and Targeting
Answer
Situation read → Two audiences in one run, with different connections.
What I'd do → Use two plays, or delegate_to: localhost for a single task.
- hosts: db
tasks:
- ansible.builtin.dnf: { name: postgresql-server, state: present }
- hosts: localhost
connection: local
gather_facts: false
tasks:
- ansible.builtin.debug: { msg: "DB tier configured" }
Why → Plays are the unit of host selection; mixing hosts inside one play is what delegate_to is for.
Interview tip → Implicit localhost is not in the all group, so it is not matched by hosts: all.
More on Inventory and Targeting
You are handed a fresh Ansible project and 10 servers. How do you make sure Ansible can reach all of them before writing any playbook?BeginnerYour team keeps prod and staging hosts in one inventory file and someone ran a playbook on prod by mistake. How do you prevent it?IntermediateYou need to run a playbook on only the web servers in Mumbai that are not in the canary group. How?BeginnerServers are autoscaled in AWS and IPs change daily. How do you keep the inventory accurate?IntermediateA host is reachable by SSH on port 2222 with a different user from all others. How do you model that?BeginnerYou must configure servers in a private subnet that is only reachable through a bastion.Intermediate