Can't access register variable in a loop

891 views Asked by At

I've been following this example playbook to create rackspace servers using Ansible

http://nicholaskuechler.com/2015/01/09/build-rackspace-cloud-servers-ansible-virtualenv/

Which works great, but only works on one server at a time, so I am trying to make it more dynamic, using with_items to loop through the number of servers I want to build

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

This creates the servers fine, but when I try and add this to the deploy group using the method in the link, I get an error, as expected as now there is a 'results' key I"ve tried all kinds of ways to try and target this in the way that I perceive the documentation to allude to:

- name: Add new cloud server to host group
    local_action:
        module: add_host
        hostname: "{{ item.success.name }}"
        ansible_ssh_host: "{{ item.success.rax_accessipv4 }}"
        ansible_ssh_user: root
        groupname: deploy
    with_items: rax.results

(I’ve also tried many other kinds of ways to target this) But I get ‘One or more undefined variables: ‘list object’ has no attribute ‘rax_accessipv4”

This is a stripped down version of the object I get back from rax, through debug. These servers don't exist anymore. http://pastebin.com/NRvM7anS

Can anyone tell me where I'm going wrong I'm starting to go a bit mad

2

There are 2 answers

2
Kashyap On BEST ANSWER

If you notice the type of rax.results.success is list.

So this: hostname: "{{ item.success.name }}"

should be

  • hostname: "{{ item.success[0].name }}" or
  • hostname: "{{ item['success'][0]['name'] }}"

.

{
            "changed": true,
            "msg": "All items completed",
            "results": [
                {
                    "instances": [
                        {
                            "name": "server-app-01",
                            "rax_accessipv4": "134.213.51.171",
                            "rax_accessipv6": "2a00:1a48:7808:101:be76:4eff:fe08:5251",
                        }
                    ],
                    "item": "server-app-01",
                    "success": [
                        {
                            "name": "server-app-01",
                            "rax_accessipv4": "134.213.51.171",
                            "rax_accessipv6": "2a00:1a48:7808:101:be76:4eff:fe08:5251",
                        }
                    ],
                    "timeout": []
                },
          ......
}
3
Matt Darby On

I was just wrestling with this Friday. Here is my solution:

---
- name: Provision rackspace webheads
  hosts: localhost
  gather_facts: false
  max_fail_percentage: 10
  tasks:
    - name: Provision a set of instances
      local_action:
          group: servers
          count: 5
          exact_count: yes
          credentials: cred.ini
          flavor: <FLAVOR ID>
          group: raxhosts
          image: <IMAGE ID>
          key_name: <SSH KEYNAME>
          module: rax
          name: webheads
          state: present
          wait: yes
      register: rax
    - name: Add new instances to the group 'raxhosts'
      local_action:
        module: add_host
        hostname: "{{ item.name }}"
        ansible_ssh_host: "{{ item.rax_accessipv4 }}"
        ansible_ssh_pass: "{{ item.rax_adminpass }}"
        groupname: raxhosts
      with_items: rax.success
      when: rax.action == 'create'
    - name: Wait for hosts
      local_action: wait_for host={{ item.rax_accessipv4 }} port=22 delay=60 timeout=600 state=started
      with_items: rax.success

Here is what my cred.ini looks like:

[rackspace_cloud]
username = 
api_key =

Run it like so:

RAX_CREDS_FILE=cred.ini RAX_REGION=DFW ansible-playbook <playbook>.yml