oscars
oscars

Reputation: 67

Ansible variable in jinja2 template

I have a small playbook with jinja2 template to apply nginx configuration. This template uses some variables.

In my variables file I have:

---
domains:
  - test@example.com

In my template file I have:

server_name  {{ domains }};

Once playbook deployed, in my config file I have:

server_name  ['test@example.com'];

What can I do to print only test@example.com? without ['...']?

Thanks in advance!

Upvotes: 0

Views: 2942

Answers (1)

Marco Baldelli
Marco Baldelli

Reputation: 3728

If you only have one domain, you can just create a simple variable instead of a list variable:

domain: test@example.com

and than use a Jinja2 template such as:

server_name  {{ domain }};

If you have more than one:

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

you can loop over the list:

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

This will create a file like:

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

Upvotes: 2

Related Questions