Ansible jinja2过滤器'|'(pipe)是什么意思? [英] Ansible jinja2 filters '|'(pipe) what does it mean?

查看:213
本文介绍了Ansible jinja2过滤器'|'(pipe)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个下面的任务,但是不明白什么'|'是吗?

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(",", ".") }}

另一个例子:

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

如果有人可以详细解释或指向我一些URL会很棒吗?

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

任何帮助将不胜感激.

谢谢.

推荐答案

使用竖线字符将值传递给过滤器.有很多 Jinja 2过滤器,但是Ansible

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

过滤器一词有时会令人困惑,因为所有过滤器的工作方式都大不相同.例如,有些会减少哈希/数组的结果集,有些会修改字符串的内容,但是有些过滤器只会返回true或false.

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.

可以链接过滤器,将结果从第一个过滤器传递到下一个过滤器,依此类推.它的工作原理与unix shell上的管道命令类似.

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

如果传递的结果失败,则failed过滤器将返回true.它只是从result检查failed属性.

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

changed过滤器是相同的,但是检查传递的结果是否有更改.它从result检查changed属性.

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

ipaddr | replace(",", ".").替换所有出现的,.因此,值127,0,0,1将转换为127.0.0.1.

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

如果输入为null,例如default过滤器将设置默认值.未定义的变量. undefined_var | default("var was undefined")->这将打印undefined_var的内容或字符串"var is undefined".在上面给出的示例中,您输出D(D[1])的第二个元素的值,如果不存在,则返回字符串"undefined".

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.

这篇关于Ansible jinja2过滤器'|'(pipe)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆