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
If you notice the type of
rax.results.success
islist
.So this:
hostname: "{{ item.success.name }}"
should be
hostname: "{{ item.success[0].name }}"
orhostname: "{{ item['success'][0]['name'] }}"
.