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
rackspace_flavor=4
groupname=a
[acceptance-b:children]
b
local-rackspace
[acceptance-b:vars]
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
On Sunday, November 24, 2013 5:30:50 PM UTC+1, Michael DeHaan wrote:
>
> 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.
>
>
>
>
> On Sun, Nov 24, 2013 at 11:29 AM, Michael DeHaan
> <[email protected]<javascript:>
> > wrote:
>
>> 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://www.google.com/url?q=http%3A%2F%2Fansibleworks.com%2Fdocs%2Fmodules.html%23rax&sa=D&sntz=1&usg=AFQjCNFhakL_krg_UpTClykOtR6KIrFmYA>
>>
>> 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.
>>
>>
>>
>> On Sun, Nov 24, 2013 at 11:16 AM, Lars Vonk
>> <[email protected]<javascript:>
>> > wrote:
>>
>>> 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
>>>
>>>
>>> On Sunday, November 24, 2013 5:02:02 PM UTC+1, Michael DeHaan wrote:
>>>
>>>> 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.
>>>>
>>>>
>>>>
>>>>
>>>> On Sun, Nov 24, 2013 at 10:42 AM, Lars Vonk <[email protected]>wrote:
>>>>
>>>>> 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:
>>>>> ---
>>>>> - hosts: a
>>>>> connection: local
>>>>> remote_user: root
>>>>> tasks:
>>>>> - name: Foo
>>>>> command: echo {{ hostname }}
>>>>>
>>>>> b.yml contains:
>>>>> ---
>>>>> - hosts: b
>>>>> connection: local
>>>>> remote_user: root
>>>>> tasks:
>>>>> - name: Foo
>>>>> command: echo {{ hostname }}
>>>>>
>>>>> test contains:
>>>>> localhost ansible_python_interpreter=/usr/local/bin/python
>>>>>
>>>>> [a]
>>>>> localhost
>>>>>
>>>>> [b]
>>>>> localhost
>>>>>
>>>>> [test-a:children]
>>>>> a
>>>>>
>>>>> [test-a:vars]
>>>>> hostname=a.com<http://www.google.com/url?q=http%3A%2F%2Fa.com&sa=D&sntz=1&usg=AFQjCNEYnDzIlRjJbXDcx3Cax_hkvntAFQ>
>>>>>
>>>>> [test-b:children]
>>>>> b
>>>>>
>>>>> [test-b:vars]
>>>>> hostname=b.com<http://www.google.com/url?q=http%3A%2F%2Fb.com&sa=D&sntz=1&usg=AFQjCNFq7tUq50NgFVyTsk2WMeiRf6l6WQ>
>>>>>
>>>>> When I run ansible-playbook a.yml -i test -vvvv it echos *b.com
>>>>> <http://www.google.com/url?q=http%3A%2F%2Fb.com&sa=D&sntz=1&usg=AFQjCNFq7tUq50NgFVyTsk2WMeiRf6l6WQ>*
>>>>> while
>>>>> I expect *a.com
>>>>> <http://www.google.com/url?q=http%3A%2F%2Fa.com&sa=D&sntz=1&usg=AFQjCNEYnDzIlRjJbXDcx3Cax_hkvntAFQ>*.
>>>>>
>>>>> I expected this because the hosts in a.yml is a and a is a child of
>>>>> test-a
>>>>> that defines the hostname as *a.com
>>>>> <http://www.google.com/url?q=http%3A%2F%2Fa.com&sa=D&sntz=1&usg=AFQjCNEYnDzIlRjJbXDcx3Cax_hkvntAFQ>*.
>>>>>
>>>>> For some reason I don't understand the hostname variable resolves to
>>>>> *b.com
>>>>> <http://www.google.com/url?q=http%3A%2F%2Fb.com&sa=D&sntz=1&usg=AFQjCNFq7tUq50NgFVyTsk2WMeiRf6l6WQ>*
>>>>> .
>>>>> What we are trying to achieve with this setup is to create a rackspace
>>>>> instance using a local connection (a.yml) and that add a host to the
>>>>> test-a
>>>>> which is then used later in the playbook as host. This fails because it
>>>>> resolves to the wrong hostname.
>>>>>
>>>>> What am I doing wrong here? Is there a better setup to achieve this?
>>>>>
>>>>> Thanks in advance,
>>>>> Lars
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>>
>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Michael DeHaan <[email protected]>
>>>>
>>>> CTO, AnsibleWorks, Inc.
>>>> http://www.ansibleworks.com/<http://www.google.com/url?q=http%3A%2F%2Fwww.ansibleworks.com%2F&sa=D&sntz=1&usg=AFQjCNEn7XMcb-1wMxliSyOhlnyBBlDaQg>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected] <javascript:>.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>
>>
>> --
>> Michael DeHaan <[email protected] <javascript:>>
>>
>> CTO, AnsibleWorks, Inc.
>> http://www.ansibleworks.com/<http://www.google.com/url?q=http%3A%2F%2Fwww.ansibleworks.com%2F&sa=D&sntz=1&usg=AFQjCNEn7XMcb-1wMxliSyOhlnyBBlDaQg>
>>
>>
>
>
> --
> Michael DeHaan <[email protected] <javascript:>>
> CTO, AnsibleWorks, Inc.
> http://www.ansibleworks.com/<http://www.google.com/url?q=http%3A%2F%2Fwww.ansibleworks.com%2F&sa=D&sntz=1&usg=AFQjCNEn7XMcb-1wMxliSyOhlnyBBlDaQg>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.