# Trying to access groups\[\] programmatically not working

**URL:** <https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [July 2, 2015, 2:55pm UTC](https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662 "2015-07-02T14:55:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dean\_Mills](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dean\_Mills](https://forum.ansible.com/u/Dean_Mills)\
**Post date:** [July 2, 2015, 2:55pm UTC](https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662/1 "2015-07-02T14:55:19Z")

</div>

Having an issue with one of my new playbooks. I am assuming this should work:

In my hosts file:

[develop.us-east-1.vpn]  
[vpn01.us](http://vpn01.us) ansible\_ssh\_host=10.1.1.1  
[vpn02.us](http://vpn02.us) ansible\_ssh\_host=10.1.1.2

[develop.us-east-1.vpn:vars]  
ansible\_ssh\_user=openvpnas

[develop.us-east-1.haproxy]  
[haproxy01.us](http://haproxy01.us) ansible\_ssh\_host=10.1.1.10

[develop.us-east-1.haproxy:vars]  
ansible\_ssh\_user=ubuntu

In my playbook:

vars:  
machine\_environment: ‘development’  
vpc\_region: ‘us-east-1’  
proxy\_servers: ‘haproxy’  
haproxy\_group: “groups[‘{{machine\_environment}}.{{vpc\_region}}.{{ proxy\_servers }}’]”

tasks:

- debug:  
msg: “{{ item }}”  
with\_items: haproxy\_group

Which I assume will loop through my proxy servers as they are listed in my hosts file, but instead I get this:

TASK: [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [[haproxy01.us](http://haproxy01.us)] =\> One or more undefined variables: ‘item’ is undefined

If I change it to this:

- debug:  
msg: “{{ haproxy\_group }}”

I get this:

TASK: [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[haproxy01.us](http://haproxy01.us)] =\> {  
“msg”: “groups[‘development.us-east-1.haproxy’]”  
}

How do I get my groups programmatically so I can use them inside my template to configure haproxy, etc?

I’m sure I’ve just boneheaded something or am assuming it works a specific way, any help greatly appreciated!

D

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 4, 2015, 3:07am UTC](https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662/2 "2015-07-04T03:07:48Z")

</div>

try

haproxy\_group: "{{groups[machine\_environment + '.' + vpc\_region + '.'  
+ proxy\_servers]}}"

---

<div class="post-metadata">

**Author:** ![Dean\_Mills](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dean\_Mills](https://forum.ansible.com/u/Dean_Mills)\
**Post date:** [July 6, 2015, 1:51pm UTC](https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662/3 "2015-07-06T13:51:42Z")

</div>

Thanks Brian, although that didn’t change the error any ☹

D

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 6, 2015, 2:09pm UTC](https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662/4 "2015-07-06T14:09:33Z")

</div>

well, the group is 'develop' and your string is 'development'

---

<div class="post-metadata">

**Author:** ![Dean\_Mills](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dean\_Mills](https://forum.ansible.com/u/Dean_Mills)\
**Post date:** [July 6, 2015, 3:26pm UTC](https://forum.ansible.com/t/trying-to-access-groups-programmatically-not-working/18662/5 "2015-07-06T15:26:00Z")

</div>

Sorry Brian, I should have mentioned I caught that and fixed it after posting the original question! ☹

D
