Interview Prep/Ansible/Vault, Privilege Escalation and Error Handling

How does `delegate_to` work and what are its subtleties?

AdvancedConceptVault, Privilege Escalation and Error Handling
Answer

delegate_to runs a task on a different host while keeping the original host's variable context (inventory_hostname, host vars, registered values). Typical uses: call a load balancer API from the controller, update a DNS record, or take a node out of rotation.

- ansible.builtin.command: "/usr/local/bin/lb-disable {{ inventory_hostname }}"
  delegate_to: lb1

Subtleties: connection variables (such as ansible_user, ansible_host) come from the delegate host; become rules still apply; delegate_facts: true stores gathered facts on the delegate instead of the original host; and without run_once, the task is executed once per original host, so a delegated task in a 50-host play runs 50 times.

All Ansible questions Roadmap: Ansible

More on Vault, Privilege Escalation and Error Handling