I am attempting to past a nested list into jinja to create a configuration file for Telegraf using the SNMP input plug-in.
So far I have been able to parse the following simple list into a suitable configuration file.
List:
- name: CpuUsage
oid: 1.3.6.1.4.1.5951.4.1.1.41.1.0
- name: mgmt_CPU
oid: 1.3.6.1.4.1.5951.4.1.1.41.6.1.2.8.77.103.109.116.32.67.80.85
- name: MemUsage
oid: 1.3.6.1.4.1.5951.4.1.1.41.2.0
- name: TotRxMbits
oid: 1.3.6.1.4.1.5951.4.1.1.43.27.0
- name: TotTxMbits
oid: 1.3.6.1.4.1.5951.4.1.1.43.30.0
- name: httpTotRequestsRate
oid: 1.3.6.1.4.1.5951.4.1.1.48.76.0
- name: tcpCurClientConnEstablished
oid: 1.3.6.1.4.1.5951.4.1.1.46.12.0
- name: tcpCurServerConnEstablished
oid: 1.3.6.1.4.1.5951.4.1.1.46.10.0
- name: tcpCurClientConnClosing
oid: 1.3.6.1.4.1.5951.4.1.1.46.9.0
into the following config:
[[inputs.snmp.field]]
name = "CpuUsage"
oid = "1.3.6.1.4.1.5951.4.1.1.41.1.0"
conversion = "int"
[[inputs.snmp.field]]
name = "mgmt_CPU"
oid = "1.3.6.1.4.1.5951.4.1.1.41.6.1.2.8.77.103.109.116.32.67.80.85"
conversion = "int"
[[inputs.snmp.field]]
name = "MemUsage"
oid = "1.3.6.1.4.1.5951.4.1.1.41.2.0"
conversion = "int"
[[inputs.snmp.field]]
name = "TotRxMbits"
oid = "1.3.6.1.4.1.5951.4.1.1.43.27.0"
conversion = "int"
[[inputs.snmp.field]]
name = "TotTxMbits"
oid = "1.3.6.1.4.1.5951.4.1.1.43.30.0"
conversion = "int"
[[inputs.snmp.field]]
name = "httpTotRequestsRate"
oid = "1.3.6.1.4.1.5951.4.1.1.48.76.0"
conversion = "int"
[[inputs.snmp.field]]
name = "tcpCurClientConnEstablished"
oid = "1.3.6.1.4.1.5951.4.1.1.46.12.0"
conversion = "int"
[[inputs.snmp.field]]
name = "tcpCurServerConnEstablished"
oid = "1.3.6.1.4.1.5951.4.1.1.46.10.0"
conversion = "int"
[[inputs.snmp.field]]
name = "tcpCurClientConnClosing"
oid = "1.3.6.1.4.1.5951.4.1.1.46.9.0"
conversion = "int"
Where I am stuck is with the following list of lists
netscaler_citrix_vserver:
- name: name
oid: NS-ROOT-MIB::vsvrName
is_tag: "true"
- name: fullName
oid: NS-ROOT-MIB::vsvrFullName
is_tag: "true"
- name: requests
oid: NS-ROOT-MIB::vsvrTotalRequests
- name: state
oid: NS-ROOT-MIB::vsvrState
- name: servicesUp
oid: NS-ROOT-MIB::vsvrCurServicesUp
netscaler_citrix_vservice:
- name: vserver
oid: NS-ROOT-MIB::vserverFullName
is_tag: "true"
- name: vservice
oid: NS-ROOT-MIB::vsvrServiceFullName
is_tag: "true"
- name: hits
oid: NS-ROOT-MIB::vsvrServiceHits
I've tried a number of variations, but each proving unsuccessful. I'm trying to get the following output:
[[inputs.snmp.table]]
name = "netscaler_citrix_vserver"
# oid omitted, to allow collection of specific columns only
[[inputs.snmp.table.field]]
name = "name"
oid = "NS-ROOT-MIB::vsvrName"
is_tag = true
[[inputs.snmp.table.field]]
name = "fullName"
oid = "NS-ROOT-MIB::vsvrFullName"
is_tag = true
[[inputs.snmp.table.field]]
name = "requests"
oid = "NS-ROOT-MIB::vsvrTotalRequests"
[[inputs.snmp.table.field]]
name = "state"
oid = "NS-ROOT-MIB::vsvrState"
[[inputs.snmp.table.field]]
name = "servicesUp"
oid = "NS-ROOT-MIB::vsvrCurServicesUp"
[[inputs.snmp.table]]
name = "netscaler_citrix_vservice"
# oid omitted, to allow collection of specific columns only
[[inputs.snmp.table.field]]
name = "vserver"
oid = "NS-ROOT-MIB::vserverFullName"
is_tag = true
[[inputs.snmp.table.field]]
name = "vservice"
oid = "NS-ROOT-MIB::vsvrServiceFullName"
is_tag = true
[[inputs.snmp.table.field]]
name = "hits"
oid = "NS-ROOT-MIB::vsvrServiceHits"
Basically I need to reference the top-level entry and for each top-level entry list the sub-level entries also.
Any help would be appreciated.
the code used to create the first list is pretty straightforward, as follows:
[[inputs.snmp.field]]
name = "{{ item.name }}"
oid = "{{ item.oid }}"
{% endfor %}
for the SNMP table code I've resorted to explicitly declaring the table name as follows:
[[inputs.snmp.table]]
name = "netscaler_citrix_vserver"
{% for item in netscaler_citrix_vserver %}
[[inputs.snmp.table.field]]
name = "{{ item.name }}"
oid = "{{ item.oid }}"
{% if item.is_tag is defined %}
is_tag = {{item.is_tag}}
{% endif %}
{% endfor %}
[[inputs.snmp.table]]
name = "netscaler_citrix_vservice"
{% for item in netscaler_citrix_vservice %}
[[inputs.snmp.table.field]]
name = "{{ item.name }}"
oid = "{{ item.oid }}"
{% if item.is_tag is defined %}
is_tag = {{item.is_tag}}
{% endif %}
{% endfor %}
Of course as soon as I posted the answer from another question in related questions popped up. Resolved with the following code
{% for input_snmp_table in input_snmp_tables recursive %}
[[inputs.snmp.table]]
name = {{ input_snmp_table.table_name }}
{% for input_snmp_table_fields in input_snmp_table.input_snmp_table_fields %}
[[inputs.snmp.table.field]]
name = "{{ input_snmp_table_fields.name }}"
oid = "{{ input_snmp_table_fields.oid}}"
{% if input_snmp_table_fields.is_tag is defined %}
is_tag = {{input_snmp_table_fields.is_tag}}
{% endif %}
{% endfor %}
{% endfor %}```
Related
I want to get the output of information after comparing whether the region is the capital?
Help me figure out how to use "lookup" correctly?
{% if capital == lookup('vars', item) %} yes {% else %} no {% endif %}
or
{% if capital.action == {{ item }} %} yes {% else %} no {% endif %}
I get the following error
failed: [localhost] (item=region1a) => {"ansible_loop_var": "item", "changed": false, "item": "region1a", "msg": AnsibleError: template error while templating string: expected token ':', got '}'
here {{ item }} is a variable = region1a
I have these variables
vars:
AllCountry:
- name1
- name2
name1:
- region1a
- region1b
name2:
- region2a
- region2b
capital:
- region1a
- region2a
what am I wrong about?
It seems to me like this is what you are looking to achieve:
{% if item in capital %} yes {% else %} no {% endif %}
But that is really not foolproof. Imagine you have two regions named the same in two countries and one is the capital of one of the countries when the other is not the capital of the other one, how would you do it here?
I would really go with a more tied kind of dictionary like:
countries:
country1:
regions:
- region1
- region2
- region3
capital: region1
country2:
regions:
- region4
- region5
capital: region5
But without your actual use case and what you are trying to build with all this, this is hard to advise on the right type of data structure to construct.
Who can tell you how to implement the output of all regions in the name1 group when entering a template named region1a, and when entering a template named region2b, output all regions from the name2 group
I implement it like this:
there is a task that starts template generation:
vars:
AllCountry:
- name1
- name2
name1:
- region1a
- region1b
name2:
- region2a
- region2b
tasks:
- name:
template:
src: "regions.j2"
dest: "{{ item }}.conf"
loop:
- region1a
- region2b
---regions.j2---
regions [{%for count in name1%} "my country = {{count}}", {%end for %}]
this gives the desired output, but only because it is explicitly specified for which name (1 or 2) to output
regions "my country = region1a", "my country = region1b"
For each value specified in the loop, a template configuration file must be generated.
When you specify values in loop region1a and region1b template should generate only one row in the configuration file for region1a.conf
regions "my country = region1a", "my country = region1b"
for region1b generate only one row in the configuration file for region1b.conf
regions "my country = region1a", "my country = region1b"
User β.εηοιτ.βε a more optimal structure was proposed. If convenient, you can use it.
vars:
countries:
country1:
regions:
- region1
- region2
- region3
capital: region1
country2:
regions:
- region4
- region5
capital: region5
Thank you all for your help. Still, I managed to figure it out myself.
Here is the final solution:
{% for country in AllCountry %}
{% if item in lookup('vars', country) %}{% for count in lookup('vars', country) %} "My country = {{ count }}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}{% endfor %}
Have a file that contains line in a format like below:
category1 name=item1
category1 attributes=attr1, attr2
category1 name=item2
category1 attributes=attr1, attr2, attr3
category2 name=item1
category2 attributes=attr1
Looking to get a dictionary in ansible like below:
- category1:
- name: item1
attributes: [attr1, attr2]
- name: item2
attributes: [attr1, attr2, attr3]
- category2:
- name: item1
attributes: [attr1]
I was trying with jinja2 and below is what I get as of now. Problem is that the code is getting complicated after every step. Is there any way using filter or other methods that could produce the expected result with more readable code?
Code:
- set_fact:
converted_data: >-
{%- set res = dict() -%}
{%- for line in content.stdout_lines -%}
{%- set key_val = line.split(' ', 1) -%}
{%- set second_level = [] -%}
{%- if res[key_val[0]] is defined -%}
{%- set second_level = res[key_val[0]] -%}
{%- endif -%}
{%- set val_split = key_val[1].split('=') -%}
{%- set _ = second_level.append( dict({ val_split[0]: (val_split[1].split(',') | map('trim') | list) }) ) -%}
{%- set _ = res.update({key_val[0]: second_level}) -%}
{%- endfor -%}
{{ res }}
Produces:
category1:
- name:
- item1
- attributes:
- attr1
- attr2
- name:
- item2
- attributes:
- attr1
- attr2
- attr3
category2:
- name:
- item1
- attributes:
- attr1
I would strongly advise you to stop writing this in Jinja and switch to Python. It can be a lookup plugin, a module, or just a very simple script to read from stdin and output a json.
Jinja is not designed for algorithms. Whilst you can implement any efficiently computable function using the turing-complete Jinja2 template language, it's going to be very very painful for you and for anyone else reading your code.
If you do it in Python, you can add few tests (just define few 'test_case1', 'test_case2' functions inside your script and use pytest to run them). Without tests you never will get a stable code for all cases (like if some line is missing, or there is a reordering, or duplicates, etc).
I am running an Ansible playbook. In a YAML file "jinja.yaml", I have the following jinja2 template.
{% set cnt = 0 %}
{% for x in range(4, 20) %}
{% for y in range(1, 251) %}
- pool_name: pool_{{ cnt }}
{% set cnt = cnt + 1 %}
pool_member: 10.30.{{ x }}.{{ y }}
{% endfor %} {% endfor %}
Here is the snippet in the Ansilbe code:
tasks:
- name: XXX
set_fact:
members: "{{ lookup('template', 'jinja.yaml') | from_yaml}}"
When I ran the Ansible playbook, I got the following error message:
return loader.get_single_data()
File "/usr/lib/python2.7/dist-packages/yaml/constructor.py", line 37, in get_single_data
node = self.get_single_node()
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 36, in get_single_node
document = self.compose_document()
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 55, in compose_document
node = self.compose_node(None, None)
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 82, in compose_node
node = self.compose_sequence_node(anchor)
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 111, in compose_sequence_node
node.value.append(self.compose_node(node, index))
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 84, in compose_node
node = self.compose_mapping_node(anchor)
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 127, in compose_mapping_node
while not self.check_event(MappingEndEvent):
File "/usr/lib/python2.7/dist-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/usr/lib/python2.7/dist-packages/yaml/parser.py", line 428, in parse_block_mapping_key
if self.check_token(KeyToken):
File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 116, in check_token
self.fetch_more_tokens()
File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 220, in fetch_more_tokens
return self.fetch_value()
File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 576, in fetch_value
self.get_mark())
ScannerError: mapping values are not allowed here
in "<unicode string>", line 2, column 32:
pool_member: 10.30.4.1
^
fatal: [10.6.177.160]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
I am 99% sure it's the syntax error in the jinja.yaml but I just did not fix it. Any help will
appreciate it.
You are solving the wrong problem; if you want a list[dict], then don't try to build yaml text using jinja templating only to later convert it into the actual data structure you wanted: just construct the list[dict] you want without the intermediate serialization:
- name: XXX
set_fact:
members: >-
{%- set cnt_holder = {"cnt": 0} -%}
{%- set results = [] -%}
{%- for x in range(4, 20) -%}
{%- for y in range(1, 251) -%}
{%- set _ = results.append({
"pool_name": ("pool_%d" | format(cnt_holder.cnt)),
"pool_member": ("10.30.%d.%d"|format(x, y)),
}) -%}
{%- set _ = cnt_holder.update({"cnt": cnt_holder.cnt + 1}) -%}
{%- endfor -%}
{%- endfor -%}
{{ results }}
As you'll observe, your original code block had a bug: one cannot reassign variables (which goes doubly so from within a lexical block like a for loop); that "feature" of jinja2 is actually documented in the fine manual
However, you can mutate existing data structures (in fact that is the whole point of results.append). So, we are side-stepping jinja2's set behavior and storing the mutable cnt in a global dict so we can mutate it. That's why my {% set cnt_holder differs from your syntax
I'm using nunjucks to create a JSON export file. I have a list of variables that have the same name except for an incrementing number on the end. I'm using a for loop in the following way:
{% for i in range(1, 6) -%}
{% set items = ["{{ answer",i, " }}"] %}
"Solution{{ i }}" : "{{ items | join }}",
{%- endfor %}
I want answer1 to grab the variable answer1, but instead it is giving me a string "{{ anwser1 }}" .
Any idea how to use the for loop to point to each variable (answer1, answer2, answer3, etc)?
You can add some global function or filter to get access to a context (vars scope) by name.
const nunjucks = require('nunjucks');
const env = new nunjucks.Environment();
env.addGlobal('getContext', function (prop) {
return prop ? this.ctx[prop] : this.ctx;
});
const tpl = `{% for i in range(1, 4)%} {{ getContext('a' + i) }} {% endfor %}`;
const output = env.renderString(tpl, {a1: 10, a2:20, a3: 30, b: 1, c: 2});
console.log(output);