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.
More on Vault, Privilege Escalation and Error Handling
What is Ansible Vault?BeginnerWhich `ansible-vault` commands do you use day to day?BeginnerHow do you supply the vault password when running a playbook?BeginnerWhat is `encrypt_string` and when is it better than encrypting a whole file?IntermediateWhat are vault IDs and why use them?AdvancedHow is Vault data encrypted and what does the file look like?Advanced