variable trouble - item: not returning correct value

44 views
Skip to first unread message

David Neudorfer

unread,
May 2, 2014, 4:26:25 PM5/2/14
to ansible...@googlegroups.com
I need to reference the variable hostvars['{{ item }}'].rax_accessipv4 in a task but I'm getting the node name and not the IP address.

In the task rackspace_clb | Add servers to Load balancer I need address: to be the IP address displayed in rackspace_clb | debug hostvars.

Thank you.

- name: debug hostvars {{ groups.web5 }}
  debug:
    var: hostvars['{{ item }}'].rax_accessipv4
  with_items: groups.web5

- name: Add servers to Load balancer
  rax_clb_nodes:
    load_balancer_id: "{{ lb.balancer.id }}"
    address: hostvars['{{ item }}'].rax_accessipv4
    port: 80
    condition: enabled
    type: primary
    wait: yes
    region: ORD
  with_items: groups.web5


TASK: [rackspace_clb | debug hostvars {{groups.web5}}] ************************ 
ok: [127.0.0.1] => (item=test5-web02) => {
    "hostvars['test5-web02'].rax_accessipv4": "23.253.194.158", 
    "item": "test5-web02"
}
ok: [127.0.0.1] => (item=test5-web01) => {
    "hostvars['test5-web01'].rax_accessipv4": "23.253.194.156", 
    "item": "test5-web01"
}

TASK: [rackspace_clb | Add servers to Load balancer] ************************** 
<127.0.0.1> REMOTE_MODULE rax_clb_nodes condition=enabled type=primary address='hostvars['"'"'test5-web02'"'"'].rax_accessipv4' load_balancer_id=218421 region=ORD
<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1399061971.04-220974670688213 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1399061971.04-220974670688213 && echo $HOME/.ansible/tmp/ansible-tmp-1399061971.04-220974670688213']
<127.0.0.1> PUT /tmp/tmpfo3nrV TO /home/david.neudorfer/.ansible/tmp/ansible-tmp-1399061971.04-220974670688213/rax_clb_nodes
<127.0.0.1> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /home/david.neudorfer/ansible/venv/bin/python /home/david.neudorfer/.ansible/tmp/ansible-tmp-1399061971.04-220974670688213/rax_clb_nodes; rm -rf /home/david.neudorfer/.ansible/tmp/ansible-tmp-1399061971.04-220974670688213/ >/dev/null 2>&1']
failed: [127.0.0.1] => (item=test5-web02) => {"failed": true, "item": "test5-web02"}
msg: Validation Failure
<127.0.0.1> REMOTE_MODULE rax_clb_nodes condition=enabled type=primary address='hostvars['"'"'test5-web01'"'"'].rax_accessipv4' load_balancer_id=218421 region=ORD
<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1399061972.43-17587149073179 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1399061972.43-17587149073179 && echo $HOME/.ansible/tmp/ansible-tmp-1399061972.43-17587149073179']
<127.0.0.1> PUT /tmp/tmpDj36JB TO /home/david.neudorfer/.ansible/tmp/ansible-tmp-1399061972.43-17587149073179/rax_clb_nodes
<127.0.0.1> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /home/david.neudorfer/ansible/venv/bin/python /home/david.neudorfer/.ansible/tmp/ansible-tmp-1399061972.43-17587149073179/rax_clb_nodes; rm -rf /home/david.neudorfer/.ansible/tmp/ansible-tmp-1399061972.43-17587149073179/ >/dev/null 2>&1']
failed: [127.0.0.1] => (item=test5-web01) => {"failed": true, "item": "test5-web01"}
msg: Validation Failure

Matt Martz

unread,
May 2, 2014, 4:29:59 PM5/2/14
to ansible...@googlegroups.com, David Neudorfer
I think you just need to adjust some jinja syntax:

- name: debug hostvars {{ groups.web5 }}
  debug:
    var: hostvars[item].rax_accessipv4
  with_items: groups.web5

- name: Add servers to Load balancer
  rax_clb_nodes:
    load_balancer_id: "{{ lb.balancer.id }}"
    address: "{{ hostvars[item].rax_accessipv4 }}"
    port: 80
    condition: enabled
    type: primary
    wait: yes
    region: ORD
  with_items: groups.web5

-- 
Matt Martz
ma...@sivel.net
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e7eea46d-625b-4a00-8b7c-7d4d0127c7c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Neudorfer

unread,
May 2, 2014, 4:32:50 PM5/2/14
to ansible...@googlegroups.com, David Neudorfer
Thanks Matt! That did it!
Reply all
Reply to author
Forward
0 new messages