Ansible: default filter does not resolve variables in Ansible 1.8.2

Created on 19 Dec 2014  ·  3Comments  ·  Source: ansible/ansible

Issue Type:

Bug Report

Ansible Version:

1.8.2

Environment:

RHEL 5

Summary:

The default filter does not resolve variables defined in the inventory.

For example, if I have defined in my inventory file the following variable:

app_install_dir=/test

My playbook has this and "somevariable" is not defined:

dest_dir="{{ somevariable | default('{{app_install_dir}}/DATA') }}"

When running the playbook, the default fails to resolve "app_install_dir"

This used to work on Ansible 1.7.1.

Steps To Reproduce:

1) Define a variable in the inventory called called "test_directory=/tmp/testdir"
2) In the playbook, have a task to create a directory defined by variable dest={{ undefined_variable | default('{{test_directory}}') }}
3) After running the playbook, it fails to resolve the value of "test_directory"

Expected Results:

The default filter should be able to resolve variables

Actual Results:

The default filter is not resolving variables defined in inventory

Most helpful comment

I don't normally see posts in closed tickets but an irc user pinged me on this:
dest={{ undefined_variable | default(test_directory + '/subdir') }}

All 3 comments

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

dest={{ undefined_variable | default('{{test_directory}}') }}

is an invalid expression , you cannot nest {{ }}, what you probably want is the following:

dest={{ undefined_variable | default(test_directory) }}

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Thank you once again for this and your interest in Ansible!

@bcoca: Good to know, but does that mean we cannot concatenate a variable and a literal in a default ? Something like: dest={{ undefined_variable | default('{{test_directory}}/subdir') }} (but not invalid ;-)

I don't normally see posts in closed tickets but an irc user pinged me on this:
dest={{ undefined_variable | default(test_directory + '/subdir') }}

Was this page helpful?
0 / 5 - 0 ratings