Ansible 2.5 is not accepting dynamic hosts variable

Hi,

I have upgraded to Ansible 2.5 right now and facing an issue with variable hosts such as:


---
- hosts: "{{ dbhosts }}"
 gather_facts: false
 vars_prompt:
 - name: dbhosts
 prompt: "Which hosts would you like to run?"
 private: no
 default: all

 tasks:
 - debug:
 var: hostvars


`
ERROR! The field ‘hosts’ has an invalid value, which includes an undefined variable. The error was: ‘dbhosts’ is undefined The error appears to have been in ‘/ansible/playbooks/tester.yaml’: line 3, column 3, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: — - hosts: “{{ dbhosts }}” ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance: with_items: - {{ foo }}

`


The same playbook was running fine with Ansible 2.4.
Is there any specific change that I should follow in Ansible 2.5?
Please advise. 

Thanks & Regards.

Did you ever resolve this issue? Or find an acceptable workaround?

I have the same issue and that is a core part of many of our playbooks.

DP

I suggest making ‘dbhosts’ a group name in your inventory, then you can have

hosts: dbhosts

in your playbook, and its settled that what happens in that playbook will only happen on hosts in the group ‘dbhosts’

If you still need to set hosts dynamically, consider defaulting, like this

hosts: “{{dbhosts|default(‘dbhosts’)”

That way you can override the default hosts by passing -e ‘dbhosts=some-other-host’ to your ansible-playbook command, if you need to.

Hope this helps,

Jon

Thanks for the reply. I found the issue and it is a bug in 2.5.0 and was reported here: https://github.com/ansible/ansible/issues/37984

The fix is also linked in the bug report at: https://github.com/ansible/ansible/pull/38000. I pulled the fixed file and my install of 2.5.0 is now working as expected in terms of vars_prompt.

Thanks for the help.

dp