[Ansible] – How to define that some task into the playbook should be executed locally or in another host?

In order to configure that the task should be executed in another host use the attribute delegate_to and to define that the should be played locally use the attribute local_action. Following some examples.

---
- name: Example with delegate_to 
  hosts: databases
  tasks:
    - name: Check process
      command: ps
      delegate_to: webserver.example.com
      register: result
    - name: Output ps command 
      debug: 
        msg: "{{ result.stdout }}"

NOTE: If the attribute delegate_facts as True combined with the delegate_to will tell for the ansible looking for the facts, the system values, from the hosted mapped in the delagate_to instead of the hosts of the playbook. See more about Ansible facts here.

The same above action could be done locally as follows.

    - name: Check process
      command: ps
      delegate_to: localhost
      register: result

It could be configured by local_action. 

    - name: Check process
      local_action: command ps
      register: result

Following some references in the Ansible documentation related to this subject.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s