Ansible 2 - latest beta - a variable gets the value of another

Hi,
I am having a variable replaced with the contents of another (which has a completely different name). This is the relevant part of my task file, which installs a new version of the application I’m supporting. This part checks if we are actually going to install, and shuts it down if we are:

`

  • block:
  • debug: msg={{ customer }}

stop the running instance, if it exists (if it doesn’t exist, then the version is set to ‘not_found’)

  • name: Stop the installed domain
    include: appctl.yml action=stop
    when: “{{ ‘not_found’ != hostvars[inventory_hostname][‘app_info_’ ~ customer][‘version’] }}”
    `

This is not the whole playbook, of course. I added the “debug” line just to show how the value changes between lines (the variable being messed up here is “customer”. The output of this fragment is:

TASK [node_servers : debug msg={{ customer }}] ********************************* ok: [server.example.com] => { "changed": false, "msg": "test" } TASK [node_servers : Stop the installed instance] ******************************** included: /path/to/roles/node_servers/tasks/avpsctl.yml for server.example.com TASK [node_servers : Stop nodes] *********************************************** fatal: [server.example.com]: FAILED! => {"failed": true, "msg": "ERROR! The conditional check '{{ 'not_found' != hostvars[inventory_hostname]['app_info_' ~ customer]['version'] }}' failed. The error was: ERROR! 'dict' object has no attribute u\"app_info_{u'name': 'anotherserver.example.com', u'port': 9000}\""}

The debug task outputs the correct value (“test”), while in the templated conditional it gets changed to a dict (by the way, initially I was using “+” for concatenation and lost a couple hours with a “cannot concatenate str and dict” error…). That dict is defined earlier in the playbook, in an included var file whose path depends on “customer”, but that is where the relation ends.

I’ll try to come up with a reduced sample to demonstrate the error, in the meantime: is this a known issue? any place where to look for a solution?

Thanks and regards

OK, I have reduced my ansible code to the minimum required to trigger the bug.
It has a three-level inclusion depth, which with the reduced code may seem too much, but in the real code has a good reason to be.

I noticed that removing the ‘when’ in appctl.yml is enough to avoid the bug, but I need that (I use it with a ‘delegate_to’ clause to stop the application in all hosts where it is running)

I will also create a bug report in Github.

(attachments)

test-case.zip (3.26 KB)

You’re trying to do multiple levels of variable expansion, and that doesn’t always work.

It works in debug statements, but not necessarily in task arguments or conditionals.

There is a work around, use a lookup, the “items” lookup may get you what you want.

So, basically what you say is: what I’m trying to do doesn’t work (it doesn’t work reliably == it doesn’t work).
While I’d like to get some more details on why it doesn’t work, to make sure I don’t hit any “limit” in the future, I will try to work around this problem by converting my “appctl” task file to a module, maybe reducing the depth and levels of inclusion helps.

I reported the problem here https://github.com/ansible/ansible/issues/13506

Also was still able to reproduce the problem by removing the concatenation: replacing the “‘app_info_’ ~ customer” part with just “customer” (e.g. no extra variable expansion) produces the same error.