Configuring a Jinja template in Cisco DNAC Template editor - jinja2

I am configuring a Jinja template in Cisco DNAC Template editor to provision new switches. The problem I am having is getting Jinja to split the string variable within the Cisco Template tool. I tried using this split command.
{% set list1 = variable1.split(';') %}
This works fine if I hard code in the {{ HOSTNAME }} variable, for example using ABC-DEF-111-AS-10-11-12-13
{# {% set hname = '{{ HOSTNAME }}' %} #}
{% set hname = 'ABC-DEF-111-AS-10-11-12-13' %}
Device name is {{ hname }}
{% set list1 = hname.split('-') %}
{% for list in list1 %}
<p>{{ list }}</p>
{% endfor %}
The campus is {{ list1[0] }}
The building is {{ list1[1] }}
Room number is {{ list1[2] }}
Switch type is {{ list1[3] }}
IP address is {{ list1[4] }}.{{ list1[5] }}.{{ list1[6] }}.{{ list1[7] }}
However, when I use the {{ HOSTNAME }} variable to input the hostname, the split command will not split the hostname, instead it passes the hostname through in its entirity within {{ list1[0] }}. This happens even if I use the variable directly within the split command, for example.
{% set list1 = '{{ HOSTNAME }}'.split('-') %}
I have also tried splitting the resultant {{ list1[0] }} but it too just passes the hostname on through in its entirety.
I am not sure if this is a problem with the version of Jinja used in the Template Editor because I get a systax error when ever I tried using this split command:
{% set item1, item2 = variable1.split(';') %}
Is there away to get {{ HOSTNAME }} split? What am I missing?

The solution is that I should change the variable into a statement like this:
{% set list1 = **HOSTNAME**.split('-') %}

Related

how can i use jinja template and pillar in saltstack states

there is my sls file:
{% set java_script_path = salt['pillar.get']('script_path', default='/opt/java-app') %}
{% if salt['pillar.get']('script_path') %}
{% set file = {{ java_script_path }}/startup.sh %} ## seem this line have Jinja syntax error
{% if salt['file.file_exists']('{{ file }}') %}
cmd.run:
- name: mv {{ java_script_path }}/startup.sh {{ java_script_path }}/startup.sh.backup-$(date +"%Y-%m-%d-%H-%M-%S")
{% endif %}
{% endif %}
is using salt['pillar.get']('script_path') can not split other string?
example: name: {{ salt['pillar.get']('script_path') }}/startup.sh will raise error like: failed: Jinja syntax error: expected token ':', got '}' how can i fix ?
can you help me to fix my sls file to work?
{% already starts a Jinja context. You do not need to try to start another one with {{.
{% set file = java_script_path ~ "/startup.sh" %}
It was expecting a : because { starts a dict literal.
In simple use case such as shown in your question, you might not even need to set another variable at all. We could directly use {{ java_script_path }}/startup.sh in the if condition. Like below:
{% if salt['file.file_exists']("{{ java_script_path }}/startup.sh") %}
cmd.run:
- name: mv {{ java_script_path }}/startup.sh {{ java_script_path }}/startup.sh.backup-$(date +"%Y-%m-%d-%H-%M-%S")
{% endif %}
You could also reconsider using command to backup the file, and use an appropriate Saltstack module instead.

How can I use {{this}} as a parameter in a dbt jinja macro?

How can I pass the name of the current model using {{this}} as a parameter for a macro in a config function?
I have tried a couple of options and none of them works.
model/Table1.sql
{{ config(post_hook= calculate_test("{{this}}") ) }}
macro/calculate_test.sql
{% macro calculate_test(tableN) %}
{%- set tableName = tableN -%}
{% set sql %}
SELECT
COUNT(*) as cnt
FROM {{ tableName }}
{% endset %}
{% set results = run_query(sql) %}
{% endmacro %}
The error is:
You’re almost there! Only thing is that you’ve got extra curly brackets and misplaced quotes. The entire config block is Jinja so you don’t need the curly brackets around ‘this’.
{{ config(post_hook= “calculate_test(this)” ) }}

Passing list of Relation object to dbt_utils.union_relation macro fails

Related to dbt and jinja2
I am using union_relations from dbt_utils package (0.5.0).
I created my macro which takes list of fully qualified name (like database.schema.identifier) splits it and uses api.Relations.create (link) to create a relation and append each relation to a list.
{{ list_of_relation }} is given to dbt_utils.union_relations(as relations=my_macro([list of fully qualified names])), it's giving me an _is_relation error, I did use log to debug and see if it actually creates a relation and it does. What could be wrong?
It sounds like you have a macro written something like this:
{% macro my_macro(names) %}
{% set list_of_relations = [] %}
{% for name in names %}
{% set relation = something(name) %}
{% do list_of_relations.append(relation) %}
{% endfor %}
{{ list_of_relations }}
{% endmacro %}
Instead of using {{ list_of_relation }}, you’ll want {{ return(list_of_relation) }} or {% do return(list_of_relation) %}. The problem is that {{ ... }} turns things into strings in jinja macros, and macros by default return strings.
The documentation on return is here.

How to get datas with multiple variables in path with jekyll and liquid

At its most basic level I need to append a partial object path onto an existing object path. In this particular instance I can't use plugins.
Say you have an object path:
{{ site.data.grants.2015.Return.ReturnHeader.ReturnTypeCd }}
Which, of course, can also be referenced as follows:
{% assign var = "ReturnTypeCd" %}
{{ site.data.grants.2015.Return.ReturnHeader[var] }}
How would I go about adding additional levels of nesting to the variable?
{% assign xTest = "Return.ReturnHeader.ReturnTypeCd" %}
{{ site.data.grants.2015[xTest] }}
//does not work
I've played around with both dot and bracket notations and using append as well as capture, but can't seem to find a solution that works.
This works :
Data file is _data/grants.yml
"2015":
Return:
ReturnHeader:
ReturnTypeCd: "Et hop !"
Getting deep target with a "dotted" string :
{% assign dataPath = site.data.grants.2015 %}
{% assign target = "Return.ReturnHeader.ReturnTypeCd" %}
{% comment %} ++++ Transform target string to an array {% endcomment %}
{% assign labels = target | split:"." %}
{% comment %} ++++
Looping in labels array and reassigning dataPath on each loop.
This goes deeper and deeper in the data tree
++++ {% endcomment %}
{% for label in labels %}
<h2>Label : {{ label }}</h2>
{% assign dataPath = dataPath[label] %}
<p>dataPath : {{ dataPath }}</p>
{% endfor %}

ansible jinja2 concatenate IP addresses

I would like to cocatenate a group of ips into a string.
example ip1:2181,ip2:2181,ip3:2181,etc
{% for host in groups['zookeeper'] %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}
I have the above code, but can't seem to quite figure out how to concatenate into a string.
searching for "Jinja2 concatenate" doesn't give me the info I need.
Updated this answer, because I think I misunderstood your question.
If you want to concatenate the IP's of each host with some string, you can work with the loop controls, to check if you're in the last iteration:
{% for host in groups['zookeeper'] -%}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{%- if not loop.last %}, {% endif -%}
{%- endfor %}
Old answer:
The word you're looking for is join:
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] | join(", ") }}
You can use the 'extract' filter for this (provided you use ansible>=2.1):
{{ groups['zookeeper'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) | join(',') }}
More info:
http://docs.ansible.com/ansible/playbooks_filters.html#extracting-values-from-containers
Found a similar solution at https://adamj.eu/tech/2014/10/02/merging-groups-and-hostvars-in-ansible-variables/ .
I did a set_fact using a groups variable as suggested in the post:
- hosts: all
connection: local
tasks:
- set_fact:
fqdn_list: |
{% set comma = joiner(",") %}
{% for item in play_hosts -%}
{{ comma() }}{{ hostvars[item].ansible_default_ipv4.address }}
{%- endfor %}
This relies on joiner, which has the advantage of not having to worry about the last loop conditional. Then with set_fact I can make use of the new string in later tasks.