问题描述
我在2个组中有一个主机:pc和Servers我在每个packages.yml文件中都有2个group_vars(PC和服务器)这些文件定义了要在PC主机和服务器主机上安装的软件包的列表
I have a host in 2 groups : pc and ServersI have 2 group_vars (pc and servers) with, in each the file packages.ymlThese files define the list of packages to be installed on pc hosts and on servers hosts
我有安装默认软件包的作用
I have a role to install default package
问题是:角色任务仅考虑group_vars/pc/packages.yml,未安装来自group_vars/servers/packages.yml的软件包
The problem is : only the group_vars/pc/packages.yml is take into account by the role task, packages from group_vars/servers/packages.yml are not installed
我当然想要安装为PC和服务器定义的软件包
Of course what I want is installation of packages defined for pc and servers
我不知道这是错误还是功能...
I do not know if it is a bug or a feature ...
感谢您的帮助
这是配置:
# file: production
[pc]
armen
kerbel
kerzo
[servers]
kerbel
---
# packages on servers
packages:
- lftp
- mercurial
---
# packages on pc
packages:
- keepassx
- lm-sensors
- hddtemp
推荐答案
如果要使用这样的方案.您应该在ansible.cfg中设置hash_behaviour选项:
If you want to use such scheme. You should set the hash_behaviour option in ansible.cfg:
[defaults]
hash_behaviour = merge
此外,您必须使用字典而不是列表.为了防止重复,我建议使用名称作为键,例如:
In addition, you have to use dictionaries instead of lists. To prevent duplicates I recommend to use names as keys, for example:
group_vars/servers/packages.yml:
group_vars/servers/packages.yml:
packages:
package_name1:
package_name2:
group_vars/pc/packages.yml:
group_vars/pc/packages.yml:
packages:
package_name3:
package_name4:
在剧本任务中(| default({})-对于缺少"package"变量的情况):
And in a playbook task (| default({}) - for an absent "package" variable case):
- name: install host packages
yum: name={{ item.key }} state=latest
with_dict: packages | default({})
这篇关于Ansible:多个组中的主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!