Interview Prep/Ansible/Inventory and Targeting

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.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting