說明
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 }}"
寫作不易,轉載請注明出處,謝謝~~