Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible jinja2 filters '|'(pipe) what does it mean?

Tags:

I have written a task as below but can not understand what '|' does?

tasks:  - shell: /usr/bin/foo    register: result    ignore_errors: True   - debug: msg="it failed"    when: result|failed   - debug: msg="it changed"    when: result|changed 

Also I have found some examples on web but can not understand what '|' does?

debug: msg={{ ipaddr |replace(",", ".") }} 

One more example:

- hosts: localhost   vars:     D:       1 : "one"       2 : "two"   tasks:     - debug: var=D     - debug: msg="D[1] is {{ D[1]|default ('undefined') }}" 

Would be great if someone can explain in details or point me to some URL?

Any help would be appreciated.

Thanks.

like image 206
Ram Meena Avatar asked Jun 17 '15 05:06

Ram Meena


People also ask

What are filters in Ansible?

Filters in Ansible are from Jinja2, and are used for transforming data inside a template expression. Jinja2 ships with many filters. See builtin filters in the official Jinja2 template documentation.

What are Jinja filters?

Jinja2 filter is something we use to transform data held in variables. We apply filters by placing pipe symbol | after the variable followed by name of the filter. Filters can change the look and format of the source data, or even generate new data derived from the input values.

What is Jinja2 used for in Ansible?

Ansible uses Jinja2 templating to enable dynamic expressions and access to variables and facts. You can use templating with the template module.


1 Answers

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

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.

Filters can be chained, passing the result from the first filter to the next and so forth. It works exactly like piping commands on a unix shell.

"value" | filter1 | filter2 | filterN 

The failed filter returns true if the passed result has failed. It simply checks the failed property from result.

The changed filter is the same, but checks if the passed result has changes. It checks the changed property from result.

ipaddr | replace(",", ".") replaces all occurrences of , with .. So a value of 127,0,0,1 will be transformed to 127.0.0.1.

The default filter will set a default value if the input was null, e.g. an undefined variable. undefined_var | default("var was undefined") -> This will either print the contents of undefined_var or the string "var was undefined". In your given example above you output the value of the 2nd element of D (D[1]) and if it does not exist the sting "undefined" instead.

like image 146
udondan Avatar answered Oct 19 '22 22:10

udondan