Asible: host in multiple groups

I have a host in 2 groups: PCs and servers I have 2 group_vars (PCs and servers), in each package.yml file These files define the list of packages that should be installed on the host computers and on the server hosts

I have a role to install the default package

The problem is that only the role of group_vars / pc / packages.yml is taken into account in the role task, packages from group_vars / servers / packages.yml are not installed

Of course I want to install packages specific to PCs and servers

I don't know if this is a bug or function ...

thanks for the help

here is the configuration:

# file: production [pc] armen kerbel kerzo [servers] kerbel --- # packages on servers packages: - lftp - mercurial --- # packages on pc packages: - keepassx - lm-sensors - hddtemp 
+6
source share
2 answers

If you want to use such a scheme. You must set the hash_behaviour option in the ansible.cfg file:

 [defaults] hash_behaviour = merge 

In addition, you should use dictionaries instead of lists. To prevent duplicates, I recommend using names as keys, for example:

group_vars / servers / packages.yml:

 packages: package_name1: package_name2: 

group_vars / pc / packages.yml:

 packages: package_name3: package_name4: 

And in the problem with the book (| default ({}) - for the missing case with the variable "package"):

 - name: install host packages yum: name={{ item.key }} state=latest with_dict: packages | default({}) 
+2
source

It's not a mistake. According to docs about the priority of a variable, you should not define a variable in several places and try to simplify it. Michael DeHaan (Ansible lead dev) answered a similar question on this topic:

As a rule, I find the goal of games, but to bind hosts to roles, so individual roles should contain list of packages.

I would use roles as this is a bit cleaner IMO.

If you really want (and this is NOT the recommended way), you can set the hash_behaviour option in the ansible.cfg file:

 [defaults] hash_behaviour = merge 

This will merge the two values ​​when overriding the hash (dict) instead of replacing the old value with the new one. However, this does not work in lists, so you need to create a hash of lists, for example:

group_vars / all / package.yml:

 packages: all: [pkg1, pkg2] 

group_vars / servers / package.yml:

 packages: servers: [pkg3, pkg4] 

The cycle, although the textbook is a bit more complicated.

+1
source

All Articles