# add\_host after Rackspace Provisioning

**URL:** <https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633>\
**Category:** Ansible Project\
**Created:** [October 8, 2013, 4:39pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633 "2013-10-08T16:39:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_Verheyden](https://avatars.discourse-cdn.com/v4/letter/p/c6cbf5/32.png) [@Phillip\_Verheyden](https://forum.ansible.com/u/Phillip_Verheyden)\
**Post date:** [October 8, 2013, 4:39pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633/1 "2013-10-08T16:39:44Z")

</div>

I’m running into some trouble with Rackspace provisioning. I am attempting to provision a new host and then add it to the in-memory host to continue with the rest of the plays.

The provisioning looks like this:

- name: “Ensure {{ num\_tomcat\_servers }} Tomcat servers exist”  
local\_action:  
module: rax  
api\_key:   
username:   
region:   
service: cloudservers  
state: active  
wait: yes  
wait\_timeout: 600  
flavor:   
image:   
name: ‘identifier-tomcat-{{ item }}’  
meta:  
group: tomcat  
with\_sequence: count={{ num\_tomcat\_servers }}  
register: tomcat\_hosts  
notify:
- Register tomcat instances
- Group tomcat instances

Then, I have these handlers invoked:

handlers:

- name: Register tomcat instances  
local\_action: add\_host name={{ item.accessIPv4 }} groups={{ item.name }}  
with\_items: tomcat\_hosts.instances

- name: Group tomcat instances  
local\_action: add\_host name={{ item.accessIPv4 }} groups=tomcat  
with\_items: tomcat\_hosts.instances

The idea here is that I will add all of the instances to their respective name group (identifer-tomcat-1, identifier-tomcat-2, etc) as well as a more generic group called ‘tomcat’. This is the only way that I could figure out how to do that (using 2 handlers)

The issue that I get is in the handler. It seems to bomb out on the add\_host line:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 8, 2013, 5:28pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633/2 "2013-10-08T17:28:53Z")

</div>

Can you re-run the playbook with the ‘-v’ flag?

That should provide you debug information about the items in the loop from with\_items.

---

<div class="post-metadata">

**Author:** ![Phillip\_Verheyden](https://avatars.discourse-cdn.com/v4/letter/p/c6cbf5/32.png) [@Phillip\_Verheyden](https://forum.ansible.com/u/Phillip_Verheyden)\
**Post date:** [October 8, 2013, 7:36pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633/3 "2013-10-08T19:36:27Z")

</div>

I ran with -vvvv but I only included the non-SSH output; let me know if I should post some of that. I also scrubbed the IP address from the provisioned server along with it’s ID but it all looked right.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 8, 2013, 8:00pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633/4 "2013-10-08T20:00:26Z")

</div>

Ah, I see the issue.

When you do a ‘register’ in combination with ‘with\_sequence’, you get back a response that has a ‘results’ attribute that contains a list of each of the individual responses. You will need to loop through that. Something like:

handlers:

- name: Register tomcat instances  
local\_action: add\_host name={{ item.instances[0].accessIPv4 }} groups={{ [item.instances[0].name](http://item.name) }}  
with\_items: tomcat\_hosts.results

Currently, the rax module, will only create a single host at a time, which is likely why you are using with\_sequence. So referencing index 0 should be somewhat safe.

It is on my to do list to add “count” functionality to the rax module, so that this isn’t needed. I’ve been working on a number of new features for it already.

---

<div class="post-metadata">

**Author:** ![Phillip\_Verheyden](https://avatars.discourse-cdn.com/v4/letter/p/c6cbf5/32.png) [@Phillip\_Verheyden](https://forum.ansible.com/u/Phillip_Verheyden)\
**Post date:** [October 8, 2013, 8:15pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633/5 "2013-10-08T20:15:21Z")

</div>

Hm ok I think I understand. So then the handler would be executed multiple times, once for each item in the sequence? That seems like the only scenario that referencing index 0 for the instances list would be safe, right?

Actually no, let me answer my own question here. The handler itself is only executed once which is fine: we’re actually looping through a list of ‘results’ which is what gets yielded from with\_sequence. Each item in the results list has an ‘instances’ attribute which is a single-element list. Do I understand correctly?

Also, is there any other way I would know about this ‘results’ attribute on the response without you telling me or super deep diving into the source? I wouldn’t mind updating some docs for the next poor sap that comes along :). I guess the documentation item here would be to say what the priority of yields are (with\_sequence happens after the rax creation command in this case).

Although to be honest, at this point this has turned into a bit of an academic discussion. Rackspace has recently been taking quite a while to provision servers (sometimes 10-15 minutes) so it has become impractical to expect instantiation and provisioning to occur all within the same playbook.

Thanks for your help! Lifesaver!

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 8, 2013, 8:27pm UTC](https://forum.ansible.com/t/add-host-after-rackspace-provisioning/13633/6 "2013-10-08T20:27:16Z")

</div>

I believe you understand correctly.

As far as the ‘results’ attribute, I implemented a simple test case, and then used ‘debug’ to print out my registered variable. It doesn’t seem to be documented currently, but it looks like playbooks\_loops.rst would be the likely place for it to show up.

The other current ‘issue’ with the ‘rax’ module, is that it acts in a serial manner. Because you call it once per loop, the ‘wait’ parameter causes ansible to kick off a build and wait for it to become active before moving on to the next server. Generally a linux server may take less than 5 minutes to build, so as you add more, it would be 5\*n minutes.

I’m working on this as well, but don’t yet have anything to show for it yet. I hope to have something soonish…
