有没有办法覆盖定义为 ansible 星系角色的模板? [英] Is there a way to override a template defined into an ansible galaxy role?
问题描述
我正在尝试使用 ansible 设置服务器,我想使用 这个星系角色.
I'm trying to set up a server with ansible and I'd like to use this galaxy role.
它定义了一个 模板,我想自定义,但我不知道如何.
It defines a template that I'd like to customize, but I don't know how.
现在我将角色定义到 requirements.yml
并使用以下方法安装它:
Right now I defined the role into requirements.yml
and I installed it using:
ansible-galaxy install -r requirements.yml
在我系统的某个地方安装了这个角色.
which installed the role somewhere on my system.
我尝试将文件夹树重新创建到我存储剧本的存储库中:
I tried by recreating the folder tree into my repository where I store my playbooks:
roles
|- ansible-role-passenger
|- templates
|- passenger.j2
但它不起作用.当我运行我的剧本时,ansible 使用来自星系角色内部的 passenger.j2
文件.
but it does not work. When I run my playbook, ansible uses the passenger.j2
file from inside the galaxy role.
我想我可以在github上分叉galaxy角色,然后像我想要的那样编辑文件passenger.j2
,但我不知道这是否有更好"的方法它:)
I think I can fork the galaxy role on github and just edit the file passenger.j2
like I want, but I don't know if this is there is a "better" way to do it :)
推荐答案
不幸的是,您的发现是正确的.从调用剧本覆盖角色中的硬编码模板是不可能的除非角色的作者将其作为功能实现.请注意,对于 files
目录中的简单文件也是如此.
Your findings are unfortunately true. Overriding a hardcoded template in a role from a calling playbook is merely impossible unless the role's author implemented that as a feature. Note that this is also true for simple files in the files
directory.
到目前为止我发现的最好方法:鉴于角色在 templates/passenger.j2
中包含默认模板,在 default/main.yml
中添加一个 var例如 passenger_config_template:passenger.j2
并在角色中使用该变量.然后,用户可以在其剧本/清单中覆盖该变量,并为模板使用不同的名称,该名称将在其他角色中或直接在剧本级别的 templates
目录中获取.
The best way I have found so far: given that the role contains the default template in templates/passenger.j2
, add a var in default/main.yml
such as passenger_config_template: passenger.j2
and use that var in the role. The user can then override that var in its playbook/inventory and use a different name for the template which will be fetched in an other role or directly in a templates
directory at playbook level.
您可以查看类似问题和一个被接受的公关我曾经就他的 ansible 向 @geerlingguy 提出过-角色-gitlab.他可能会考虑对他的乘客角色做同样的事情(或者如果你提出一个 PR,他可能会接受你的 PR).
You can have a look at a similar issue and an accepted PR I once made to @geerlingguy on his ansible-role-gitlab. He might consider doing the same thing on his passenger role (or might accept your PR if you propose one).
这篇关于有没有办法覆盖定义为 ansible 星系角色的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!