The Ansible include statement

As the playbook grows in size, it will eventually become obvious that many of the tasks and plays 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 use an include statement for both playbooks and tasks. Here, we will look at a simple example of extending our task.

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 chapter8_11_1.yml, which looks 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, chapter8_11_2.yml, can reuse show_output.yml in 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, because in show_output.yml, we hard coded the variable name for simplicity. 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