InterviewSolution
Saved Bookmarks
| 1. |
How can we delegate tasks in Ansible? |
|
Answer» Task delegation is an important feature of Ansible since there MIGHT be use cases where we would want to perform a task on one host with reference to other hosts. We can do this using the delegate_to keyword. For example, if we want to MANAGE NODES in a load balancer pool we can do: - hosts: webservers serial: 5 tasks:- name: Take machine out of ELB pool ansible.builtin.command: /usr/bin/take_out_of_pool {{ inventory_hostname }} delegate_to: 127.0.0.1 - name: Actual steps would go here ansible.builtin.yum: name: acme-web-stack state: latest - name: Add machine back to ELB pool ansible.builtin.command: /usr/bin/add_back_to_pool {{ inventory_hostname }} delegate_to: 127.0.0.1We are also defining serial to control the number of hosts executing at one time. There is another shorthand syntax called local_action which can be used instead of delegate_to. ...tasks: - name: Take machine out of ELB pool local_action: ansible.builtin.command /usr/bin/take_out_of_pool {{ inventory_hostname }}...But there are few exceptions also such as include, add_host, and debug tasks that cannot be delegated. |
|