Hello,
I’m trying to loop over a set of host, get their IP and then append the result to a file.
Currently the action looks like this:
- name: build rings
command: swift-ring-builder {{ item.service }}.builder add z1-{{ hostvars[inventory_hostname][“ansible_bond1.2108”].ipv4.address }}:{{ item.port }}/sdb1 100
chdir=/etc/swift
with_items:
- { service: ‘account’, port: ‘6002’ }
- { service: ‘container’, port: ‘6001’ }
- { service: ‘object’, port: ‘6000’ }
- groups.storages
Basically I’d like to look over ‘groups.storages’ in {{ hostvars[inventory_hostname][“ansible_bond1.2108”].ipv4.address }.
Is it doable?
This looks like this https://coderwall.com/p/w5o6eq, althought it doesn’t seem to work with the new convention “{{ }}”
Thanks in advance :).
Cheers!
Seems like you might mean with_nested seeing you are looping over what appears to be two different lists.
Or else “groups.storages” is something else. That looks like the odd one out to me.
Everything else looks fine, so when you say “doesn’t seem to work”, more info would be helpful about how it was not working?
Thanks!
Hello Michael,
Thanks for your response.
What I’m trying to achieve is exactly what is described in this post: https://coderwall.com/p/w5o6eq
As an example, I tried the following:
- name: iterate over hosts
command: echo {{ hostvars.{{ item }}.ansible_hostname }}
with_items:
- groups.storages
ignore_errors: true
tags: gz
As far as I understand this is supposed to return every hostname, instead I get:
changed: [ceph001.enocloud.com] => (item=groups.storages) => {“changed”: true, “cmd”: [“echo”, “{{hostvars.{{item}}.ansible_hostname}}”], “delta”: “0:00:00.003401”, “end”: “2014-04-17 10:11:03.764429”, “item”: “groups.storages”, “rc”: 0, “start”: “2014-04-17 10:11:03.761028”, “stderr”: “”, “stdout”: “{{hostvars.{{item}}.ansible_hostname}}”}
Any idea? Furthermore, the final goal is to collect the ip address of the following intertace: ansible_bond1.2108. Not sure if it’s reachable given this: https://github.com/ansible/ansible/issues/6879
Thanks for your help.
Got it! This is the problem line:
command: echo {{ hostvars.{{ item }}.ansible_hostname }}
It should look like this:
{{ hostvars[item].ansible_hostname }}
Basically templates don’t nest, and inside of template expressions, you can just use variables mostly as if they were Python.
Hope that helps!
Thanks! This is getting better and better:
Modifying my playbook like this:
- name: iterate over hosts
command: ping -c 1 {{ hostvars[item][“ansible_bond1.2108”].ipv4.address }}
with_items: groups.storages
ignore_errors: true
tags: gz
Now I get:
< TASK: swift-proxy | iterate over hosts >
“ansible_bond1.2108”
This part needs quotes around it.
I’m confused, quotes are already there.
command: ping -c 1 {{ hostvars[item][“ansible_bond1.2108”].ipv4.address }}
Try:
{{ hostvars[item][‘ansible_bond1.2108’][‘ipv4’][‘address’] }}
Could you try instead of command module something like:
debug: msg=“{{ hostvars[item][‘ansible_bond1.2108’][‘ipv4’][‘address’] }}”
And run ansible-playbook with -vvvv.
Thanks for you help, this is what I got:
http://pastebin.com/qfLyS9yi
Cheers.
You didn’t understand me, I would like you to replaced the whole “command: ping -c 1 {{ hostvars[item][“ansible_bond1.2108”].ipv4.address }}” line with the debug line I wrote in my previous reply.
Arf sorry, I read too fast.
http://pastebin.com/H1Vzmb7w
I don’t understand, it’s like it is not recognizing the hostvars keyword?
Had the same problem. Managed to solve it using this way to access the value:
debug: msg=“{{ hostvars[item][‘ansible_default_ipv4’][‘address’] }}”
with_items: groups.web_servers