ansible playbook 常用filter selectattr

栏目: 服务器 · 发布时间: 6年前

内容简介:随着ansilbe playbook 用的越来越多,我们也开始接触许多高级filter例如selectattr举个例子:

随着ansilbe playbook 用的越来越多,我们也开始接触许多高级filter

例如selectattr

举个例子:

---
users:
  - name: john
    email: john@example.com
  - name: jane
    email: jane@example.com
  - name: fred
    email: fred@example.com
    password: 123!abc

如果我们执行如下的task

- set_fact:
  emails: "{{ users | selectattr('password', 'undefined') | map(attribute='email') | list }}"

首先,我们定义的users会被传递给selectattr

然后会看下我们所有的user重,没有定义password的用户(这里第三个就不满足条件)

然后我们通过map方法将符合调价的用户的email返回给我们

第二个例子:(常用的undefined,equalto,match,search)

- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    network:
      addresses:
        private_ext:
          - type: fixed
            addr: 172.16.2.100
        private_man:
          - type: fixed
            addr: 172.16.1.100
          - type: floating
            addr: 10.90.80.10

然后我们执行如下task

- debug: msg={{ network.addresses.private_man | selectattr("type", "equalto", "floating") | map(attribute='addr') | list }}
- debug: msg={{ network.addresses.private_man | selectattr("type", "match", "^floating$") | map(attribute='addr') | list }}
- debug: msg={{ network.addresses.private_man | selectattr("type", "search", "^floating$") | map(attribute='addr') | list }}

输出结果:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        "10.90.80.10"
    ]
}
 
TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        "10.90.80.10"
    ]
}
 
TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        "10.90.80.10"
    ]
}

本文参考文章:http://www.oznetnerd.com/jinja2-selectattr-filter/


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Heuristic Search

Heuristic Search

Stefan Edelkamp、Stefan Schrodl / Morgan Kaufmann / 2011-7-15 / USD 89.95

Search has been vital to artificial intelligence from the very beginning as a core technique in problem solving. The authors present a thorough overview of heuristic search with a balance of discussio......一起来看看 《Heuristic Search》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码