Sorry Guillem, I typed that up in a hurry. I should have quoted the
key 'domain' like so:

- debug: msg="{{ project[item]['domain'] }}"

or as use have discovered, the equivalent:

   {{ project[item].domain }}

Mixing dictionary lookup forms (that is, using both dots and brackets)
can be a little confusing to other people reading the code, so I
prefer to use one or the other but not both at the same time.

K





Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



On Wed, Jan 29, 2014 at 9:11 AM, Kahlil Hodgson
<kahlil.hodg...@dealmax.com.au> wrote:
> Hi Guillem.
>
> This looks like a modeling problem: the project variables need to be
> detached from the host/group variables.
>
> Try something like:
>
> # group_vars/all.yml
> ---
> project:
>    test1:
>       domain: test1.com
>       #... other vars for project test1
>
>    test2:
>       domain: test2.com
>       # ... other vars for project test2
>
>    # ... etc
>
> # host_vars/host1.yml
> ---
> project_list:
>    - test1
>    - test2
>
> # host_vars/host2.yml
> ---
> project_list:
>    - test3
>    - test4
>
>
> # playbook.yml
> ---
> hosts: host1:host2
> tasks:
>   - debug: msg="{{ project[item][domain]}}"
>     with_items: project_list
>
> Hope this helps.
>
> K
>
> Kahlil (Kal) Hodgson                       GPG: C9A02289
> Head of Technology                         (m) +61 (0) 4 2573 0382
> DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281
>
> Suite 1415
> 401 Docklands Drive
> Docklands VIC 3008 Australia
>
> "All parts should go together without forcing.  You must remember that
> the parts you are reassembling were disassembled by you.  Therefore,
> if you can't get them together again, there must be a reason.  By all
> means, do not use a hammer."  -- IBM maintenance manual, 1925
>
>
>
> On Wed, Jan 29, 2014 at 2:38 AM, Guillem Liarte
> <guillem.lia...@googlemail.com> wrote:
>> Hello,
>>
>> I think I have got to a point where I really get confused about teh way
>> variables are used. I need some clarity to understand how you would do this
>> with ansible:
>>
>> Lets say I have one single hosts where I want to create a collection of
>> directories in /data/db/{project1,project2,n}
>>
>> I am using the structure described in the Ansible best practices:
>>
>> http://docs.ansible.com/playbooks_best_practices.html
>>
>> in /etc/ansible/hosts I have:
>>
>> [test1]
>> host1
>>
>> [test2]
>> host1
>>
>> Yes, the same host in two different groups. I believe this is legal.
>>
>> cat test.yml
>> ---
>> # file: test.yml
>> - hosts:
>>     - test1          << Hosts in group test1 and hosts in group test2
>>     - test2
>>   roles:
>>     - test
>>
>>
>> group_vars/test1.yml
>> ---
>> # variables to be used in test role
>>
>> project: test1
>> domain:   test1.com
>> (other variables like db parameters etc)
>>
>> group_vars/test2.yml
>> ---
>> # variables to be used in test role
>>
>> project: test2
>> domain:   test2.com
>> (other variables like db parameters etc)
>>
>>
>> roles/test/tasks/main.yml
>>
>> - name: Data directories should exist
>>   file: state=directory path=/data/db/{{ project }} owner=root group=root
>> mode=0770
>>
>>
>> Then when I do:
>>
>> ansible-playbook /etc/ansible/test.yml -k
>>
>> Only the variables in group test1 will apply, and /data/db/test1  will be
>> created but not test2.
>>
>> OK, so it stops after finding the first occurrence. If I remove test1 from
>> group_var, test2 gets used.
>>
>>
>> So, how do I proceed from here?
>>
>> I want to be able to have test applied to one or many hosts, so a number of
>> projects can be hosted in a number of hosts. Like this:
>>
>> [test1]
>> host1
>>
>> [test2]
>> host2
>>
>> [test3]
>> host2
>>
>> [test4]
>> host1
>>
>> I suppose that part of teh solution will be using a different thing like
>> parametrized roles. But all teh examples I find are to do the opposite, to
>> pass variables to do different things to a different hosts when applying the
>> same role. Here I want to parametrized the role so different groups can be
>> existing in the same host.
>>
>> Is it possible to tell ansible to cycle through variable group files?
>>
>> Other things I tried:
>>
>> http://docs.ansible.com/playbooks_variables.html#passing-variables-on-the-command-line
>>
>> This ignores the variable I pass and still goes with the test1 variable
>> group only.
>>
>> Other things I thought it may work are:
>>
>> - use local facts, but I do not see why that would help
>> - use conditional variable imports, but then I have a catch 22
>>
>>
>>
>> The bottom line of what i want is to be able to declare:
>>
>> In host1, projectA and projectD are hosted and the variables for each group
>> should be found in group_vars, as projectA and projectB user the role
>> project.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> --
>> 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 ansible-project+unsubscr...@googlegroups.com.
>> To post to this group, send email to ansible-project@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to