Jinja2 equalto( ) Test

Reference: Jinja

Check if an object has the same value as another object.

Example

As per the selectattr( )  page, the Jinja equalto( )  Test, as well as the Ansible match( )  and search( )  Tests all work in a similar fashion.

Using this dictionary:

Continue reading

Jinja2 default( ) Test

Example #1

Reference: Jinja

Syntax: default(value, default_value=u'', boolean=False)

If the value is undefined it will return the passed default value, otherwise the value of the variable:

This will output the value of my_variable  if the variable was defined, otherwise ‘my_variable is not defined’. If you want to use default with variables that evaluate to false  you have to set the second parameter to true:

Continue reading

Jinja2 Tests Overview

As per the Jinja documentation:

Beside “filters, there are also so-called “tests” available. Tests can be used to test a variable against a common expression.

Filters such as selectattr( )   can accept tests as a parameter. As per the documentation:

selectattr( ) filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding. If no test is specified, the attribute’s value will be evaluated as a boolean.

We seen an example of this on the selectattr ( )  page where the undefined  Test is passed as a parameter:

Continue reading

Jinja2 selectattr( ) Filter

As per the documentation:

selectattr( ) filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding. If no test is specified, the attribute’s value will be evaluated as a boolean.

Example #1

This blog post does a great job of demonstrating how to use selectattr( ) . Let’s take a look at how it works.

First, the author creates a “users” dictionary:

Continue reading

Jinja2 map( ) Filter

Reference: jinja

Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it.

The basic usage is mapping on an attribute. Imagine you have a list of users but you are only interested in a list of usernames:

Continue reading

Jinja2 Filter Overview

Reference: Stack Overflow

With the pipe character you pass a value to a filter. There are numerous Jinja 2 filters but Ansible brings some additional filters(Also see Conditionals.)

The term filter might be confusing at times because all the filters work very differently. Some for example reduce a result set of a hash/array, some modify contents of a string, but then there are filters which simply return true or false.

A better explanation might be that those are modifiers and they can do anything with your passed data. You can even write your own filters.

Continue reading

Ansible vars Lists

Lists can be provided to a module through vars  specified inside a Playbook or they can be provided directly inside a Task.

In the examples below we want to send two “show” commands to the devices. The former example uses a vars  list while the latter defines the list inside of the module call:

Continue reading