# Question about variables and local connections

**URL:** <https://forum.ansible.com/t/question-about-variables-and-local-connections/13989>\
**Category:** Ansible Project\
**Created:** [November 24, 2013, 3:42pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989 "2013-11-24T15:42:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars\_Vonk](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lars\_Vonk](https://forum.ansible.com/u/Lars_Vonk)\
**Post date:** [November 24, 2013, 3:42pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/1 "2013-11-24T15:42:11Z")

</div>

Hi,

We are creating playbooks for our servers that include creating instances with rackspace. We have problems with setting up the different inventory files with the appropriate vars. To illustrate the problem I created a small test case:

DIR  
./a.yml

./b.yml  
./test

a.yml contains:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 24, 2013, 4:02pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/2 "2013-11-24T16:02:02Z")

</div>

The host is a member of two groups.

In this case, rightfully, variables are pulled from both.

This is by design – you may select hosts based on purpose (such as webservers), while assigning other variables based on geography.

---

<div class="post-metadata">

**Author:** ![Lars\_Vonk](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lars\_Vonk](https://forum.ansible.com/u/Lars_Vonk)\
**Post date:** [November 24, 2013, 4:16pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/3 "2013-11-24T16:16:24Z")

</div>

Thanks for replying and clarifying.

So then my question is this: Both application A and B require a local connection and variables to communicate with rackspace (like hostname for instance). Do you have some pointers how I should setup my inventory file and were to put my variables? The playbook for both apps is basically:

- if not exists create instance in rackspace with this {{ hostname }}
- add host to group using add\_host command
- include application specific playbook

Like the example below one of the apps fails because it (rightfully) resolves to the wrong hostname.

Thanks again.

Lars

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 24, 2013, 4:29pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/4 "2013-11-24T16:29:34Z")

</div>

I’m having difficulty between drawing the lines between “command echo” and your cloud integration.

The rax module has a parameter “instance-name” and will create that instance with that name if it does not exist.

[http://ansibleworks.com/docs/modules.html#rax](http://ansibleworks.com/docs/modules.html#rax)

You can also use the “exact\_count” parameter to request an exact number of instances starting with a given name, and it will slay extra instances if you have too many.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 24, 2013, 4:30pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/5 "2013-11-24T16:30:50Z")

</div>

BTW, you don’t pre-create hosts when using cloud machines.

You should be using the rackspace dynamic inventory script.

You would still use add\_host, but if you are trying to predefine the system in your INI based inventory, that’s not really the way it’s meant to work.

The cloud is meant to be authorative.

Can you still use group variables? Sure. Should you it to define hostnames prior to creating instances – no.

---

<div class="post-metadata">

**Author:** ![Lars\_Vonk](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lars\_Vonk](https://forum.ansible.com/u/Lars_Vonk)\
**Post date:** [November 24, 2013, 4:50pm UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/6 "2013-11-24T16:50:54Z")

</div>

I am doing just that. Here is the code:

a.yml and b.yml start with the same rackspace setup. How can I not have hostname, or the flavor or the image as a variable?

- hosts: local-rackspace  
connection: local  
remote\_user: root  
tasks:

- name: Rackspace | Create Rackspace server instance  
local\_action:  
module: rax  
credentials: ~/.rax  
name: “{{ hostname }}”  
flavor: “{{ rackspace\_flavor }}”  
image: “{{ rackspace\_image }}”  
region: LON  
state: present  
wait: yes  
wait\_timeout: 900  
files:  
/root/.ssh/authorized\_keys: ~/.ssh/id\_rsa.pub  
register: rax

- name: Rackspace | Add new instance to host group  
action: add\_host hostname={{ hostname }} ansible\_ssh\_host=${item.accessIPv4} groupname={{ groupname }}  
with\_items: ${rax.instances}

My slimmed down inventory file:

[local-rackspace]  
localhost ansible\_python\_interpreter=/usr/local/bin/python

[a]

# Added by rackspace module

[b]

# Added by rackspace module

[acceptance-a:children]  
a  
local-rackspace

[a:vars]  
[hostname=test.a.com](http://hostname=test.a.com)  
rackspace\_flavor=4  
groupname=a

[acceptance-b:children]  
b  
local-rackspace

[acceptance-b:vars]  
[hostname=test.b.com](http://hostname=test.b.com)  
rackspace\_flavor=3

groupname=b

rackspace\_image is defined in group\_vars/all since we use the same base image for our apps.

Maybe my earlier example was to simplistic but I thought it was easier to explain my question since it doesn’t require a rackspace account to test it.

So my question is still: how can I make sure that I pass the correct variables to the rax module in this case? Or I just hardcode them in the file? I’d really like it to make it a role so I can use it for my other apps as well, but being unable to use variables makes it impossible. Now I am sure I am missing something really obvious here, but I can’t figure out what…

Thanks again for your help.

Lars

---

<div class="post-metadata">

**Author:** ![Lars\_Vonk](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lars\_Vonk](https://forum.ansible.com/u/Lars_Vonk)\
**Post date:** [November 25, 2013, 10:54am UTC](https://forum.ansible.com/t/question-about-variables-and-local-connections/13989/7 "2013-11-25T10:54:59Z")

</div>

Ok I get how it supposed to work now. I’d like to share our setup.

We created a rackspace role and pass in the different variables (different apps have different flavors on different environments). We no longer add the local-rackspace group to the application group and add specific vars to the local-rackspace group on specific environments(acceptance/production). Setup is now like this:

**playbook app\_a** :  
./appa.yml
