In this article, we will learn some of the use-cases and advantages of Ansible local_action.

Ansible local_action module:

Consider the problem / usecase scenario where we want to run tasks on the system which runs the Ansible-playbook command. In other words, how can we run for eg. shell command on the local machine parallely while playbook is already running. Also, how can we make sure that some tasks has been fulfilled in order to run the other part of playbook on some other remote machine.The solution to the above usecase is the local_action option provided by Ansible. It takes the module name and module argument and will run that module locally.

Ansible local_action shell

The first task will run ps command (shell command) on the remote machine and to check the output of the command a register remote_process has been set.The second task as shown in above example will run command on local machine due to the local_action module being specified.

How to run local actions?

To run local actions, we just need to define the module name (i.e. local_action module name) for the command or task to run.For example:

Conclusion:

Ansible local_action is the way to make double sure that the some things are available before we jump to run on remote machine. Its like running pretest locally before running command or task on remote machine.Thats all about Ansible local_action usage and its purpose.