different groups with different variables using the same role in the same host

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

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

Kahlil,

Thanks for the reply.

I have tried your exact example in a new ansible instance.

I had to adapt it as the playbook would not pass syntax checks:

This works however:

Kahlil,

I found a way to adress those values stored in the array:

  • name: Create directories
    file: state=directory path=/data/db/{{ project[item].domain }} owner=root group=root mode=0770
    with_items: project_list

This creates effectively:

/data/
└── db
├── test1.com
└── test2.com

Which is what i wanted to do. Excellent.

I've been using variants of dict.keys(), dict.values(), and dict.items() when utilizing with_items iteration.

Adam,

Would you be so kind to explain that or link to the right documentation? I have not been able to find a solution that way.

Are you saying there is a better way to do this?

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

Kahlil,

Many thanks for this. I am finding lots of power in Ansible, but also lots of ‘hidden’ gotchas. I have now revamped my configuration to be used with your suggestion and it works really well.

The next pit I found was in the variable precedence.

I honestly understood from the best practice and the docuementation that if you had a role called web, then placing a file called web.yml in there would make the tasks in web to lookup variables stored in web.yml. However if I had another file lets say test.yml before, it would use it instead. So in order to overcome this problem I see two solutions, manually forcing vars_file from each playbook or store all variables for all playbooks in one file. I’m still deciding what would be the best behavior for the future…

Many thanks for your input so far, you healped me overcome a fundamental issue I had in here.

Guillem