有没有办法覆盖定义为ansible星系角色的模板? [英] Is there a way to override a template defined into an ansible galaxy role?

查看:44
本文介绍了有没有办法覆盖定义为ansible星系角色的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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上派出银河角色,然后按照我的需要编辑文件 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 中添加一个变量例如 passenger_config_template:passenger.j2 并在角色中使用该var.然后,用户可以在其剧本/清单中覆盖该var,并为模板使用其他名称,该模板将以其他角色或直接在剧本级别的 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.

您可以查看类似问题和<一个href ="https://github.com/geerlingguy/ansible-role-gitlab/pull/89/files" rel ="nofollow noreferrer">可接受的PR 我曾经对他的ansible发给@geerlingguy,角色gitlab.他可能会考虑在乘客角色上做同样的事情(或者如果您提出一个公关请求,也可能会接受您的公关).

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屋!

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