The Ansible include statement

As the playbook grows in size, it will eventually become obvious that many of the tasks and playbooks can be shared across different playbooks. The Ansible include statement is similar to many Linux configuration files that just tell the machine to extend the file the same way as if the file was directly written in. We can do an include statement for both playbooks and tasks. We will look at a simple example for extending our tasks.

Let's assume that we want to show outputs for two different playbooks. We can make a separate YAML file called show_output.yml as an additional task:

    ---
- name: show output
debug:
var: output

Then, we can reuse this task in multiple playbooks, such as in chapter5_11_1.yml that looked largely identical to the last playbook with the exception of registering the output and the include statement at the end:

    ---
- name: Ansible Group and Host Varibles
hosts: localhost

tasks:
- name: create router configuration files
template:
src=./nxos.j2
dest=./{{ item.key }}.conf
with_dict: "{{ nexus_devices }}"
register: output

- include: show_output.yml

Another playbook, chapter5_11_2.yml, can reuse show_output.yml inn the same way:

    ---
- name: show users
hosts: localhost

tasks:
- name: show local users
command: who
register: output

- include: show_output.yml

Note that both playbooks use the same variable name, output, as the show_output.yml hard codes the variable name for simplicity in demonstration. You can also pass variables into the included file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.204.186