如何将一个Ansible角色的`defaults/main.yml`文件分割成多个文件? [英] How to split an ansible role's `defaults/main.yml` file into multiple files?
问题描述
在一些烦人的角色(例如roles/my-role/
)中,我有一些很大的默认变量文件(defaults/main.yml
).我想将main.yml
拆分为几个较小的文件.可以这样做吗?
In some ansible roles (e.g. roles/my-role/
) I've got quite some big default variables files (defaults/main.yml
). I'd like to split the main.yml
into several smaller files. Is it possible to do that?
我曾尝试创建文件defaults/1.yml
和defaults/2.yml
,但是ansible不会加载它们.
I've tried creating the files defaults/1.yml
and defaults/2.yml
, but they aren't loaded by ansible.
推荐答案
我在下面描述的功能自Ansible 2.6起就可用,但在v2.6.2中有一个错误修复,而在v2.7中有一个错误修正.
要查看旧版本的解决方案,请参阅Paul的答案.
The feature I'm describing below has been available since Ansible 2.6, but got a bugfix in v2.6.2 and another (minor) one in v2.7.
To see a solution for older versions, see Paul's answer.
创建目录- defaults/main/
-并将所有YAML文件放在其中,而不是创建defaults/main.yml
.
-
defaults/main.yml
→defaults/main/*.yml
Ansible将在该目录中加载任何*.yml
文件,因此您可以将文件命名为roles/my-role/defaults/main/{1,2}.yml
.
Ansible will load any *.yml
file inside that directory, so you can name your files like roles/my-role/defaults/main/{1,2}.yml
.
请注意,旧文件-defaults/main.yml
-必须不存在.参见此Github评论.
Note, the old file — defaults/main.yml
— must not exist. See this Github comment.
顺便,上述解决方案也适用于vars/
:
By the way, the above solution also works for vars/
:
-
vars/main.yml
→vars/main/*.yml
该功能已在v2.6中引入- git commit ,请求,
The feature has been introduced in v2.6 — git commit, Pull Request, main Github issue.
有两个错误修正:
- v2.7修复: git commit ,提交,拉请求
- v2.7修复: git commit ,错误讨论
- v2.7 fix: git commit, Pull Request — backported to v2.6.2: commit, Pull Request
- v2.7 fix: git commit, Pull Request, bug discussion
这篇关于如何将一个Ansible角色的`defaults/main.yml`文件分割成多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!