Pages

Monday, February 22, 2016

Ansible delegation

Share it Please
Ansible by default run the task all at once on the remote configure machine. What If a task is based on status of a certain command on another machine?. Say if you are patching a package on a machine and you need to continue until a certain file is available on another machine. This is done in Ansible using the delegation option.

By using the Ansible delegate option we can configure to run a task on a different host than the one that is being configured using the delegate_to key. The module will still run once for every machine, but instead of running on the target machine, it will run on the delegated host. The facts available will be the ones applicable to the current host. Lets write a basic playbook using Ansible delegate

[root@vx111a delegate]# cat sample-playbook.yml
---
- hosts: cent
 
  tasks:
    - name: Install zenity
      action: yum name=zenity state=installed
    - name: configure zenity
      action: template src=hosts dest=/tmp/zenity.conf
    - name: Tell Master
      action: shell echo "{{ansible_fqdn}} done" >> /tmp/list
      delegate_to: 172.16.202.97

We have defined multiple tasks to install zenity, configure zenity and the last task is to echo command on the delegate machineof different IP address.  Ansible-playbook will delegate the last task to the IP address 172.16.202.97. Once we run the playbook we can see


[root@vx111a delegate]# ansible-playbook sample-playbook.yml

PLAY [cent] *******************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [Install zenity] ********************************************************
changed: [172.16.202.96]

TASK: [configure zenity] ******************************************************
changed: [172.16.202.96]

TASK: [Tell Master] ***********************************************************
changed: [172.16.202.96 -> 172.16.202.97]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=4    changed=3    unreachable=0    failed=0  

Now check the remote machine 172.16.202.97 for the file created.

root@ubuntu:/home/vagrant# cat /tmp/list
dev.foohost.vm done


More to Come. Happy learning

4 comments :

  1. Thanks for sharing your thoughts about delegate list.
    Regards

    ReplyDelete
  2. Hurrah, that's what I was exploring for, what a information! existing here
    at this web site, thanks admin of this web site.

    ReplyDelete