I'm using variables within the inventory host name to provision environments dynamically using the ansible openstack modules and this is working well
but, when I try to use these hostnames in a jinja2 template snippet, it's giving me errors
Is there a way to make it working? Any help or references in this regard will be appreciated :-)
To replicate this kind of error, run the below play (test.yml) with the below inventory file(inventory.txt)
ansible-playbook test.yml -i inventory.txt --extra-vars="env=dev1"
The play fails with the below error :(
PLAY [openstack] ***************************************************************
TASK [setup] *******************************************************************
TASK [debug] *******************************************************************
}
TASK [debug] *******************************************************************
fatal: [myserver-{{env}}.
subdomain.mycompany.com]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.hostvars.HostVars object' has no attribute u'
myserver-dev1.subdomain.mycompany.com'\n\nThe error appears to have been in '/home/hos/test.yml': line 6, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - debug: msg=\"inventory_hostname={{inventory_hostname}}\"\n - debug: msg=\"openstack_hosts={% for host in groups['openstack'] %}{{ hostvars[host]['inventory_hostname']}}{% if not loop.last %},{% endif %}{% endfor %}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @test.retry
PLAY RECAP *********************************************************************