【问题标题】:Ansible variable in jinja2 templatejinja2 模板中的 Ansible 变量
【发布时间】:2021-05-04 18:23:57
【问题描述】:

我有一个带有 jinja2 模板的小剧本来应用 nginx 配置。 该模板使用了一些变量。

在我的变量文件中,我有:

---
domains:
  - test@example.com

在我的模板文件中:

server_name  {{ domains }};

部署剧本后,在我的配置文件中,我有:

server_name  ['test@example.com'];

如何只打印 test@example.com?没有 ['...']?

提前致谢!

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    如果您只有一个域,您可以只创建一个simple variable 而不是list variable

    domain: test@example.com
    

    然后使用 Jinja2 模板,例如:

    server_name  {{ domain }};
    

    如果你有多个:

    domains:
      - one@example.com
      - two@example.com
      - three@example.com
    

    你可以loop在列表中:

    {% for domain in domains %}
    server_name  {{ domain }};
    {% endfor %}
    

    这将创建一个类似的文件:

    server_name  one@example.com;
    server_name  two@example.com;
    server_name  three@example.com;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多