Ansible string concatenation into config file - ansible

I have a list of variables ("/dev/sda", "/dev/sdb") which I would like to to dynamically update a config file.
The config file is finally expected to look like
filter = [ "a|/dev/sda[145]|", "a|/dev/sdb[145]|", "a|/dev/mapper/3500.*part1|", "r|.*|" ]
Currently, the config file looks like this.
filter = [ "a|/dev/mapper/3500.*part1|", "r|.*|" ]

I have the answer to my own question. I am not sure if this is the "jinja" way of doing things.
{% set ldsk = [] %}
{% for disk in ldisks %}
{{ ldsk.append( "\"|a"+ disk +"[145]|\"") }}
{% endfor %}
filter = [ {{ ldsk | join(", ") }}, "a|/dev/mapper/3500.*part1|", "r|.*|" ]

Related

Read yaml file into Jinja2 template

I am trying to read a list of ip from a yaml file and render them with jinja2 like:
a.yml:
server_ips:
- 192.168.1.1
- 192.168.1.2
- 192.168.1.3
Expected results after rendering:
server_ips = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
However, what I got for now is like:
server_ips = ['192.168.1.1', '192.168.1.2', '192.168.1.3']
What I worte in the jirja2 file is like:
{% set start_join = [] %}
{% for ip in server_ips %}
{{ start_join.append(ip)}}
{% endfor %}
start_join = {{ start_join }}
So is there any way I could turn the single quote to double quotes in the list?

Jinja using a variable as the name of the file used in a Jinja "include"

Within jinja I am trying to pass a variable into the file name used by the "include" for example: {% include = variable1 %}
Something like this:
{% set filepath = ['Templates/file1.j2', 'Templates/file2.j2', 'Templetes/file3.j2'] %}
These are the files:
{% for list in filepath -%}
'{{ list }}'
{% endfor %}
{% set Mypath = filepath[1] %}
This is the path and file to use: '{{ Mypath }}'
{% include "'" Mypath "'" %}
The result I am hoping to achieve should look like this.
{% include = 'Templates/file2.j2' %}.
Is is possible to pass a variable into an include in this fashion within Jinja?

Ansible template mix up order of elements

I have a problem with may came up with a new Ansible version, as it worked before:
I'm passing this block to the ansible template
- monitoring-test-blackbox_exporter:
source: "{{ consul_template_template_dir }}/blackbox_exporter.ctmpl"
destination: "/etc/prometheus/file_sd/blackbox_exporter.json"
create_dest_dirs: true
command_timeout: "60s"
error_on_missing_key: false
grafana_link: "xtkCtBkiz"
This is the template:
# Template configuration
{% for ctmpl in consul_template_templates_config_node %}
# {{ ctmpl | first }}
template {
{% for option, value in ctmpl.items() %}
{% if value is sameas true %}
{{ option }} = true
{% elif value is sameas false %}
{{ option }} = false
{% elif value is string %}
{{ option }} = "{{ value|string }}"
{% elif value is number %}
{{ option }} = {{ value|int }}
{% endif %}
{% endfor %}
ctmpl | first always worked before to filter out first element monitoring-test-blackbox_exporter this is important as we use it later in the template configuration.
I tried several things with sort and select attributes neither of them worked. Does anyone have an idea to get it working again?

Jinja2 for item in list change to upper case

I am trying to generate configuration using jinja2 and ansible. ansible read csv and set facts.
{%- for item in facts_csv %}
Hello {{ item.abc}}
{% endfor %}
in the csv file "abc" has a value "mike". When i run the play book its generating "Hello mike".
Now i want to print "Hello MIKE", without changing the CSV file values?
seems item.abc|upper and item.abc.upper() did not work. Any other solutions?
The template works as expected
{%- for item in facts_csv %}
Hello {{ item.abc }}
{% endfor %}
{%- for item in facts_csv %}
Hello {{ item.abc|upper }}
{% endfor %}
gives
Hello mike
Hello MIKE

how to iterate csv file in ansible

i have a jinja2 template including a section that need data from a csv file
how can i read a csv file and split it into a list then iterate it in the jinja2 template? sth. like this:
{% for line in csv_data %}
{{ line[0] }} = {{ line[1] }}
{% endfor %}
in my task file, i am trying to use lookup to read the csv file into csv_data, but it seems lookup can only query and get one line not the whole file, or just the whole file in raw format
vars:
csv_data: "{{ lookup('file', 'test.csv') }}"
figured a not so good method:
{% for line in csv_data.split("\n") %}
{% set list = line.split(",") %}
{{ list[0] }}={{ list[1] }}
{% endfor %}

Resources