Explaining the Ansible local_action

Explaining the Ansible local_action
Techiio-author
Written by Nilima PaulFebruary 3, 2022
11 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible local_action.

Introduction to Ansible local_action

In Ansible, we tend 100% of the time to deal with remote objective hubs, for which we compose plays, assignments. This is a typical arrangement that anything written in a play and assignment will be executed on remoted target has except if determined in any case. Be that as it may, commonly you get what is happening where you want to chip away at a nearby or regulator machine to create a little information or do some alteration, in the middle of a playbook or two errands in a play. In such cases we want to highlight boundaries like local_action, delegate_to, run_once are exceptionally helpful. In this article, we will procure Ansible local_action and ways of utilizing it.

Explaining the Ansible local_action

In Ansible, we tend 100% of the time to deal with remote objective hubs, for which we compose plays, assignments. This is a typical arrangement that anything written in a play and assignment will be executed on remoted target has except if determined in any case. Be that as it may, commonly you get what is happening where you want to chip away at a nearby or regulator machine to create a little information or do some alteration, in the middle of a playbook or two errands in a play. In such cases we want to highlight boundaries like local_action, delegate_to, run_once are exceptionally helpful. In this article, we will procure Ansible local_action and ways of utilizing it.

How Does Ansible local_action Works?

In an Ansible playbook, when local_action is utilized, Ansible will run the module work referenced under it on the regulator hub. Generally, modules utilized with local_action are shell and order. As you can do practically every one of the undertakings utilizing these modules on the regulator hub.

---
# ...
tasks:
- name: a sample module
local_action: command <command to execute>

But even if you want to use an Ansible module to work locally, you can use it like below: –

---
# ...
tasks:
- name: sample module local_action:
module: <module name>
<....module parameter...>
<....other feature parameters   >

We will see some of the possible ways to use it in the next section using some examples. But you must note the syntax and indentations to be used.

Examples

Presently by utilizing models, we will attempt to find out with regards to Ansible component boundary local_action alongside a few other helpful boundaries, which you may need to use in everyday activities. We will take a few models, however prior to going there, we initially comprehend our lab, we utilized for testing reason.

Here we have an Ansible regulator server named ansible-regulator and two controllers has named have one and host-two. We will make playbooks and run Ansible orders on the ansible-regulator hub and see the outcomes on remote hosts.

In this model, we have made a playbook with substance like beneath, here we are attempting to accomplish that when we use local_action, a document will be made in regulator hub and the subsequently, this will be duplicated to the remote objective

---
hosts: all tasks:
name: This will create a local file /tmp/local_file.ini.
local_action: command touch /tmp/"{{ ansible_hostname }}"_local_file.ini
name: Here we copy the local file to remote copy:
src: /tmp/{{ ansible_hostname }}_local_file.ini dest: /var/tmp/

Executing the playbook using below command: –

ansible-playbook ansible_local_action_create_file.yaml -v

Give the output like below: –

blogpost

Here we can see that when playbook ran, copied a file on remote target nodes, this file was created in the task where local_action is mentioned and this was performed on local machine viz. the Ansible controller node.

ll /tmp/*_local_file.ini

In this example, we have created a playbook with contents like below, here we are trying to achieve to do Rsync directory /tmp on the controller node to remote

---
hosts: all tasks:
name: recursively copy files in /tmp contoller node to remote local_action: command rsync -a /tmp {{ ansible_hostname }}:/tmp

Executing the playbook using below command: –

ansible-playbook ansible_local_action_rsync.yaml -v

Give the output like below: –

blogpost

Here we can see that when playbook ran, it did rsync on remote objective hubs with the regulator hub in the undertaking where local_action is referenced and performed on nearby machine viz. the Ansible regulator hub.

In this model, we have made a playbook with substance like beneath, here we are attempting to accomplish that when we are refreshing a bundle on remote machines, a mail should be shipped off an email account, with a mail body and subject, so that mail proprietor is advised of the changes. Likewise note the utilization of run_once, which make the sending letters to execute as it were

---
hosts: all tasks:
name: Send summary mail local_action:
module: mail
subject: "Summary Mail" to: root@localhost
body: we are installing or updating the httpd run_once: True
name: Update the remote machines. yum:
name: httpd state: latest

Executing the playbook using below command: –

ansible-playbook ansible_local_action_mail_update.yaml

Give the output like below: –

blogpost

Here we can see that when playbook ran, it attempted to overhaul httpd bundle on remote objective hubs and the mailing task where local_action is referenced was performed on nearby machine viz. the Ansible regulator hub. Likewise, it was performed just a single time, as we have utilized run_once.

We can really take a look at that assuming the mail was made and sent, to make sure that we have mailx utility. Utilizing this we can peruse mail like beneath: -

mailx

In this model, we have made a playbook with substance like beneath, here we are attempting to accomplish that in a group of servers working behind a heap balancer. To stay away from personal time, we make a server out of the group and update it and afterward add back to the bunch

---
hosts: web_servers tasks:
name: make a server out of LB pool
local_action: command: /script_to_take_out {{ inventory_hostname }}
name: update the relevant package yum:
name: package to update state: latest
deleagte_to: {{ inventory_hostname }}
name: add back the server to LB pool
local_action: command: /script_to_add {{ inventory_hostname }}

We can replace the above sample with real-life values and use them. The task which has delegate_to will run on the machine which is mentioned after it.

Conclusion

As we saw, utilizing local_action is a bit interesting, however it can assist you with conquering the issue of running numerous playbooks for basic undertakings that include the localhost or regulator machine alongside target remote machines. Additionally, as you found in models, utilizing it with other element boundaries like run_once makes it more reasonable to utilize. So learn it first and afterward use it.

Python
Ansible
ansib facts
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance