Interview Prep/Ansible/Rolling Updates, Delegation and Safe Deployments

When and how do you use `delegate_to: localhost`?

BeginnerProductionRolling Updates, Delegation and Safe Deployments
Answer

For actions that operate through an API from the controller and are not tied to the host's OS: calling a cloud API, a webhook, a monitoring silence, or DNS update.

- ansible.builtin.uri:
    url: "https://monitoring.example.com/api/silences"
    method: POST
    body_format: json
    body: { host: "{{ inventory_hostname }}", duration: "30m" }
  delegate_to: localhost
  become: false

The task runs once per host, so add run_once: true when it is a single, global action. Also set become: false, which otherwise inherits the play's setting.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Safe Deployments