Pages

Thursday, February 11, 2016

Ansible – Local Action

Share it Please

There are certain cases where we want tasks to run the same machine that runs the Ansible-playbook command. Consider a case where you want to run shell command on the local machine simultaneously when the playbook is running. Also we can make sure that certain tasks needs to be fulfilled in order to run the other parts of the playbook on remote machine. That is we can make sure that a file is available on local machine before running tasks on the remote machine.

The local_action option provided by Ansible comes into the picture in such situations.If you pass the module name and the module argument to local_action, it will run that module locally. Let’s see an example of using local_action in playbook,

[root@vx111a local_action]# 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 }}"

The above is the playbook that I will be using for local_action. If we the above playbook we can see that we have 2 taks

- name: remote running process
   shell: ps
   register: remote_process

This task is for running PS ( shell ) command will be ran on the remote machine. A register remote_process was set for this to check the output

 - name: Running Local Process
   local_action: shell ps
   register: local_process

This is the second task that will run only on the local machine due to the local_action module that we specified. In order to run local actions we need to define the local_action module for the command or task to run.

When we execute the playbook we can see,

[root@vx111a local_action]# 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  

We can see that both outputs are different as one ran on the Remote Machine and the other one on the local machine.

This helps in making sure that some things are available before running on remote machine.

3 comments :

  1. I love looking through an article that will make people think.
    Also, thank you for allowing me to comment!

    ReplyDelete