We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Lloyd • 6 years ago

Interesting post but I got the following error when trying this:
{"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_host'"}

Perhaps these ansible host vars have been deprecated and this post is no longer applicable.

Siyuan Liu • 6 years ago

Hi, the "ansible_host" attribute comes from your inventory file. In my example, my inventory is defined as "ubuntu1 ansible_host=192.168.0.1" (for one of the nodes) so the "ansible_host" is supplied by me. This is not some kind of built-in variable of ansible.

Leo Guzynski • 8 years ago

I could never get this to work to do an operation on a command: module; I cannot tell you all the ways I tried. Basically I was able get a list of IP address but they were all in a single element. The freenode channel suggested I should not join, or that I was double templating - but still no luck. Finally I was able to use the command module with_items: "{{ groups['all'] }}" Then in the command: ping "{{ hosvars[item]['ansible_host'] }}" ... But to give you credit - I started here and appreciate the post. It was quite helpful in getting me going even though I ended a little different.

Siyuan Liu • 8 years ago

Hi Leo, thanks for your comment.

My method here is for generating configuration files from templates (.j2 files). The end result is simply a string in a particular format that appears in the final configuration file. That's why I need join to turn a list into a single string.

I see that in your case you want to iterate through the list of IPs (instead of turning it to a single string). That of course works differently by utilizing "with_items" (like a for loop) and then accessing each item's IP address individually. Join shouldn't be used in your case.

Hope it clarifies why my method doesn't work in your case :)

praveer09 • 8 years ago

Thanks Siyuan for sharing this.

Siyuan Liu • 8 years ago

Hi Praveer, I hope you found this post useful. :)