目錄
- 說明
- 字元串過濾器
- 數字過濾器
- 清單過濾器
- 其它過濾器
ansible中的過濾器功能來自于jinja2模闆引擎,它是一種幫助我們處理資料的工具。有些過濾器是jinja2内置的,有些是ansible特有的,如果這些過濾器都不能滿足你的需求,jinja2也支援自定義過濾器。
- upper:過濾資料,将小寫字母變成大寫。
0 19:46:13 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test1.yaml --- - hosts: ck-node1 vars: var1: 121b3cd tasks: - debug: msg: "{{var1|upper}}" 0 19:46:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test1.yaml
- lower:将大寫字母變成小寫。
- capitalize:将字元串的首字母變為大寫,後面的字母變為小寫。
- reverse:将字元串反轉。
- first:傳回字元串的第一個字元。
- last:傳回字元串的最後一個字元。
- trim:去除字元串開頭和結尾的空格。
- center:字元串居中顯示。center(width=30)表示字元串居中顯示,兩側用空格補全30位。
- count和length:傳回字元串長度。
- list:将字元串轉換成清單,每個字元作為一個元素。
- shuffle:将字元串轉換成清單,每個字元作為一個元素,随機打亂順序。
- int:将對應的值轉換為int類型。
0 19:52:06 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test2.yaml --- - hosts: ck-node1 vars: var1: '50' tasks: - debug: msg: "{{2+(var1|int)}}" # 字元串和整形不能直接計算,是以需要對var1變量進行int轉換。 0 19:52:07 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test2.yaml # 如果無法轉換為int類型,預設傳回0。個性化指定傳回值,比如傳回6:{{ 'a' | int(6) }} 0 19:55:22 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test3.yaml --- - hosts: ck-node1 vars: var1: 'a' tasks: - debug: msg: "{{2+(var1|int)}}" # 預設傳回0,如果想個性化指定傳回值,比如傳回8,那可以寫成:msg: "{{2+(var1|int(8))}}" 0 19:55:23 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test3.yaml
- float:對應的值轉換為浮點型。如果無法轉換,預設傳回‘0.0’,個性化指定傳回值,比如傳回8.8:{{ 'a' | float(8.8) }}。
- abs:擷取對應值的絕對值。
- round:四舍五入。取小數點後五位{{ 3.1415926 | round(5) }}。
- random:取随機數。
- 從0到100中随機傳回一個随機數:{{ 100 | random }}。
- 從5到10中随機傳回一個随機數:{{ 10 | random(start=5) }}
- 從0到15中随機傳回一個随機數,這個随機數是5的倍數:{{ 15 | random(step=5) }}
- length、count、first、las、shuffle、upper、lower與上面字元串過濾器作用一緻。
- min:取清單中的最小值。
0 19:58:32 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test4.yaml --- - hosts: ck-node1 vars: var1: [1,'b',2,'a','e'] tasks: - debug: msg: "{{var1|min}}" 0 19:58:34 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test4.yaml
- max:取清單中的最大值。
- sort:排序
- 将清單升序排序輸出:{{ var | sort }}
- 将清單降序排序輸出:{{ var | sort(reverse=true) }}
- sum:求純數字清單中數字的和。
- flatten:拉平清單
- 将嵌套清單展開平鋪:{{ var | flatten }}
- 隻展開第一層的嵌套清單:{{ var | flatten(levels=1) }}
- 取嵌套清單中的最大值:{{ var | flatten | max }}
- join:
- 将清單中的元素合并成一個字元串:{{ var | join }}
- 将清單中的元素合并成一個字元串,每個元素之間用指定的字元隔開:{{ var | join('???? }}
- random:從清單中傳回一個元素,對清單使用random過濾器時,不能使用start和step參數。{{ var | random }}
- unique:去除重複的元素。{{var | unique}}
- union:合并清單,也就是求清單的并集。{{ var1 | union(var2) }}
- intersect:求清單的交集。{{ var1 | intersect(var2) }}
- difference:取出存在于var1清單中,但不存在于var2清單中的元素。{{ var1 | difference(var2) }}
- symmetric_difference:取出兩個清單中各自獨有的元素,重複的元素隻留下一個。{{var1 | symmetric_difference(var2)}}
- default:
- 如果變量沒有定義,臨時傳回一個指定好的預設值。
0 20:01:10 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test5.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{var1|default('wula')}}" 0 20:01:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test5.yaml
- 如果變量的值是一個空字元串或者變量沒有定義,臨時傳回一個指定的預設值。
0 20:04:14 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test6.yaml --- - hosts: ck-node1 vars: var1: 'natasha' tasks: - debug: msg: "{{var1|default('wula',boolean=true)}}" 0 20:04:16 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test6.yaml
- default過濾器還可能幫我們實作差異配置,比如要建立三個檔案,有些檔案要設定權限屬性,有些不要(此處牽扯到with_item循環可在第8節檢視)。
# 第一種方式:使用循環實作。 0 20:27:55 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test7.yaml --- - hosts: ck-node1 vars: paths: - path: /root/file1 mode: '0666' - path: /root/file2 - path: /root/file3 tasks: - file: path={{item.path}} state=touch mode={{item.mode}} with_items: "{{paths}}" when: item.mode is defined - file: path={{item.path}} state=touch with_items: "{{paths}}" when: item.mode is undefined 0 20:28:14 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test7.yaml # 第二種方式:使用過濾器實作。 ## {{item.mode | default(omit):}}:如果item有mode,就調用mode屬性的值,如果沒有就忽略mode參數。 0 20:30:03 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test8.yaml --- - hosts: ck-node1 vars: paths: - path: /root/file1 mode: '0666' - path: /root/file2 - path: /root/file3 tasks: - file: dest={{item.path}} state=touch mode={{item.mode | default(omit)}} with_items: "{{paths}}" 0 20:30:14 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test8.yaml
- 如果變量未定義,則報出“Mandatory variable not defined.”錯誤,而不是報出預設錯誤:{{var5 | mandatory}}
- json_query:取出bob的愛好。
0 14:00:02 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml --- users: - name: bob age: 18 hobby: - swimming - runnning - name: zoe age: 20 hobby: - music 0 14:00:09 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test9.yaml --- - hosts: ck-master tasks: - include_vars: file: /server/ops_ansible/variables.yaml name: var1 - debug: msg: "{{ var1 | json_query('users[?name==`bob`].hobby[*]') }}" # 單雙引号都存在了,就使用了反引号。 0 14:00:10 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test9.yaml
- quote:過濾器可以代替引号。
0 14:08:53 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test10.yaml --- - hosts: ck-node1 tasks: - shell: echo {{var1 | quote}} >/root/info vars: var1: "aa\nbb\ncc" 0 14:08:58 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test10.yaml # 帶引号的寫法與quote作用相同,不帶引号識别不了\n換行符,會報錯。 - shell: echo "{{var1}}" >/root/info
- ternary:實作三元運算的效果,類似if else的功能。
# 如果變量var1等于Selina,則對應值是Ms,否則是Mr。 0 14:17:07 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test11.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ (var1=='selina') | ternary('Ms','Mr') }}" vars: var1: "selina" 0 14:17:09 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test11.yaml
- basename:擷取路徑字元串的檔案名。
0 14:19:20 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test12.yaml --- - hosts: ck-node1 tasks: - vars: var1: "/server/ops_ansible/variables.yaml" debug: msg: "{{var1 | basename}}" 0 14:19:22 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test12.yaml
- dirname:擷取路徑字元串的目錄路徑。
0 14:20:29 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test13.yaml --- - hosts: ck-node1 tasks: - vars: var1: "/server/ops_ansible/variables.yaml" debug: msg: "{{var1 | dirname}}" 0 14:20:31 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test13.yaml
- realpath:檢視軟連結所指向的真正檔案路徑,等于pwd -P。
0 14:22:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # ln -s /server/ops_ansible/variables.yaml /root/variables.yaml 0 14:22:19 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test14.yaml --- - hosts: ck-node1 tasks: - vars: var1: "/root/variables.yaml" debug: msg: "{{var1 | realpath}}" 0 14:22:23 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test14.yaml
- relpath:擷取指定路徑的相對路徑。
0 14:35:12 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test15.yaml --- - hosts: ck-node1 tasks: - vars: var1: "/server/ops_ansible/variables.yaml" debug: msg: "{{var1 | relpath('/server/ops_tools/ops_scripts')}}" 0 14:35:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test15.yaml
- splitext:将檔案名與字尾名分開。
0 14:29:19 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test16.yaml --- - hosts: ck-node1 tasks: - vars: var1: "/server/ops_ansible/variables.yaml" debug: msg: "{{var1 | splitext}}" 0 14:29:20 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test16.yaml # 取檔案字尾名。 0 14:30:36 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test17.yaml --- - hosts: ck-node1 tasks: - vars: var1: "/server/ops_ansible/variables.yaml" debug: msg: "{{var1 | splitext | last}}" # 取檔案名用first即可。 0 14:30:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test17.yaml
- bool:判定bool值。
0 14:46:51 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test18.yaml --- - hosts: ck-node1 tasks: - vars: var1: "1" debug: msg: "{{var1 | bool}}" 0 14:46:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test18.yaml
- map:擷取資料中每個直接子元素所共有的屬性(不能擷取嵌套清單的屬性),并将值組成一個清單呈現。
0 14:50:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test19.yaml --- - hosts: ck-node1 vars: users: - name: bob age: 18 hobby: - swimming - runnning - name: zoe age: 20 hobby: - music tasks: - debug: msg: "{{users | map(attribute='name') | list}}" 0 14:50:27 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test19.yaml # 也可以組成一個字元串,用指定的字元隔開,比如分号。 msg: "{{users | map(attribute='name') | join(';')}}"
- to_datetime:計算時間。預設情況下,to_datatime轉換的字元串的格式必須是“%Y-%m-%d %H:%M:%S”,如果對應的字元串不是這種格式,則需要在to_datetime中指定與字元串相同的時間格式,才能正确的轉換時間類型。
# 計算時間差1。 0 14:58:58 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test20.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ ('2021-10-25 14:55:14' | to_datetime) - ('2021-10-24 15:55:14' | to_datetime) }}" 0 14:59:00 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test20.yaml # 計算時間差2。 0 15:02:08 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test21.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ ('20211025' | to_datetime('%Y%m%d')) - ('2021-10-24 15:55:14' | to_datetime) }}" 0 15:02:08 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test21.yaml # 計算兩個日期之間一共相差多少秒。 0 15:05:55 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test22.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ (('20211025' | to_datetime('%Y%m%d')) - ('20211024' | to_datetime('%Y%m%d'))).total_seconds() }}" 0 15:05:56 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test22.yaml # 計算兩個日期之間一共相差多少天。 0 15:08:30 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test23.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ (('2021-10-25 14:55:14' | to_datetime) - ('2021-10-20 15:55:14' | to_datetime)).days }}" 0 15:08:31 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test23.yaml
- 使用base64編碼方式對字元串進行編碼解碼。
# 編碼 0 15:12:18 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test24.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ 'hello' | b64encode }}" 0 15:12:20 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test24.yaml # 解碼 0 15:13:43 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test25.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{ 'aGVsbG8=' | b64decode }}" 0 15:13:45 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test25.yaml
- hash:對字元串進行哈希。
# 使用sha1算法對字元串進行哈希 0 15:14:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test26.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{'123456' | hash('sha1')}}" # 使用md5算法對字元串進行哈希。 0 15:15:19 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test27.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{'123456' | hash('md5')}}"
- password_hash:對字元串進行哈希,并随機加“鹽”,以便
# 使用sha256算法對字元串進行哈希。 0 15:24:40 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test28.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{'123456' | password_hash('sha256')}}" # 使用sha512算法對字元串進行哈希。 0 15:25:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test29.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{'123456' | password_hash('sha512')}}" # 可以使用指定字元串加“鹽” 0 15:37:47 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test30.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{'123456' | password_hash('sha512','wula')}}" 0 15:37:50 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test30.yaml
- dict2items:将字典資料轉換處理。
0 17:05:05 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test31.yaml --- - hosts: ck-node1 vars: users: bob: male zoe: female tasks: - debug: msg: "{{item.key}} is {{item.value}}" loop: "{{ users | dict2items }}"