BacklogZero®

Ansible in a Minute · Ansible Core ·

Use delegate_to to run a task on another host

Short answer

Use delegate_to so a task runs on localhost (or another host) while the play still targets the current inventory host.

Ansible in a Minute — delegate_to

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

echo inventory_hostname delegated to localhost

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: echo inventory_hostname delegated to localhost
  hosts: all
  tasks:
    - name: Echo inventory_hostname delegated to localhost
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }}"

How it works

This example prints inventory_hostname on each play host. debug cannot be delegated, so the task still runs on the current host. Use delegate_to when the work must happen on the controller or on another host. inventory_hostname still names the current play host.

When to use it

  • Updating a pool or inventory record on the controller
  • Running a command that only exists on localhost

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-16

Notes

debug cannot be delegated. Connection variables on the task refer to the delegated host. Prefer a host that exists in inventory. Pair with run_once when many hosts would write the same file in parallel.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Use delegate_to to run a task on another host · Level Up Labs