I'm looking for a way to conditionally include a file in an Ansible play, if the file exists. Using "include" unfortunately throws a fatal error if the file doesn't exist. I'm looping through a bunch of packages and installing them and I want to check for an optional config file for each package. See simplified example below:
---
- name: Basic setup of an Ubuntu box
hosts: all
vars:
packages:
- ack-grep
- vim
- zsh
- htop
- openssh-server
- cowsay
tasks:
- name: Run package configuration
action: apt name=$item
include: "packages/${item}.yml"
with_items: $packages
As soon as the script tries to include a file that doesn't exist, it stops with an error. I'm sure I'm just trying to do something in the wrong way, but I've been at this for hours and tried everything I can think of, with no results.
You shouldn't do that as it is described on https://github.com/ansible/ansible/issues/2726 Take a special look at this comment:
Yeah include + with_items is not something folks should use.
We should not have documented it.
While I understand you have that particular structure in your variables now, it's better to use with_items inside the task.
It doesn't work with inventory variables, so this is why you want to put loops in your tasks/roles instead.
It is not even supported on Ansible 1.5. If you try to run it, you will get this error:
ERROR: [DEPRECATED]: include + with_items is a removed deprecated feature. Please update your playbooks. Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.
The best way to use include other variables and tasks on your playbook is using roles. Take a look at http://docs.ansible.com/playbooks_roles.html#task-include-files-and-encouraging-reuse