Conditional text in jinja2 templates for ansible - jinja2

I have playbook which may set lot of options for the daemon command line. I want to allow set them all from variables, but the same time I want to make them all optional.
Now I have template (j2) with all variables mandatory:
{% for router in flowtools.captures %}
-d {{router.debug_level}} -e {{router.expire_count}} -E {{router.expire_size}} -f {{router.fiter_name}} -F {{router.filter_definition}} -n {{router.rotations}} -N {{router.netsting_level}} -S {{router.start_interval}} -t {{router.tag_name}} -T {{router.active_def}} -V {{pdu_version}} -w {{router.workdir}} -x {{router.xlate_fname}} -z {{router.z_level}}
{% endfor %}
I want:
To allow undefined variables in 'router' (without failing a playbook).
Not to include option (-z, -b, etc) to the output if related variable is empty.
For example above, if flowtools.captures[0] contains only debug_level=2 and workdir=/tmp it should generate:
-d 2 -w /tmp.
I can add huge list of {% if %}'s but this would be very bulky. Is it possible to make it gracefully?

Well, after some struggle I made
vars.yml:
flowtools_capute_options:
byte_order: ' -b '
workdir: ' -w '
...etc...
template.j2:
{% for router in flowtools.captures %}
{% for opt in router %}
{{flowtools_capute_options[opt]}} {{-router[opt]-}}
{% endfor %}
{% endfor %}

Related

Using Saltstack cmd.script with args to Insert Jinja Variable

I am running a powershell script to obtain a particular user's credentials, and then use those credentials in a salt state. This works fine when the username is hardcoded directly in the powershell script. But, I am unable to pass the username as an argument. Here is my salt state:
{% set creds = salt['cmd.powershell']('C:\test2.ps1' 'username') %}
test_output:
cmd.run:
- name: echo {{ creds }}
I have also tried this too...but it doesn't work.
{% set creds = salt['cmd.script'](shell='powershell' source='C:\test2.ps1' args='username')
%}
How do i correctly pass an argument to my powershell script to set my variable?
The rules for strings and function calls are the same in Jinja as for Python. Either " or ' are the string delimiters, and \ is the escape character. Function arguments are separated by commas.
{% set creds = salt['cmd.powershell']('C:\\test2.ps1 username') %}
For cmd.script, the source should be a remote URL, not a local file.
{% set creds = salt['cmd.script'](shell='powershell', source='salt://path/test2.ps1', args='username') %}

Concatenating a variable in Jinja with a single quote

I'm having trouble trying to concatenate variables (one with a single quote) in Jinja. My code looks like this:
{%- set my_quote = "'" -%}
{%- set invocation = invocation_id -%}
And the output I'm attempting is this:
{{ invocation ~ my_quote }}
The output from this is:
f21f9039-44e5-452f-8d7a-ee64245ada23'
Ok great! Now when I attempt to add the single quote to the beginning as well:
{{ my_quote ~ invocation ~ my_quote }}
The output is the invocation variable value without any single quotes:
f21f9039-44e5-452f-8d7a-ee64245ada23
How can I get this to output both single quotes properly?
Try the following:
"'{{ invocation_id }}'"
oh I love this question and the great reproducible example you provided. First thought, what happens if you escape the ' like this?
{%- set my_quote = "\'" -%}
Is there a reason it has to be part of the variable?
Could you do:
'{{ invocation }}'

Saltstack - Unable to print output in Jinja templating

I am trying to reuse the output of a command in Saltstack, but when I try to print the output using "cmd.run", it is failing with the below error. Not sure in which format the data is getting returned from "cmd.run".
{% set output = salt['cmd.shell']('ifconfig') %}
display:
cmd.run:
- name: echo '{{ output }}'
Error:
Data failed to compile:
----------
Rendering SLS 'base:patching.install_patches' failed: mapping values are not allowed in this context
The issue seems to be due to the placement of quotes. The single quotes are required around the entire command to run.
Below should work:
{% set output = salt['cmd.shell']('ifconfig') %}
display:
cmd.run:
- name: 'echo "{{ output }}"'

Jinja2 if/else on user defined variable

Attempting to make a decision in a template based on the last character of a variable (third level domain hostname) , but the epiphany alludes me. Make a config stanza if value else, do the other.
I set a fact in play:
- name: Set third level domain name to a variable
set_fact:
my_3rd_levelname: "{{ ansible_nodename.split('.')[0] }}"
- name: Ascertain if which server we're on
set_fact:
my_one_or_two: "{{ my_3rd_levelname[-1]|int }}"
...which appears to echo out with debug, save the casting as an int...see below.
TASK [role-test : Echo out my_one_or_two] *******************************************************************************************************************
ok: [w.x.y.42] => {
"my_one_or_two": "2"
}
Then in the template.j2...
{# If my_one_or_two is even list server1 first. If not, second. #}
{% if lookup('vars,',my_one_or_two) + my_one_or_two|int is 1 %}
[some config file stanza here]
{% else %}
[some other config file stanza instead]
I've poked and hoped until I can stand it no longer and am reaching out. I've tried just using the raw variable, e.g., {% if my_one_or_two|int == 1 %} along with many other attempts, but I'm stuck. I can't seem to overcome this error:
AnsibleError: template error while templating string: expected token 'name', got 'integer'. String: [the contents of my template]
Any input would be greatly appreciated at this juncture.
Thanks
Okay...leaving this here in case someone else doesn't realize you can use any Python method that the object supports. Here's what I did. Remember the server names end in 1 or 2 and its a String.
Created a varible in /roles/[rolename]/vars...
my_simple_hostname: "{{ ansible_nodename.split('.')[0] }}"
Then used the 'endswith' method to evaluate it....
% if my_simple_hostname.endswith('1') == true %}
[content if true]
{% else %}
[content when false]
{% endif %}

SaltStack cmd.run with curl and json body

I have a trivial task that I am stuck to write salt state for. I need to call REST endpoint using curl with json body. This is
curl localhost/endpoint -d '{"some" : "data"}'
My idea was to simply take this and put it into salt state by using cmd.run. Does not work. So far I have this:
{%- set data = {'some': 'data'} %}
Use echo instead of curl:
cmd.run:
- name: echo '{{ data|json }}'
And this gives me
failed: Unknown yaml render error; line 5
Use echo instead of curl:
cmd.run:
- name: echo '{"some": "data"}' <======================
I have Salt version 2014.7.1
For me the problem was the ":" within the curl command that was interpreted as YAML (see: How to escape indicator characters (i.e. : or - ) in YAML)
I ended up using the multi-line approach. That allows me to write the command with no escaping while variables (e.g pillar data) are still interpreted correctly.
E.g.
Salt state description:
cmd.run:
- name: >-
curl -X GET "https://api.example.com/client/{{ pillar['client_id'] }}" -H "X-Auth-Email: name#example.co.za" -H "X-Auth-Key: {{ pillar['api_key'] }}" -H "Content-Type: application/json" --data '{"some_json":true}'
When working with json it is sometimes easier to avoid the jinja renderer altogether. The following example uses the pybojects renderer (which is nice for a lot of other reasons too).
echo.sls:
#!pyobjects
import json
data = {'some': 'data'}
def dump(d):
return "'" + json.dumps(d).replace("'", "'\\''") + "'"
Cmd.run("echo {}".format(dump(data)))
Note that the custom dump function definition and usage is added for the sake of completeness.