# How to use group\_vars when managed host is in multiple groups?

**URL:** <https://forum.ansible.com/t/how-to-use-group-vars-when-managed-host-is-in-multiple-groups/27585>\
**Category:** Ansible Project\
**Created:** [May 21, 2018, 9:25pm UTC](https://forum.ansible.com/t/how-to-use-group-vars-when-managed-host-is-in-multiple-groups/27585 "2018-05-21T21:25:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [May 21, 2018, 9:25pm UTC](https://forum.ansible.com/t/how-to-use-group-vars-when-managed-host-is-in-multiple-groups/27585/1 "2018-05-21T21:25:04Z")

</div>

Maybe the question I should be asking is how do people manage what packages get installed on what computers?

My approach might be wrong?

I’m trying to figure out how to use group\_vars for chocolatey packages to install when the managed host is in multiple groups.

I’d like to append package named, identified by the managed host’s group membership, to the group\_var “chocolatey\_packages” and use 1 task to install all the packages.

The 1 ansible tasks:

- name: Install programs (choco)  
win\_chocolatey:  
name: “{{ item }}”  
state: “present”  
when:
- chocolatey\_packages is defined  
with\_items:
- “{{ chocolatey\_packages }}”

Here is the tree of how I have group\_vars and the associated package names for that group

CORP/  
├── company-wide-package1  
├── company-wide-package2  
├── company-wide-package3  
├── company-wide-package4  
├── company-wide-package5  
└── sales  
├── all-sales-people-package1  
├── all-sales-people-package2  
├── all-sales-people-package3  
├── inside  
│ └── all-inside-sales-people-package1  
└── outside  
└── all-outside-sales-people-package1

Every computer is part of the CORP group.

Some computers are part of the Sales group.

Even less computers are part of the inside or outside sales group.

All computers get the “company-wide” packages installed.

All sales computers get the “all-sales-people” packages installed.

All inside sales people get the “all-inside-sales-people” packages installed.

All outside sales people get the “all-outside-sales-people” packages installed.

I’ve tried to append list of packages to the chocolatey\_packages variable but that seems fragile and tightly coupled to my group\_vars layout.

Variable definition inside CORP/win\_chocolatey.yml

chocolatey\_corp\_pkgs:

- cleanup
- firefox
- git
- googlechrome
- veeam-agent
- virtualclonedrive
- winscp

chocolatey\_packages: “{{ chocolatey\_corp\_pkgs }}”

Variable definition inside sales/win\_chocolatey.yml

chocolatey\_sales\_pkgs:

- crystalreports2010runtime

chocolatey\_packages: “{{ chocolatey\_corp\_pkgs + chocolatey\_sales\_pkgs }}”

Variable definitions inside the sales/inside/win\_chocolatey.yml

chocolatey\_inside\_sales\_pkgs:

- winscp

chocolatey\_packages: “{{ chocolatey\_corp\_pkgs + chocolatey\_sales\_pkgs + chocolatey\_inside\_sales\_pkgs }}”

And things do not work properly if a computer is part of the inside and outside sales groups (yes, there’s 2 computers).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 31, 2018, 4:23am UTC](https://forum.ansible.com/t/how-to-use-group-vars-when-managed-host-is-in-multiple-groups/27585/2 "2018-05-31T04:23:18Z")

</div>

something like?:

vars:  
&nbsp;&nbsp;packages:   
tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact: packages={{ packages|union(lookup('vars', item))}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
"{{hostvars[inventory\_hostname]|select('match','.\*\_pkgs$')|list}}"

&nbsp;&nbsp;&nbsp;&nbsp;- yum: name={{packages}}

---

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [May 31, 2018, 6:38am UTC](https://forum.ansible.com/t/how-to-use-group-vars-when-managed-host-is-in-multiple-groups/27585/3 "2018-05-31T06:38:31Z")

</div>

Won’t that install packages specific host?

Not packages for a specific group?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 31, 2018, 4:17pm UTC](https://forum.ansible.com/t/how-to-use-group-vars-when-managed-host-is-in-multiple-groups/27585/4 "2018-05-31T16:17:49Z")

</div>

all packages for a host, no matter where defined. there are no group  
vars outside of inventory, they are just a convenient way to assign  
host vars in bulk.

group\_vars/all.yml =\>  
all\_pkgs: # packages defined in all group for all servers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vim-minimal  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- less

group\_vars/webservers.yml =\>  
webserver\_pkgs: # packages for only hosts in webserver group  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- nginx

my tasks above will install vim-minimal and less on all machines and  
nginx ONLY on machines that are part of webserver group
