String to vars not working anymore in Ansible v1.8.x

118 views
Skip to first unread message

Khuong Dinh Pham

unread,
Feb 22, 2015, 8:35:39 AM2/22/15
to ansible...@googlegroups.com
Hi

In v1.7.2 It was possible to do this i a template:

{{ vars['prefix_' + {{ item.env }} + '_postfix' ] | get_something }}

But it's not working anymore in v.1.8.x. It just say that variable 'prefix_main_postfix' does not exist in dict

Can i somehow concat a string and use it as variable name in Ansible v.1.8.x?

Serge van Ginderachter

unread,
Feb 22, 2015, 8:44:43 AM2/22/15
to ansible...@googlegroups.com

On 22 February 2015 at 14:35, Khuong Dinh Pham <khuo...@gmail.com> wrote:
​​
{{ vars['prefix_' + {{ item.env }} + '_postfix' ] | get_something }}


​I'm not sure this would have worked in older versions.​ Either way, don't nest those braces, just use plain variables:

Khuong Dinh Pham

unread,
Feb 22, 2015, 1:30:42 PM2/22/15
to ansible...@googlegroups.com
Hi

Typo error. I meant

​{{ vars['prefix_' + item.env + '_postfix' ] | get_something }}

This is not working anymore in v1.8.4 If I switch back to v.1.7.2 it's working fine.

However if I type

​{{ prefix_main_postfix | get_something }} in v.1.8.4 it's working fine

Serge van Ginderachter

unread,
Feb 22, 2015, 1:38:36 PM2/22/15
to ansible...@googlegroups.com
On 22 February 2015 at 19:30, Khuong Dinh Pham <khuo...@gmail.com> wrote:
​{{ vars['prefix_' + item.env + '_postfix' ] | get_something }}

This is not working anymore in v1.8.4 If I switch back to v.1.7.2 it's working fine.

However if I type

​{{ prefix_main_postfix | get_something }} in v.1.8.4 it's working fine


​Your latter example strike me as being obvious.​
Is the vars[]dict meant to be something special?


Khuong Dinh Pham

unread,
Feb 22, 2015, 2:04:59 PM2/22/15
to ansible...@googlegroups.com
Maybe it obvious but it is just to tell you that the variable exist. We use the vars[] like this:

myvars.yml:
prefix_main_postfix: some_value


master yml:
---
- hosts: "{{ hosts }}"
  vars:
    os: "linux"
  vars_files:
    - ~/myvars.yml


task:
- template: src=mytemplate.j2 ...
    with_items:
      - { env: 'main' }
      - { env: 'staging' }


mytemplate.j2:
​{{ vars['prefix_' + item.env + '_postfix' ] | get_something }}


error:
fatal: [127.0.0.1 -> 127.0.0.1] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'prefix_main_postfix'", 'failed': True}

Jonathan Davila

unread,
Feb 23, 2015, 10:34:08 AM2/23/15
to ansible...@googlegroups.com
Try taking out the middle curly braces. So like:

{{ 'prefix_' + item.env + '_postfix' | get_something }}

If you are declaring vars with the vars construct, you don't need to reference vars as a dict, the key/values are directly available.

Let me know if that helps.

Khuong Dinh Pham

unread,
Feb 23, 2015, 3:02:54 PM2/23/15
to ansible...@googlegroups.com
Hi

It does not work because get_something can't parse the string "prefix_main_postfix" I need the value.

If I remove "| get_something" like this:

{{ 'prefix_' + item.env + '_postfix' }}

it just print the text 'prefix_main_postfix'
Reply all
Reply to author
Forward
0 new messages