How to execute a particular task in different machine in ansible?

How to execute a particular task in different machine in ansible?

By default, Ansible executes all tasks on the machines that match the hosts line of our ansible playbook.

Here we see how to delegate tasks to a different machine for executing particular task in ansible playbook.

Some tasks always execute on the machine mentioned in the hosts line of the playbook examples including include, add_host, and debug, cannot be delegated.

Lets see a sample example to execute a particular task in a different machine using delegate_to in ansible.

This ansible playbook runs a ping command in a different machine '172.169.41.50" than hosts line local host.

- hosts: localhost
  gather_facts: no

  tasks:

	- name: initialize command
  	set_fact:
    	cmd_str: "ping -c 4 google.com"

	- name: "Executing {{ cmd_str }} in different machine"
  	delegate_to: 172.169.41.50
  	command: "{{ cmd_str }}"
  	register: out

	- name: Print the msg
  	debug:
    	msg: "{{ out.stdout }}"

Output:

$ ansible-playbook ping_test.yml

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize command] ***************************************************************************************************************************************************
ok: [localhost]

TASK [Executing ping -c 4 google.com in different machine] ********************************************************************************************************************
[DEPRECATION WARNING]: Distribution centos 8.4 on host localhost should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with prior
 Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [localhost -> 172.169.41.50]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "PING google.com (122.117.129.142) 56(84) bytes of data.\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=1 ttl=53 time=97.7 ms\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=2 ttl=53 time=98.3 ms\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=3 ttl=53 time=97.7 ms\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=4 ttl=53 time=97.7 ms\n\n--- google.com ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3007ms\nrtt min/avg/max/mdev = 97.670/97.842/98.251/0.237 ms"
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=3	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

This playbook is worked as expected.

Executed ping command in different machine and displayed result in localhost machine.

Executing same command in different machines in ansible?

This ansible playbook runs same command in the machine mentioned in hosts line and also in different machine which is mentioned using delegate_to.

- hosts: localhost
  gather_facts: no

  tasks:

	- name: initialize command
  	set_fact:
    	cmd_str: "ping -c 4 google.com"

	- name: "Executing {{ cmd_str }} in localhost machine"
  	command: "{{ cmd_str }}"
  	register: out

	- name: Print the msg
  	debug:
    	msg: "{{ out.stdout }}"

	- name: "Executing {{ cmd_str }} in another machine"
  	delegate_to: 172.169.41.50
  	command: "{{ cmd_str }}"
  	register: out

	- name: Print the msg
  	debug:
    	msg: "{{ out.stdout }}"

Output:

$ ansible-playbook ping_test.yml

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize command] ***************************************************************************************************************************************************
ok: [localhost]

TASK [Executing ping -c 4 google.com in localhost machine] ******************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "PING google.com (127.171.151.206) 56(84) bytes of data.\n64 bytes from adad-in-ssd.1e100.net (127.171.151.206): icmp_seq=1 ttl=53 time=4.34 ms\n64 bytes from adad-in-ssd.1e100.net (127.171.151.206): icmp_seq=2 ttl=53 time=4.85 ms\n64 bytes from adad-in-ssd.1e100.net (127.171.151.206): icmp_seq=3 ttl=53 time=4.45 ms\n64 bytes from adad-in-ssd.1e100.net (127.171.151.206): icmp_seq=4 ttl=53 time=4.46 ms\n\n--- google.com ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3003ms\nrtt min/avg/max/mdev = 4.343/4.526/4.847/0.191 ms"
}

TASK [Executing ping -c 4 google.com in another machine] ********************************************************************************************************************
[DEPRECATION WARNING]: Distribution centos 8.4  on host localhost should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with prior
 Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [localhost -> 172.169.41.50]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "PING google.com (122.117.129.142) 56(84) bytes of data.\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=1 ttl=53 time=97.6 ms\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=2 ttl=53 time=98.3 ms\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=3 ttl=53 time=97.8 ms\n64 bytes from idad-in-sewsd.1e32100.net (122.117.129.142): icmp_seq=4 ttl=53 time=97.6 ms\n\n--- google.com ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3012ms\nrtt min/avg/max/mdev = 97.587/97.817/98.306/0.425 ms"
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=5	changed=2	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0

We can see the difference in the command execution result in both machines.




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^