How to use group_vars when managed host is in multiple groups?

212 views
Skip to first unread message

b...@tanners.org

unread,
May 21, 2018, 5:25:04 PM5/21/18
to Ansible Project
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).



Brian Coca

unread,
May 31, 2018, 12:23:29 AM5/31/18
to Ansible Project
something like?:

vars:
packages: []
tasks:
- set_fact: packages={{ packages|union(lookup('vars', item))}}
with_items:
"{{hostvars[inventory_hostname]|select('match','.*_pkgs$')|list}}"

- yum: name={{packages}}

--
----------
Brian Coca

Bob Tanner

unread,
May 31, 2018, 2:38:45 AM5/31/18
to ansible...@googlegroups.com

On May 30, 2018, at 11:23 PM, Brian Coca <bc...@redhat.com> wrote:

something like?:

vars:
 packages: []
tasks:
   - set_fact: packages={{ packages|union(lookup('vars', item))}}
    with_items:
"{{hostvars[inventory_hostname]|select('match','.*_pkgs$')|list}}"

   - yum: name={{packages}}

Won’t that install packages specific host?

Not packages for a specific group?

--
Bob Tanner <b...@tanners.org>          Clash of Clan Tag: #LUJ2CPU
Follow me on Facebook and Twitter!  Clash Royale Tag: #2Q98YPC

signature.asc

Brian Coca

unread,
May 31, 2018, 12:18:02 PM5/31/18
to Ansible Project
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
- vim-minimal
- less

group_vars/webservers.yml =>
webserver_pkgs: # packages for only hosts in webserver group
- nginx

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



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages