I cannot find documentation needed to clean up before upgrading to 1.6. Searching 'first_available_file' on http://docs.ansible.com, shows 4 results, but the pages themselves do not contain the search term.
PROBLEM: In the playbook below, Ansible 1.4.5 shows 'DEPRECATION WARNING' when I use $item, and 'undefined variables' when in use {{item}}.
- name: build and write /etc/hosts file
action: template src=templates/$item dest=/etc/hosts owner=root group=root mode=0644
first_available_file:
- templates/etc-hosts.{{ansible_hostname}}.j2
- templates/etc-hosts.j2
[DEPRECATION WARNING]: Legacy variable substitution, such as using ${foo} or
$foo instead of {{ foo }} is currently valid but will be phased out and has
been out of favor since version 1.2. This is the last of legacy features on our
deprecation list. You may continue to use this if you have specific needs for
now. This feature will be removed in version 1.6. Deprecation warnings can be
disabled by setting deprecation_warnings=False in ansible.cfg.
How do I refactor this?
{{item}} is the right syntax
undefined variables is another problem, is it possible you get no match
for first_available_file?
You might want to use with_first_found instead of the
older first_available_file though, then if no match, the task would get
skipped.
Serge
Sililar to the example on http://docs.ansible.com/playbooks_loops.html
- name: build and write /etc/hosts file
action: template src=templates/{{ item }} dest=/etc/hosts owner=root group=root mode=0644
with_first_found:
- “etc-hosts.{{ansible_hostname}}.j2”
- “etc-hosts.j2”
Ansible 1.4.5 fails with the newer ‘with_first_found’
fatal: [backend] => {‘msg’: ‘One or more items failed.’, ‘failed’: True, ‘changed’: False, ‘results’: [{‘msg’: ‘unable to read /home/bas/code/iwelcome-ansible/playbooks/hosts/templates/None’, ‘failed’: True}]}
When I replace ‘with_first_found’ with ‘first_available_file’ it fails in a different way:
fatal: [backend] => One or more undefined variables: ‘item’ is undefined
Yep, first_available_file is quite deprecated, use “with_first_found” instead, which is the replacement in all recent versions of Ansible.
That’s probably why you aren’t seeing any references to it. I’m not sure what was going on with search.
Since which version has that been deprecated and when did the behaviour change? I’m running 1.4.5 and cannot get rid of the deprecation warnings because “with_first_found”, in 1.4.5, cannot iterate over a file not found.
Expected behaviour mentioned by Serge “then if no match, the task would get skipped.”
Observed behaviour when there is no template named etc-hosts.backend.j2, then it crashes with:
fatal: [backend] => One or more undefined variables: ‘item’ is undefined
If there is no alternative construct that works in 1.4.5, then there is a problem in the backward-compatibility period because the deprecation cannot be fixed before upgrade.
"Since which version has that been deprecated and when did the behaviour change? I’m running 1.4.5 and cannot get rid of the deprecation warnings because “with_first_found”, in 1.4.5, cannot iterate over a file not found. "
I’m not sure what you mean about a behavior change, but first_available_file is still part of the software.
““with_first_found”, in 1.4.5, cannot iterate over a file not found.”
Not sure what this means, because it finds the first file it can find, and will use that one, just like the other. Can you explain more?
It is in Ansible 1.4.4 actually, I created a repo for this that you can clone and ‘make’ on OSX with XCode, Vagrant and Virtualbox.
The repo is 100 lines of code to provision a VM with Ansible demonstrating the issue in two commands:
git clone git@github.com:datasmid/first_available_file.git
make
More on https://github.com/datasmid/first_available_file