如何在jinja2过滤器内使用嵌套

时间:2022-01-27 20:47:53

I am a writing a template file for virtual host and DNS inside the file should change as per environment host-name.Below is problem i am trying to solve:

我正在为虚拟主机编写模板文件,文件中的DNS应根据环境主机名更改。我正在尝试解决问题:

server {
    listen 80;
    charset utf-8;
    server_name "{{ 'a.com if ansible_hostname='p.com' 'b.com' if   ansible_hostname= 'd.com' 'z.com' if ansible hostname is 'k.com' else 'default.com' }}";
    rewrite ^(.*)  https://$server_name$1 permanent;
}

How can i achieve this thing in template ie:

我怎样才能在模板中实现这一点,即:

{{ 'a.com' if ansible_hostname='p.com' 'b.com' if ansible_hostname= 'd.com' 'z.com' if ansible hostname is 'k.com' else 'default.com' }}" `

I am new to jinja 2 and have no idea how to do this. Single if else statement is working but how can i use multiple if else statement for defining the value of a variable in the file.

我是jinja 2的新手,不知道怎么做。单个if else语句正在工作,但我如何使用多个if else语句来定义文件中变量的值。

2 个解决方案

#1


So after some research work i was able to crack down the issue.We can

因此,经过一些研究工作,我能够打破这个问题。我们可以

{% if ansible_hostname == 'a.com' %}
 {% set server = 'b.com' %}
{% elif ansible_hostname == 'c.com' %}
 {% set server = 'd.com' %}
{% else %}
 {% set server = 'define yourself' %}
{% endif %}

server {
    listen 80;
    charset utf-8;
    server_name {{ server }};
    rewrite ^(.*)  https://$server_name$1 permanent;
}

Still if someone can show the use of jinja2 filters for achieving this then that will be much appreciated.

如果有人可以展示使用jinja2过滤器来实现这一点,那么将非常感激。

#2


I think no Jinja filter will be of use here but you can simply do it with a dict:

我认为没有Jinja过滤器可以在这里使用,但你可以简单地用dict做:

server {
    listen 80;
    charset utf-8;
    server_name {{ {"a.com": "b.com", "c.com": "d.com"}[ansible_hostname] | default("default.com") }};
    rewrite ^(.*)  https://$server_name$1 permanent;
}

That might work with 2 or a few more hosts but gets quickly dirty with increasing numbers. In general I think it would be more clean if such decisions would not happen in a template file. This could as well be a config option in either host_vars or group_vars. To stay with your example hosts you could have a file host_vars/a.com with the content

这可能适用于2个或更多的主机,但随着数量的增加而变得越来越脏。一般来说,如果在模板文件中不会发生这样的决定,我认为会更干净。这也可以是host_vars或group_vars中的配置选项。要继续使用您的示例主机,您可以拥有包含内容的host_vars / a.com文件

---

server: b.com

...

and a file host_vars/c.com with the content

以及带有内容的文件host_vars / c.com

---

server: d.com

...

In a file group_vars/all you can define the default value.

在文件group_vars / all中,您可以定义默认值。

---

server: default.com

...

#1


So after some research work i was able to crack down the issue.We can

因此,经过一些研究工作,我能够打破这个问题。我们可以

{% if ansible_hostname == 'a.com' %}
 {% set server = 'b.com' %}
{% elif ansible_hostname == 'c.com' %}
 {% set server = 'd.com' %}
{% else %}
 {% set server = 'define yourself' %}
{% endif %}

server {
    listen 80;
    charset utf-8;
    server_name {{ server }};
    rewrite ^(.*)  https://$server_name$1 permanent;
}

Still if someone can show the use of jinja2 filters for achieving this then that will be much appreciated.

如果有人可以展示使用jinja2过滤器来实现这一点,那么将非常感激。

#2


I think no Jinja filter will be of use here but you can simply do it with a dict:

我认为没有Jinja过滤器可以在这里使用,但你可以简单地用dict做:

server {
    listen 80;
    charset utf-8;
    server_name {{ {"a.com": "b.com", "c.com": "d.com"}[ansible_hostname] | default("default.com") }};
    rewrite ^(.*)  https://$server_name$1 permanent;
}

That might work with 2 or a few more hosts but gets quickly dirty with increasing numbers. In general I think it would be more clean if such decisions would not happen in a template file. This could as well be a config option in either host_vars or group_vars. To stay with your example hosts you could have a file host_vars/a.com with the content

这可能适用于2个或更多的主机,但随着数量的增加而变得越来越脏。一般来说,如果在模板文件中不会发生这样的决定,我认为会更干净。这也可以是host_vars或group_vars中的配置选项。要继续使用您的示例主机,您可以拥有包含内容的host_vars / a.com文件

---

server: b.com

...

and a file host_vars/c.com with the content

以及带有内容的文件host_vars / c.com

---

server: d.com

...

In a file group_vars/all you can define the default value.

在文件group_vars / all中,您可以定义默认值。

---

server: default.com

...