variable trouble - item: not returning correct value

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

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

Thanks Matt! That did it!