Ansible delegate_to Module with Examples

Ansible delegate_to module

If you want to run any task on any particular machine you can use ansible delegate_to module. In the following steps i will show how to use ansible delegate to module with different examples.

---

- hosts: web

  tasks:

  - name: print httpd version
    command: /usr/sbin/httpd -v

  - name: install httpd
    yum:
      name: httpd
      state: latest
    delegate_to: web.jboss.etm1

Here first task will run on all hosts in web group but second task will run on only web.jboss.etm1  machine.

ansible delegate to localhost

If you want to run the task on local ansible control machine you can use this module. Or you can use ansible local_action module.

---

- hosts: webservers

  tasks:

  - name: print httpd version
    command: /usr/sbin/httpd -v
    delegate_to: 127.0.0.1

  - name: install httpd
    yum:
      name: httpd
      state: latest

In this playbook you can mention 127.0.0.1 or localhost both refers to ansible control machine.

In this playbook the first task will run on local ansible control machine and second task will run on all webservers group of machines.

ansible local_action

You can modify above playbook by using local_action in the place of delegate_to 127.0.0.1(localhost).

---

- hosts: webservers

  tasks:

  - name: print httpd version
    local_action: /usr/sbin/httpd -v
    
  - name: actual steps would go here
    yum:
      name: httpd
      state: latest

Both above two playbooks will give the same result.

 

  • ansible delegate_to example
  •  group delegate_to ansible
  • ansible delegate_to not working
  • delegate_to ansible  inventory host
  • ansible delegate_to user password
  • delegate to ansible  group of hosts
  • ansible delegate to first host in group
  • delegate to ansible  remote host
  • ansible delegate to current host
  • delegate to ansible localhost

1 Response

  1. Anonymous says:

    Noicee!!

Leave a Reply

Your email address will not be published. Required fields are marked *