Saturday, August 13, 2016

Ansible local action

Originally published by me in another blog as Ansible local_action and reproduced here for larger audience. This is also an important interview questions on Ansible.

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.



Example:





cat local_action.yml

---

- hosts: dev

  tasks:

    - name: remote running process

      shell: ps

      register: remote_process



    - debug: msg="{{ remote_process.stdout }} "



    - name: Running Local Process

      local_action: shell ps

      register: local_process



    - debug: msg="{{ local_process.stdout }}"

 

Consider this example, which is the playbook I will be using for local_action. If we run the above playbook, we can see that we have two tasks.




- name: remote running process

   shell: ps

   register: remote_process



 - name: Running Local Process

   local_action: shell ps

   register: local_process

 


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:





ansible-playbook local_action.yml



PLAY [dev] ********************************************************************



GATHERING FACTS ***************************************************************

ok: [172.16.202.96]



TASK: [remote running process] ************************************************

changed: [172.16.202.96]



TASK: [debug msg="{{ remote_process.stdout }} "] ******************************

ok: [172.16.202.96] => {

    "msg": "  PID TTY          TIME CMD\n12775 pts/1    00:00:00 sh\n12776 pts/1    00:00:00 python\n12777 pts/1    00:00:00 ps "

}



TASK: [Running Local Process] *************************************************

changed: [172.16.202.96 -> 127.0.0.1]



TASK: [debug msg="{{ local_process.stdout }}"] ********************************

ok: [172.16.202.96] => {

    "msg": "  PID TTY          TIME CMD\n24362 pts/0    00:00:02 bash\n30565 pts/0    00:00:00 ansible-playboo\n30587 pts/0    00:00:00 sh\n30588 pts/0    00:00:00 python\n30593 pts/0    00:00:00 ps"

}



PLAY RECAP ********************************************************************

172.16.202.96              : ok=5    changed=2    unreachable=0    failed=0 





Analysis:

If we see the above command result, we find that the both output differ from each other because one is being run on local machine and another being run on remote machine.


Ansible local_action sudo:

If we want to make anisible use the remote user for local_action, use the -u option while running playbook. When it uses the user to sudo, it will ask for sudo password




ansible-playbook -i <inventory> ansible/local_action.yml -u <local user that can sudo with password> --ask-sudo-pass



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.

That's all about Ansible local_action usage and its purpose.