# Register variable in a loop can't access through with\_items

**URL:** <https://forum.ansible.com/t/register-variable-in-a-loop-cant-access-through-with-items/17942>\
**Category:** Ansible Project\
**Created:** [April 9, 2015, 2:12pm UTC](https://forum.ansible.com/t/register-variable-in-a-loop-cant-access-through-with-items/17942 "2015-04-09T14:12:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mallett](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Michael\_Mallett](https://forum.ansible.com/u/Michael_Mallett)\
**Post date:** [April 9, 2015, 2:12pm UTC](https://forum.ansible.com/t/register-variable-in-a-loop-cant-access-through-with-items/17942/1 "2015-04-09T14:12:47Z")

</div>

Hi, I’m an ansible noob, and I’m following this tutorial to use the rax module to spin up and configure rackspace servers  
[http://nicholaskuechler.com/2015/01/09/build-rackspace-cloud-servers-ansible-virtualenv/#comment-550108](http://nicholaskuechler.com/2015/01/09/build-rackspace-cloud-servers-ansible-virtualenv/#comment-550108)

It’s great but it’s not dynamic, as you can seemingly only add one server to the dynamic inventory. So I altered it to add servers through with\_items like so

tasks:

- name: Rackspace cloud server build request  
local\_action:  
module: rax  
credentials: “{{ credentials }}”  
name: “{{ item }}”  
flavor: “{{ flavor }}”  
image: “{{ image }}”  
region: “{{ region }}”  
files: “{{ files }}”  
wait: yes  
state: present  
networks:
- private
- public  
with\_items:
- server-app-01
- server-app-02register: rax

But the problem is that I can’t access these variables to add them to the dynamic inventory. I tried doing what the documentation said and taking into account the new ‘results’ key in the object, but nothing I do results in it correctly targeting the object value I want. This, to me, is the most logical attempt I have made for the results array, (which you can a stripped out version of [here](http://pastebin.com/NRvM7anS), these servers no longer exist)

- name: Add each cloud server to host group  
local\_action:  
module: add\_host  
hostname: “{{ item.instances.name }}”  
ansible\_ssh\_host: “{{ item.instances.rax\_accessipv4 }}”  
ansible\_ssh\_user: root  
groupname: deploy  
with\_items: rax.results

This one results in  
One or more undefined variables: ‘list object’ has no attribute ‘rax\_accessipv4’

I have tried using rax.results.instances and then using item.name etc, but this results in  
One or more undefined variables: ‘unicode object’ has no attribute ‘rax\_accessipv4’

I’ve tried too many to list here, for a very long time, but I’ve obviously got confused with how this is supposed to work, can I get some help please?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 9, 2015, 5:57pm UTC](https://forum.ansible.com/t/register-variable-in-a-loop-cant-access-through-with-items/17942/2 "2015-04-09T17:57:36Z")

</div>

try using debug module to see the actual structure of the returned data:

debug: var=rax

---

<div class="post-metadata">

**Author:** ![Michael\_Mallett](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Michael\_Mallett](https://forum.ansible.com/u/Michael_Mallett)\
**Post date:** [April 10, 2015, 9:19am UTC](https://forum.ansible.com/t/register-variable-in-a-loop-cant-access-through-with-items/17942/3 "2015-04-10T09:19:30Z")

</div>

Hi Brian, thanks for the response.

Perhaps I wasn’t clear in my answer but I actually posted a pastebin link in my second paragraph that shows the data structure output from debug. I stripped out some sensitive information but it is the same structurally.

“This, to me, is the most logical attempt I have made for the results array, (which you can a stripped out version of [here](http://pastebin.com/NRvM7anS), these servers no longer exist)”

Fortunately someone on stack overflow read my question and offered a solution. I don’t fully understand why it uses an array key tbh, but I will look into that on my own.  
[http://stackoverflow.com/questions/29542433/cant-access-register-variable-in-a-loop/29544680#29544680](http://stackoverflow.com/questions/29542433/cant-access-register-variable-in-a-loop/29544680#29544680)
