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

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

问题描述

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

任务:- 外壳:/usr/bin/foo注册:结果ignore_errors: 真- 调试:味精=失败"时间:结果|失败- 调试:味精=它改变了"时间:结果|改变

我也在网上找到了一些例子,但不明白什么是|"是吗?

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

再举一个例子:

- 主机:本地主机变量:乙:1:一个"2:两个"任务:- 调试:var=D- 调试:msg="D[1] 是 {{ D[1]|default ('undefined') }}"

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

任何帮助将不胜感激.

谢谢.

解决方案

使用竖线字符将值传递给过滤器.有许多 Jinja 2 过滤器,但 Ansible 带来一些额外的过滤器.

术语过滤器有时可能会令人困惑,因为所有过滤器的工作方式都非常不同.例如,一些减少散列/数组的结果集,一些修改字符串的内容,但还有一些过滤器只返回 true 或 false.

更好的解释可能是这些是修饰符,它们可以对您传递的数据做任何事情.您甚至可以编写自己的过滤器.

过滤器可以链接起来,将结果从第一个过滤器传递到下一个,依此类推.它的工作方式与 unix shell 上的管道命令完全一样.

"值" |过滤器1 |过滤器2 |过滤器N

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

changed 过滤器是一样的,但检查传递的结果是否有变化.它检查 result 中的 changed 属性.

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

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

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.

解决方案

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.

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

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