Ansible:在多个组中托管 [英] Ansible : host in multiple groups

查看:24
本文介绍了Ansible:在多个组中托管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主机分为 2 组:PC 和服务器我有 2 个 group_vars(PC 和服务器),在每个文件 packages.yml 中这些文件定义了要安装在 pc 主机和服务器主机上的软件包列表

I have a host in 2 groups : pc and Servers I have 2 group_vars (pc and servers) with, in each the file packages.yml These files define the list of packages to be installed on pc hosts and on servers hosts

我有安装默认包的角色

问题是:角色任务只考虑 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

不知道是bug还是功能...

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({}) - 对于不存在的包"变量情况):

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:在多个组中托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆