FreeKB - delegate_to module (run task on specific server)
Ansible - delegate_to module (run task on specific server)

This assumes you have installed Ansible, are familiar with the ansible-playbook command and have created a playbook.

The delegate_to module configures Ansible to run a task on a specific server. For example, let's examine using the shell module to ping. By default, Ansible uses SSH to connect to the target server(s).

 

With this default configuration, if you were to run the following playbook, an SSH connection would be made to each target server, and then each target server would ping itself. That's probably not what you want.

---
- hosts: all
  gather_facts: false
  tasks:
    - name: ping command
      shell: "ping -c4 {{ inventory_hostname }} "

 

The delegate_to module can be used to tell Ansible to invoke the shell ping command on localhost (that's the Ansible server).

---
- hosts: all
  gather_facts: false
  tasks:
    - name: ping command
      delegate_to: localhost
      shell: "ping -c4 {{ inventory_hostname }} "

 

In this scenario, instead of using SSH, an ICMP echo would be sent from the Ansible server to the hosts, since ping uses ICMP.

 

Running this playbook will return something like this. Notice that the ping task contains -> localhost, meaning that the task was run on localhost.

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]
ok: [server2.example.com]
ok: [server3.example.com]
ok: [server4.example.com]
ok: [server5.example.com]

TASK [ping]
ok: [server1.example.com -> localhost]
ok: [server2.example.com -> localhost]
ok: [server3.example.com -> localhost]
ok: [server4.example.com -> localhost]
ok: [server5.example.com -> localhost]

PLAY RECAP
server1.example.com   : ok=2  changed=0  unreachable=0  failed=0
server2.example.com   : ok=2  changed=0  unreachable=0  failed=0
server3.example.com   : ok=2  changed=0  unreachable=0  failed=0
server4.example.com   : ok=2  changed=0  unreachable=0  failed=0
server5.example.com   : ok=2  changed=0  unreachable=0  failed=0

 

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 680e5 in the box below so that we can be sure you are a human.




Comments