Ansible - include + with_items back in 2.0

August 21, 2015

One of the widely used(abused) feature of Ansible which was taken back in 1.6 is back in 2.0.

  • include: statement to include a file with variable name
  • include: statement with with_items

Prior to 2.0, any of these operations would always result in the following error:

---
- include: "{{ my_var }}.yml"
ERROR: file not found: /path/to/ansible/provision/{{my_var}}.yml

---
- include: "{{ item }}"
  with_items:
  - "{{ my_var }}.yml"
ERROR: [DEPRECATED]: include + with_items is a removed deprecated feature. Please update your playbooks.

With 2.0(currently devel branch), these are possible now:

include: statement to include a file with variable name

---
- hosts: all
  tasks:
  - include: "{{ my_var }}.yml"

include: statement with with_items

---
- hosts: all
  tasks:
  - include: play.yml {{ item }}
  with_items:
  - var_1
  - var_2
  - var_3

This would help in dynamically including a file based on a variable or a fact. This would also help to iterate over the same play/file with different values.

comments powered by Disqus