ruk·si

⚙️ Ansible
Includes

Updated at 2017-03-22 01:46

Includes are usually used to create re-usable collections of tasks.

---
# tasks/foo.yml
- name: placeholder foo
  command: /bin/foo
- name: placeholder bar
  command: /bin/bar
tasks:
  - include: tasks/foo.yml

Includes can be parametrized.

tasks:
  - include: wordpress.yml wp_user=timmy
  - include: wordpress.yml wp_user=alice
  - include: wordpress.yml wp_user=bob
# And now you can use {{ wp_user }} in `the wordpress.yml`

You can also include playbooks if you use include at the root level. But this style doesn't support variable substitution.

- hosts: all
  remote_user: root
  tasks:
  - name: say hi
    tags: foo
    shell: echo "hi..."
- include: load_balancers.yml
- include: webservers.yml
- include: dbservers.yml

Task includes can be dynamic. Playbook includes cannot.

- include: foo.yml param={{item}}
  with_items:
  - 1
  - 2
  - 3

Sources