Jinja using a variable as the name of the file used in a Jinja "include" - 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?

Related

Ansible endfor don't ending loop

I have Jinja2 template like this
parameter={% for name in parameter %}{{ name.value }},{% endfor %}
and when I add into this template static parameter like eg.
parameter={% for name in parameter %}{{ name.value }},{% endfor %}
parameter2=value2
after templating I have output like this:
parameter=value1,value2,value3,parameter2=value2
but I need something like this
parameter=value1,value2,value3,
parameter2=value2

How to trim last character when using ansible jinja loop

My template like as blow
{% if hostvars[inventory_hostname].local_zk_server_id is defined %}
zookeeperServers={% for host in {{ groups[{{ target_hosts }}] %}}
"{{ hostvars[host].inventory_hostname }}:2181,"
{% endfor %}
{% endif %}
output ishost1:2181,host2:2181,host3:2181,
How to trim last comma
There are several possible gotchas in your above template regarding variables access. Moreover, rather than trimming the last character in your string, the best solution is probably not to write it. Here is a better solution IMO in my below example fixing all the problems I'm referring to:
{% set zookeeperServers=[] %}
{% if hostvars[inventory_hostname].local_zk_server_id is defined %}
{% for host in groups[target_hosts] %}
{% zookeeperServers.append(hostvars[host].inventory_hostname + ":2181") %}
{% endfor %}
zookeeperServers="{{ zookeeperServers | join(',') }}"
{% endif %}

Ansible dictionary not working when we have multiple strings in the list

I have dictionary to loop multiple strings in the list, if I provide 2 or more then it's always reading last value in the list, please suggest me.
- set_fact:
env_microservice_variable_map: |
{% set res = [] -%}
{% for microservice_name in MICROSERVICE_NAMES -%}
{% if microservice_name in MICROSERVICE_ENV_MAP -%}
{% set microservice_envs = MICROSERVICE_ENV_MAP[microservice_name] -%}
{% else -%}
{% set microservice_envs = env_variable_map.keys() -%}
{% endif -%}
{% for env in microservice_envs -%}
{% set variables = env_variable_map[env] -%}
{% set ignored = variables.__setitem__("MICROSERVICE_NAME", microservice_name) -%}
{% set ignored = res.extend([variables]) -%}
{%- endfor %}
{%- endfor %}
{{ res }}
- name: Copy values file
command: cp {{dir_path}}/helm/{{item.MICROSERVICE_NAME}}/values-template.yaml {{dir_path}}/helm/{{item.MICROSERVICE_NAME}}/values-{{item.EXEC_ENV}}-{{item.EXEC_REGION}}.yaml
with_items: "{{ env_microservice_variable_map }}"
become_user: jenkins
First one is set_fact where it has mapping.
The 2nd task should be able to loop when we have multiple strings in the variable defined "MICROSERVICE_NAMES"
There is ansible command am running, it's always reading last string in the List(read-service), please help me, thanks.
ansible-playbook generate_values_files.yml -i hosts --extra-vars "#generate_values_files_variable.yml" --extra-vars="{"'"MICROSERVICE_NAMES"'":{'processor-create','processor-update','read-service'}}" '--extra-vars={"MICROSERVICE_ENV_MAP":{}}'
Varibales:
dir_path: /jenkins
EXEC_ENV: dd
EXEC_REGION: west
Basically we have multiple directories
1. /jenkins/helm/processor-create/values-template.yml
2. /jenkins/helm/processor-update/values-template.yml
3. /jenkins/helm/read-service/values-template.yml
Each folder has values-template.yml file init when i run above script it has to create multiple files based above template file in each folder.
1. /jenkins/helm/processor-create/values-template.yml
values-dd-west.yml
values-mm-west.yml
values-gg-west.yml
2. /jenkins/helm/processor-update/values-template.yml
values-dd-west.yml
values-mm-west.yml
values-gg-west.yml
3. /jenkins/helm/read-service/values-template.yml
values-dd-west.yml
values-mm-west.yml
values-gg-west.yml
Problem here is when i run above ansible tasks it's always generating files for last service in the list :"read-service".
I suspect you found the well-known (to those who was unlucky to find it) WTF of the jinja2.
If you set some variables inside the loop, they live only within this loop. You need to initialize a container (list or dict) outside of the loop and add items into it to get something out of the loop.

How to set concatenated string variable in Ansible template based on a condition

I need to create a string in a template that can change between hosts, and it needs to be in the form of:
"cores": "0,1,2,3"
And the reason the string is "0,1,2,3" in this example is because the host has 4 processor cores.
So I got stuck with something which seems too convoluted to me and I'm not even sure how to use this core_count variable in my template file.
{% set core_count = '' %}
{% for i in range(ansible_processor_cores) %}
{% set core_count = core_count ~ i %}
{% if not loop.last %}
{% set core_count = core_count ~ ',' %}
{% endif %}
{% endfor %}
There are many handy lookup plugins in Ansible. Take sequence:
- hosts: localhost
gather_facts: yes
tasks:
- debug:
msg: '"cores": "{{ lookup("sequence","start=0 count="+(ansible_processor_cores|string)) }}"'

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