過濾器其實是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]