过滤器其实是ansible中的一种插件,除了过滤器,ansible中还有tests、Inventory、Connection、循环插件,循环插件也叫做"lookup插件"
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "index is {{item.0}} , value is {{item.1}}"
with_indexed_items: ['a','b','c']
使用lookup插件,也可以做到与上述示例完全相同的效果,使用"loop关键字"配合"lookup插件"处理列表
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "index is {{item.0}} , value is {{item.1}}"
loop: "{{ lookup('indexed_items',['a','b','c']) }}"
---
- hosts: test70
remote_user: root
gather_facts: no
vars:
users:
alice: female
bob: male
tasks:
- debug:
msg: "{{item.key}} is {{item.value}}"
with_dict: "{{ users }}"
---
- hosts: test70
remote_user: root
gather_facts: no
vars:
users:
alice: female
bob: male
tasks:
- debug:
msg: "{{item.key}} is {{item.value}}"
loop: "{{ lookup('dict',users) }}"
1. 语法
lookup插件语法
lookup(‘插件名’,被处理数据或参数)
2.5版本开始,官方开始推荐使用"loop"关键字代替"with_xxx"风格的关键字,在推荐使用"loop"关键字的同时,官方认 为,loop关键字结合lookup插件的使用方法不够简洁明了,所以官方同时推荐,在使用loop关键字进行循环操作时,最好配合过滤器来处理数据,官 方认为这样做会使语法变得更加简洁明了,如果想要详细的描述官方推荐的使用方法。
lookup插件有很多,有 的lookup插件与"循环操作"完全没有关系,lookup类型的插件的主要作用是访问外部的数据源,比如,获取到外部数据并赋值给某个变量,以便之后 使用这些数据,lookup插件的操作都是在ansible主机中进行的,与目标主机没有关系。
ansible-doc -t lookup -l
“-t"选项用于指定插件类型,”-l"选项表示列出列表
ansible-doc -t lookup dict
2. 语法
2.1 file插件
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{ lookup('file','/testdir/testfile') }}"
获取多个文件中的内容
tasks:
- debug:
msg: "{{ lookup('file','/testdir/testfile','/testdir/testfile1') }}"
执行上例playbook以后,debug模块的输出信息如下:
TASK [debug] *******************************
ok: [test70] => {
"msg": "testfile in test71,testfile1 in test71"
}
使用"wantlist"参数,表示我们想要获取到的值是一个 列表,而非字符串
tasks:
- debug:
msg: "{{ lookup('file','/testdir/testfile','/testdir/testfile1',wantlist=true) }}"
2.2 query函数
2.5版本的ansible中,引入了一个新的jinja2函数,这个函数叫做"query",通过query函数 也可以调用lookup插件,但是通过query函数调用lookup插件时,query函数的默认行为是返回一个列表,如下两种写法是等价的
- debug:
msg: "{{ lookup('file','/testdir/testfile',wantlist=true) }}"
- debug:
msg: "{{ query('file','/testdir/testfile') }}"
query函数有一个简写的格式q
- debug:
msg: "{{ q('file','/testdir/testfile') }}"
2.6版本的ansible中,可以使用errors关键字控制lookup插件出错时的处理机制,如果我想要在lookup插件执行出错时忽略错误,则可以将errors的值设置为ignore。errors的值需要使用引号引起,errors的值可以设置为ignore、warn或者strict,缺省值为strict。
- debug:
msg: "{{ lookup('file','/testdir/testfil',errors='ignore') }}"
3. lookup插件
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
#file插件可以获取ansible主机中指定文件的内容
- debug:
msg: "{{ lookup('file','/testdir/testfile') }}"
#env插件可以获取ansible主机中指定变量的值
- debug:
msg: "{{ lookup('env','PATH') }}"
#first_found插件可以获取列表中第一个找到的文件
#按照列表顺序在ansible主机中查找
- debug:
msg: "{{ lookup('first_found',looklist) }}"
vars:
looklist:
- /testdir
- /tmp/staging
#当使用with_first_found时,可以在列表的最后添加- skip: true
#表示如果列表中的所有文件都没有找到,则跳过当前任务,不会报错
#当不确定有文件能够被匹配到时,推荐这种方式
- debug:
msg: "{{item}}"
with_first_found:
- /testdir1
- /tmp/staging
- skip: true
#ini插件可以在ansible主机中的ini文件中查找对应key的值
#如下示例表示从test.ini文件中的testA段落中查找testa1对应的值
#测试文件/testdir/test.ini的内容如下(不包含注释符#号)
#[testA]
#testa1=Andy
#testa2=Armand
#
#[testB]
#testb1=Ben
- debug:
msg: "{{ lookup('ini','testa1 section=testA file=/testdir/test.ini') }}"
#当未找到对应key时,默认返回空字符串,如果想要指定返回值,可以使用default选项,如下
#msg: "{{ lookup('ini','test666 section=testA file=/testdir/test.ini default=notfound') }}"
#可以使用正则表达式匹配对应的键名,需要设置re=true,表示开启正则支持,如下
#msg: "{{ lookup('ini','testa[12] section=testA file=/testdir/test.ini re=true') }}"
#ini插件除了可以从ini类型的文件中查找对应key,也可以从properties类型的文件中查找key
#默认在操作的文件类型为ini,可以使用type指定properties类型,如下例所示
#如下示例中,application.properties文件内容如下(不包含注释符#号)
#http.port=8080
#redis.no=0
#imageCode = 1,2,3
- debug:
msg: "{{ lookup('ini','http.port type=properties file=/testdir/application.properties') }}"
#dig插件可以获取指定域名的IP地址
#此插件依赖dnspython库,可使用pip安装pip install dnspython
#如果域名使用了CDN,可能返回多个地址
- debug:
msg: "{{ lookup('dig','www.baidu.com',wantlist=true) }}"
#password插件可以生成随机的密码并保存在指定文件中
- debug:
msg: "{{ lookup('password','/tmp/testpasswdfile') }}"
#以上插件还有一些参数我们没有涉及到,而且也还有很多插件没有总结,等到用到对应的插件时,再行介绍吧
#你也可以访问官网的lookup插件列表页面,查看各个插件的用法
#https://docs.ansible.com/ansible/latest/plugins/lookup.html
4. 循环替代方案
在2.5版本之前的ansible中,大多数人习惯使用"with_X"风格的关键字操作循环,从2.6版本开始,官方开始推荐使用"loop"关键字代替"with_X"风格的关键字。
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
loop:
- teststr1
- teststr2
使用"loop"关键字配合对应的"lookup"插件,替换更多的、具有更复杂功能的"with_X"风格的关键字,使用loop关键字和dict插件替换"with_dict"关键字。
---
- hosts: test70
remote_user: root
gather_facts: no
vars:
users:
alice: female
bob: male
tasks:
- debug:
msg: "{{item.key}} is {{item.value}}"
loop: "{{ lookup('dict',users) }}"
在2.6版本的官网手册中,官方开始推荐使用"loop加filter"的方式来替代"loop加lookup"的方式
---
- hosts: test70
remote_user: root
gather_facts: no
vars:
users:
alice: female
bob: male
tasks:
- debug:
msg: "{{item.key}} is {{item.value}}"
loop: "{{ users | dict2items }}"
(未完节)
————Blueicex 2020/3/30 09:40 [email protected]