Docsite: misc fixes of playbooks_delegation (#71753)

pull/71787/head
Andrew Klychkov 4 years ago committed by GitHub
parent aa1f0bd062
commit 05a45f63ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,39 +18,39 @@ Some tasks always execute on the controller. These tasks, including ``include``,
Delegating tasks Delegating tasks
---------------- ----------------
If you want to perform a task on one host with reference to other hosts, use the 'delegate_to' keyword on a task. This is ideal for managing nodes in a load balanced pool or for controlling outage windows. You can use delegation with the :ref:`serial <rolling_update_batch_size>` keyword to control the number of hosts executing at one time:: If you want to perform a task on one host with reference to other hosts, use the ``delegate_to`` keyword on a task. This is ideal for managing nodes in a load balanced pool or for controlling outage windows. You can use delegation with the :ref:`serial <rolling_update_batch_size>` keyword to control the number of hosts executing at one time::
--- ---
- hosts: webservers - hosts: webservers
serial: 5 serial: 5
tasks: tasks:
- name: take out of load balancer pool - name: Take out of load balancer pool
command: /usr/bin/take_out_of_pool {{ inventory_hostname }} ansible.builtin.command: /usr/bin/take_out_of_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1 delegate_to: 127.0.0.1
- name: actual steps would go here - name: Actual steps would go here
yum: ansible.builtin.yum:
name: acme-web-stack name: acme-web-stack
state: latest state: latest
- name: add back to load balancer pool - name: Add back to load balancer pool
command: /usr/bin/add_back_to_pool {{ inventory_hostname }} ansible.builtin.command: /usr/bin/add_back_to_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1 delegate_to: 127.0.0.1
The first and third tasks in this play run on 127.0.0.1, which is the machine running Ansible. There is also a shorthand syntax that you can use on a per-task basis: 'local_action'. Here is the same playbook as above, but using the shorthand syntax for delegating to 127.0.0.1:: The first and third tasks in this play run on 127.0.0.1, which is the machine running Ansible. There is also a shorthand syntax that you can use on a per-task basis: ``local_action``. Here is the same playbook as above, but using the shorthand syntax for delegating to 127.0.0.1::
--- ---
# ... # ...
tasks: tasks:
- name: take out of load balancer pool - name: Take out of load balancer pool
local_action: command /usr/bin/take_out_of_pool {{ inventory_hostname }} local_action: ansible.builtin.command /usr/bin/take_out_of_pool {{ inventory_hostname }}
# ... # ...
- name: add back to load balancer pool - name: Add back to load balancer pool
local_action: command /usr/bin/add_back_to_pool {{ inventory_hostname }} local_action: ansible.builtin.command /usr/bin/add_back_to_pool {{ inventory_hostname }}
You can use a local action to call 'rsync' to recursively copy files to the managed servers:: You can use a local action to call 'rsync' to recursively copy files to the managed servers::
@ -58,11 +58,10 @@ You can use a local action to call 'rsync' to recursively copy files to the mana
# ... # ...
tasks: tasks:
- name: recursively copy files from management server to target - name: Recursively copy files from management server to target
local_action: command rsync -a /path/to/files {{ inventory_hostname }}:/path/to/target/ local_action: ansible.builtin.command rsync -a /path/to/files {{ inventory_hostname }}:/path/to/target/
Note that you must have passphrase-less SSH keys or an ssh-agent configured for this to work, otherwise rsync Note that you must have passphrase-less SSH keys or an ssh-agent configured for this to work, otherwise rsync asks for a passphrase.
will need to ask for a passphrase.
To specify more arguments, use the following syntax:: To specify more arguments, use the following syntax::
@ -72,7 +71,7 @@ To specify more arguments, use the following syntax::
tasks: tasks:
- name: Send summary mail - name: Send summary mail
local_action: local_action:
module: mail module: community.general.mail
subject: "Summary Mail" subject: "Summary Mail"
to: "{{ mail_recipient }}" to: "{{ mail_recipient }}"
body: "{{ mail_body }}" body: "{{ mail_body }}"
@ -85,17 +84,17 @@ The `ansible_host` variable reflects the host a task is delegated to.
Delegating facts Delegating facts
---------------- ----------------
Delegating Ansible tasks is like delegating tasks in the real world - your groceries belong to you, even if someone else delivers them to your home. Similarly, any facts gathered by a delegated task are assigned by default to the `inventory_hostname` (the current host), not to the host which produced the facts (the delegated to host). To assign gathered facts to the delegated host instead of the current host, set `delegate_facts` to `True`:: Delegating Ansible tasks is like delegating tasks in the real world - your groceries belong to you, even if someone else delivers them to your home. Similarly, any facts gathered by a delegated task are assigned by default to the `inventory_hostname` (the current host), not to the host which produced the facts (the delegated to host). To assign gathered facts to the delegated host instead of the current host, set ``delegate_facts`` to ``true``::
--- ---
- hosts: app_servers - hosts: app_servers
tasks: tasks:
- name: gather facts from db servers - name: Gather facts from db servers
setup: ansible.builtin.setup:
delegate_to: "{{item}}" delegate_to: "{{ item }}"
delegate_facts: True delegate_facts: true
loop: "{{groups['dbservers']}}" loop: "{{ groups['dbservers'] }}"
This task gathers facts for the machines in the dbservers group and assigns the facts to those machines, even though the play targets the app_servers group. This way you can lookup `hostvars['dbhost1']['ansible_default_ipv4']['address']` even though dbservers were not part of the play, or left out by using `--limit`. This task gathers facts for the machines in the dbservers group and assigns the facts to those machines, even though the play targets the app_servers group. This way you can lookup `hostvars['dbhost1']['ansible_default_ipv4']['address']` even though dbservers were not part of the play, or left out by using `--limit`.
@ -107,7 +106,7 @@ Local playbooks
It may be useful to use a playbook locally on a remote host, rather than by connecting over SSH. This can be useful for assuring the configuration of a system by putting a playbook in a crontab. This may also be used It may be useful to use a playbook locally on a remote host, rather than by connecting over SSH. This can be useful for assuring the configuration of a system by putting a playbook in a crontab. This may also be used
to run a playbook inside an OS installer, such as an Anaconda kickstart. to run a playbook inside an OS installer, such as an Anaconda kickstart.
To run an entire playbook locally, just set the "hosts:" line to "hosts: 127.0.0.1" and then run the playbook like so:: To run an entire playbook locally, just set the ``hosts:`` line to ``hosts: 127.0.0.1`` and then run the playbook like so::
ansible-playbook playbook.yml --connection=local ansible-playbook playbook.yml --connection=local

Loading…
Cancel
Save