Register variable in a loop can't access through with_items

983 views
Skip to first unread message

Michael Mallett

unread,
Apr 9, 2015, 11:34:01 AM4/9/15
to ansible...@googlegroups.com
Hi, I'm an ansible noob, and I'm following this tutorial to use the rax module to spin up and configure rackspace servers

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-02
      register: 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, 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?

Brian Coca

unread,
Apr 9, 2015, 1:57:41 PM4/9/15
to ansible...@googlegroups.com
try using debug module to see the actual structure of the returned data:

debug: var=rax




--
Brian Coca

Michael Mallett

unread,
Apr 10, 2015, 5:19:31 AM4/10/15
to ansible...@googlegroups.com
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, 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.
Reply all
Reply to author
Forward
0 new messages