expanding lists

47 views
Skip to first unread message

James Morgan

unread,
Dec 18, 2014, 6:45:35 AM12/18/14
to ansible...@googlegroups.com
Hi,

I have a list of groups

admin:
    groups: ['group-boapp-layout-A', 'group-boapp-layout-B']

Each of these groups has a number of hosts as defined in the inventory

(note that this info needs to be global as opposed to per group_vars/group-boapp-layout-A.yml so that it can be accessed by other groups)

[group-boapp-layout-A]
ec2-test-boapp01
ec2-test-boapp02

[group-boapp-layout-B]
ec2-test-boapp03
ec2-test-boapp04

[tier-boapp:children]
group-boapp-layout-A
group-boapp-layout-B

GROUPS Variables ("groups"):
--------------------------------
{
    "all": [
        "ec2-test-boapp03",
        "ec2-test-boapp04",
        "ec2-test-boapp01",
        "ec2-test-boapp02"
    ],
    "group-boapp-layout-A": [
        "ec2-test-boapp01",
        "ec2-test-boapp02"
    ],
    "group-boapp-layout-B": [
        "ec2-test-boapp03",
        "ec2-test-boapp04"
    ],

I would like to get a list of hosts that admin should live on (union of group-boapp-layout-A and group-boapp-layout-B)

=>  ['ec2-test-boapp01','ec2-test-boapp02','ec2-test-boapp03','ec2-test-boapp04']


I have tried to use with_flattened though I only end up with the hosts from the last group
  
set_fact:
    hosts: "{{ groups[item] }}"
  with_flattened: admin.groups

- debug: msg="{{ app_name }} hosts = {{ hosts }}"

-----------------------------------

ok: [ec2-test-boapp01] => {
    "msg": "admin hosts = ['ec2-test-boapp03', 'ec2-test-boapp04']"
}
ok: [ec2-test-boapp03] => {
    "msg": "admin hosts = ['ec2-test-boapp03', 'ec2-test-boapp04']"
}
ok: [ec2-test-boapp02] => {
    "msg": "admin hosts = ['ec2-test-boapp03', 'ec2-test-boapp04']"
}
ok: [ec2-test-boapp04] => {
    "msg": "admin hosts = ['ec2-test-boapp03', 'ec2-test-boapp04']"
}


This was working with v1.7.x I am now using 

ansible-playbook 1.8.1


I'd appreciate any input or if there is a better way to do this.

Thanks 

James








James Morgan

unread,
Dec 18, 2014, 8:10:16 AM12/18/14
to ansible...@googlegroups.com
A possible solution if I can't expand the lists into one list is to define a different group of hosts specifically for the application. Though that seems messier 

Brian Coca

unread,
Dec 18, 2014, 9:54:22 AM12/18/14
to ansible...@googlegroups.com
You have group theory filters like |union that can help with this.
Note that with_flattened was buggy in 1.8.1, should be fixed in 1.8.2.
I would also avoid the - in names whenever possible, it creates issues
with certain methods of access.

--
Brian Coca

James Morgan

unread,
Dec 18, 2014, 9:58:52 AM12/18/14
to ansible...@googlegroups.com
Hi,

Thanks for that. I have tried to avoid the "in" approach though I have found that the information about where things needs to live needs to be accessed from multiple tiers / groups

I.e apache configuration on the web tier needs to know the hosts that the application is configured on.

I'll have a rethink of my approach as I do agree that it should be avoided if possible.

Interesting about 1.8.2. I'll keep an eye on that. I was pretty sure it was doing what I was expecting prior to 1.8.1.

Many thanks

James
Reply all
Reply to author
Forward
0 new messages