Creating text file from jinja template with inputs from csv file - ansible

I'm trying to create a text file which goes through the CSV file and populates variables into specific fields.
playbook.yml:
- hosts: localhost
tasks:
- name: "Reading user information"
read_csv:
path: /home/test/vlans.csv
delimiter: ','
register: vlans
- debug: var=vlans
- name: Creating VLANs configuration
template:
src: vlan.conf.j2
dest: /tmp/vlan.conf
Jinja2 Template vlan.conf.j2:
{% for item in vlans %}
!
vlan {{ item.VLAN }}
name {{ item.Description }}
vn-segment {{ item.VNI }}
interface nve1
member vni {{ item.VNI }}
{% endfor %}
and this is a test vlans.csv file:
Tenant,VRF ,VLAN,VNI,Subnet,Description,Good to go
Test,,5,20005,,LAB-Checkpoint-FW-Mgmt,Yes
Test,,208,20208,,LAB-DMZ,Yes Test,,209,20209,,LAB-CSR-MGMT,Yes
Test,10000,210,20210,192.168.12.1/28,LAB-VRF-to-FW,Yes
Prod,,761,20761,,PROD-CORE,Yes
Prod,105,840,20840,172.18.33.1/24,Backups,Yes
Prod,,841,20841,,Transport,Yes
I want to end up with file like in jinja2 template and not repeating line "interface nve 1"

In your question, interface nve1 is inside a loop. It will repeat multiple times in the resulting vlan.conf file.
Use multiple loops in the jinja template to decide what is repeated and what isn't:
{% for item in vlans.list %}
vlan {{ item.VLAN }}
name {{ item.Description }}
vn-segment {{ item.VNI }}
{% endfor %}
interface nve1
{% for item in vlans.list %}
member vni {{ item.VNI }}
{% endfor %}
Note that I've referred to vlans.list in the start of the loops instead of vlans. This is correct as per documentation, but different to the example in the question so it may need adjusting.

Related

"AnsibleError: template error while templating string: unexpected 'end of statement block'

I have a jinja2 template that looks like this:
token: {{ RANCHER_SERVER_TOKEN}}
server: https://{{ RANCHER_RKE2_SERVER }}:9345
{% endif %}{% if RANCHER_TLS_SAN is defined %}
tls-san:{% for item in RANCHER_TLS_SAN +%}
- {{ item }}{% endfor +%}
{% endif %}
{% if RANCHER_NODE_LABELS is defined %}
node-label:{% for item in RANCHER_NODE_LABELS +%}
- {{ item }}{% endfor +%}
{% endif %}
{% if RANCHER_NODE_TAINTS is defined %}
node-taint:{% for item in RANCHER_NODE_TAINTS +%}
- {{ item }}{% endfor +%}
{% endif %}
in my inventory group in group_vars I have this data structure
---
RANCHER_TLS_SAN:
- k3-crontroller-1.prometheus.internal
RANCHER_NODE_TAINTS:
- "CriticalAddonsOnly=true:NoExecute"
I test it through this link: https://ansible.sivel.net/test/
and it renders out great.
However I get this issue when running the playbook:
fatal: [k3-rancher-controller-1]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: unexpected 'end of statement block'. String: {%if RANCHER_DOWNSTREAM_SERVER_AGENT_NODE is defined %}\ntoken: {{ RANCHER_SERVER_TOKEN }}\nserver: https://{{ RANCHER_RKE2_SERVER }}:9345\n{% endif %}{% if RANCHER_TLS_SAN is defined %}\ntls-san:{% for item in RANCHER_TLS_SAN +%}\n - {{ item }}{% endfor %}\n{% endif %}\n{% if RANCHER_NODE_LABELS is defined %}\nnode-label:{% for item in RANCHER_NODE_LABELS +%}\n - {{ item }}{% endfor %}\n{% endif %}\n{% if RANCHER_NODE_TAINTS is defined +%}\nnode-taint:{% for item in RANCHER_NODE_TAINTS +%}\n - {{ item }}{% endfor %}\n{% endif %}"}
My playbook looks like this:
---
- name: Create VM for Rancher Kubernetes Control Plane Nodes
hosts: master
gather_facts: no
become: yes
become_user: root
tasks:
- name: create directory /etc/rancher/rke2
file:
path: /etc/rancher/rke2
state: directory
- name: Create template and copy to Agent nodes
template:
src: playbooks/templates/config.j2
dest: /etc/rancher/rke2/config.yaml
I am using ansible 2.9.27.
Does anyone know what is going on?
Any help is much appreciated. Many thanks

Problems with Jinja2 and ansible making a sub dict

I need to read a csv file with diferent IPs and make a dictionary with a jinja2 filter for modificate the IP depending the IPNumber value. The yml file is like:
- read_csv:
path: vms.csv
key: Number
fieldnames: Name,IP1,IP2,IP3,IP4,IPNumber
delimiter: ';'
register: vms
- name: vms to dict
debug:
msg:
- {{'Name':{{ item.value.Name }},
{% if item.value.IPNumber == "1" %}
'IP':{{ item.value.IP1 }},
{% endif %}
{% if item.value.IPNumber == "2"%}
'IP':{{ item.value.IP2 }},
{% endif %}
{% if item.value.IPNumber == "3"%}
'IP':{{ item.value.IP3 }},
{% endif %}
{% if item.value.IPNumber == "4"%}
'IP':{{ item.value.IP4 }},
{% endif %}}}
loop: "{{ vms.dict | dict2items }}"
register: vms2
But I get the error:
The error appears to be in '/etc/ansible/roles/vms.yml': line 17, column 16, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
'Name':{{ item.value.Name}},
{% if item.value.IPNumber == "1" %}
^ here
I know is a syntax problem but I dont guess where the problem is.
I need some help.
The following task should create your dictionary as per your requirement inside a var you can reuse elsewhere. Rename my_ip_dict to whatever suits your project better.
- name: Create my IP dictionary
set_fact:
my_ip_dict: >-
{{
my_ip_dict | default({})
| combine({item.value.Name: item.value['IP' + item.value.IPNumber]})
}}
loop: "{{ vms.dict | dict2items }}"
- name: Check the result:
debug:
var: my_ip_dict
Note that I have dropped all the if/else structures by calling directly the correct field depending on IPNumber. I took for granted it always has a value in the valid scope or the other existing IP* fields. If this is not the case, you can always default that value e.g. item.value['IP' + item.value.IPNumber] | default('N/A')
You should put only variables/expressions within {{ or {%. To me 'Name' looks like normal text and should be outside.
Example:
# Notice the quotes `"` symbol at the beginning and end of debug message
- debug:
msg:
- "Name: {{ item.value.Name }},
{% if item.value.IPNumber == "1" %}
IP: {{ item.value.IP1 }}
# and so on...
{% endif %}"
This at least should address the error message.

Ansible Jinja2 template for loop

I have two linux servers:
- server1: ip: 10.241.55.6, hostname: server1
- server2: ip: 10.242.55.7, hostname: server2
I have created an ansible inventory file named servers with the content bellow:
[IC]
10.241.55.6
10.241.55.7
Now I have created this jinja2 inventory template file: test.j2 with this content:
[IC]
{% for hostip in groups['IC'] %}
{% if hostip == ansible_default_ipv4.address %}
{{ ansible_default_ipv4.address }} default_hostname={{ ansible_nodename }}
{{ ansible_default_ipv4.address }} default_hostname={{ ansible_nodename }}
{% endif %}
{% endfor %}
And I'm running this ansible playbook:
---
- name: Generate portal inventory file
hosts: all
tasks:
- name: Generate inventory
delegate_to: localhost
template:
src: inventory/test.j2
dest: inventory/test
The command is: ansible-playbook -i inventory/servers generate-inventory.yml
The final goal is that ansible connects to each of the servers from the inventory files and then based on the jinja2 inventory template, it creates a new inventory file with this format:
[IC]
10.241.55.6 default_hostname=hostname_of_the_server_with_that_ip
and so on...
The issue here with the for loop is that all the entries are with the same server ip (while I should have an entry for each of the servers with their respective hostnames):
[IC]
10.241.55.6 default_hostname=server1
10.241.55.6 default_hostname=server2
What I'm missing here? Also if there is any other better way to achieve this please let me know.
You're using the same variable twice in the template...
{{ ansible_default_ipv4.address }} default_hostname={{ ansible_nodename }}
{{ ansible_default_ipv4.address }} default_hostname={{ ansible_nodename }}
...so of course you're getting two identical lines. It sounds like you want to access the per-host value of this variable, which means you need to access it via hostvars.
Maybe something like this:
[IC]
{% for host in groups['IC'] %}
{{ hostvars[host].ansible_default_ipv4.address }} default_hostname={{ hostvars[host].ansible_nodename }}
{% endfor %}

How to set a value in defaults/main.yml dynamically from inventory

I have a dynamic inventory for my project and on deployment the host_groups get loaded with inventory list. Meantime, I want to set a variable named master_ip with value from host_vars [master] in a role/defaults/main.yml.
My hosts are grouped as below:
[master]
xx.eco.project.com
[slave]
yy.eco.project.com
zz.eco.project.com
As i cannot use if/else in YAML. Like in templates i use,
{% if inventory_hostname in groups['master'] %}
master_ip: {{ lookup('dig', inventory_hostname) }}
as master_ip: 10.0.1.1. How is it possible?
I have solved this issue by defining a macro function in templates to determine the master and return the value to the template variables for master_ip. I find this more meaningful than the initial idea of getting the value in defaults/main.yml .
{% macro master_ip() -%}
{% for inventory_hostname in groups['master'] -%}
{{ lookup('dig', inventory_hostname) }}
{%- endfor -%}
{% endmacro -%}
Use in template
bind {{ master_ip() }}

How to create dynamic files?

i´m trying to rename ethernet-interfaces on Linux. Each interface with the name enp0s* have to be eth*. First, i create udev-rules for renaming. that works fine. Secondly i have to create new configuration-files for each interface ( ' etc/sysconfig/network-scripts/ifcfg-eth* ' ). I don´t know, how to create the loop to place parameters in each interface. Can somneone help me?
Thats an extract of my playbook:
- name: Get new interface-names of the new created udev-rules
become: yes
shell: cat /etc/udev/rules.d/70-persisten-ipoib.rules.cfg | egrep -i 'eth.'
register: car
- name: Create new-ifcfg-eth* files
template:
with_items: "{{ car.stdout_lines }}"
register: cat
template: src= roles/configure_network/templates/create_ifcfg.cfg.j2 dest=/etc/sysconfig/network-scripts/ifcfg-{{ item }}
# Template: roles/configure_network/templates/create_ifcfg.cfg.j2
{% for interface in cat.results %}
NAME="eth{{ item.name }}
TYPE=Ethernet
BOOTPROTO={{item.bootproto|default('dhcp')}}
IPV4_FAILURE_FATAL=no
IPV6INIT=no
{% if item.ipaddress is defined %}
IPADDR={{item.ipaddress}}
{% endif %}
{% if item.netmask is defined %}
NETMASK={{item.netmask}}
{% endif %}
{% if item.gateway is defined %}
GATEWAY={{item.gateway}}
{% endif %}
PEERDNS=no
{% if item.dns is defined %}
DNS1={{item.dns}}
{% endif %}
ONBOOT={{item.onboot|default('yes')}}
{% endfor %}
Just fix your syntax:
- name: Create new-ifcfg-eth* files
template:
src: create_ifcfg.cfg.j2
dest: /etc/sysconfig/network-scripts/ifcfg-{{ item }}
with_items: "{{ car.stdout_lines }}"
register: cat
Remove double template call, use relative path (no need to define full path to role's own templates), use YAML syntax instead of key=value (you had spaces in them, which are not allowed).

Resources