Updated April 1, 2023
Introduction to Ansible delegate_to
Ansible delegate_to property or the keyword specified in the ansible-playbook is used to provide the control to run the task locally or to the other different hosts rather than running on the remote hosts specified on the inventory server list, and this can be the few tasks or running the entire play-book locally and also delegating facts to the local machines or the specific set of groups.
How do the Ansible delegate_to works?
There are many playbooks in the projects that sometimes require to run tasks or the entire playbook on the local machine that hosts the inventory file or playbook, or we can say on the different remote hosts that are different from the remote hosts that are mentioned in the inventory list to run tasks In that situation, we can use the delegate_to parameter so that the tasks can run locally.
For example,
Let say you have a task of patching to run on a bunch of app servers, and among them, there are few database servers on which you first need to take the database down before applying patches, but they are not part of the inventory group. In that case, you need to run that command locally on the remote database servers or on the local ansible host (if needed) and have to delegate permission to the remote database hosts to run the task, so for that specific task, we will use the delegate_to parameter so it can execute the commands remotely.
Once you run the delegate_to command for the specific task, the controller machines relieve the control and grant it to the mentioned machine to run locally, and then control returns backs to the playbook’s next task.
We will use the below examples to understand the command functionality. We have differentiated the example below/
Without using the delegate_to parameter (on remote servers – linuxservers group).
---
- name: Ansible playbook without delegate_to example
hosts: linuxservers
tasks:
- name: Check the website status
uri:
url: https://theautomationcode.com
method: GET
register: http_output
- name: tasks to get the variable output
debug: var=http_output
Output:
Using Delegate_to keyword.
---
- name: Ansible playbook delegate_to example
hosts: linuxservers
tasks:
- name: Check the website status
uri:
url: https://theautomationcode.com
method: GET
delegate_to: 127.0.0.1
register: http_output
- name: tasks to get the variable output
debug: var=http_output
In the above example, we are delegating permission to run the task from the localhost (127.0.0.1).
Output:
If you notice the difference between the two outputs, in the second output, you can see the task is being delegated to the localhost (192.168.0.102 => 127.0.0.1) that is the controller node.
You can also use the localhost instead of 127.0.0.1
Once you use the delegate_to, by default, the facts gathered by any delegated tasks are assigned to the inventory_hostName (the current Host) instead of the host which produced the facts (the delegated tasks servers). To set the facts gathered by the delegated servers, we need to use delegated_facts, and its value should be true.
---
- name: Example of the delegate facts
hosts: dbservers
tasks:
- name: test playbook for delegate facts
setup:
delegate_to: {{ items }}
delegate_facts: True
loop: "{{ groups['appservers'] }}"
In the above example, we are writing the playbook for the DB servers, and the task will be delegated to few servers which are stored in the items variable, and the task gather facts will be run on the app servers group only, not on all the DB servers listed.
We can also use the local_action instead of the delegate_to command, as shown below.
---
- name: Ansible playbook local_action example
hosts: linuxservers
tasks:
- name: Check the website status
local_action: command touch /tmp/local_file.ini
Output:
The above command will run on the localhost instead of running on the remote computers. So when you check the controller machine, it will create the local_file on the ansible host at /tmp location.
You can also put the local_action command as,
local_action: command touch /tmp/local_file.ini {{ inventory_hostname }}
Examples of Ansible delegate_to
Given below are the examples of Ansible delegate_to:
Example
Understanding the delegate_to task flow.
In the below example, we have the four tasks which will copy files from the ansible controller to the remote destination; in the second task, we will use the delegate_to parameter to get the URL status from the local or the delegated server only, and in the third task, we will get the output produced by the delegated task, and in the fourth task, ansible will reboot the remote machine.
With the example, we will see how the control flow works.
---
- name: Ansible playbook delegate_to Workflow Example
hosts: linuxservers
tasks:
- name: Copy the file from the Source to the destiantion
copy:
src: /tmp/myfiles/
dest: /tmp
mode: 0644
- name: Check the website status
uri:
url: https://theautomationcode.com
method: GET
delegate_to: localhost
register: http_output
- name: tasks to get the variable output
debug: var=http_output
- name: Reboot servers after update
reboot:
reboot_timeout: 300
In this example, the first, third, and fourth tasks will be run from the controller host to the mentioned servers in the file, while the delegated host will run the second task. In this case, it is localhost. Once the first task is executed, the second task will be delegated to the localhost or the specified mentioned delegated hosts, so the control is assigned to the delegated host, and when the second task completes, the control is reassigned to the controller node (that is ansible host) to run the task again on the remote inventory host if the delegate_to is not specified. So it is a two-way process.
Conclusion
Task delegations are very useful when designing the workflow for the play-book because sometimes we need to run the commands on the hosts which are not considered in the group of the inventory file so that it can run the delegated tasks and later move back to the controller host like removing servers from the load-balancers before patching which are not mentioned in the inventory file.
Recommended Articles
This is a guide to Ansible delegate_to. Here we discuss How do the Ansible delegate_to works along with the example and output. You may also look at the following articles to learn more –