みなさんこんにちは。ヒロウミです。
ansibleのplaybookでフィルターを使ってみたのでメモっておきます。
今回はリストに対して操作を行ってみました。変数の値を変換したいときに|(パイプ)を使って操作していくようです。
# cat filter.yml
- hosts: target01
vars:
rhcp_song:
- 1.Snow
- 2.Under the bridge
- 3.Soul to squeeze
- 4.Dosed
tasks:
- debug: msg="リストの先頭 :{{ rhcp_song|first }}"
- debug: msg="ランダムな要素 :{{ rhcp_song|random }}"
- debug: msg="ランダムな要素の小文字 :{{ rhcp_song|random|lower }}"
- debug: msg="リストを,区切りで結合 :{{ rhcp_song|join(',') }}"
実行してみます
# ansible-playbook filter.yml
PLAY [target01] ****************************************************************
TASK [setup] *******************************************************************
ok: [target01]
TASK [debug] *******************************************************************
ok: [target01] => {
"msg": "リストの先頭 :1.Snow"
}
TASK [debug] *******************************************************************
ok: [target01] => {
"msg": "ランダムな要素 :3.Soul to squeese"
}
TASK [debug] *******************************************************************
ok: [target01] => {
"msg": "ランダムな要素の小文字 :3.soul to squeese"
}
TASK [debug] *******************************************************************
ok: [target01] => {
"msg": "リストを,区切りで結合 :1.Snow,2.Under the bridge,3.Soul to squeese,4.Dosed"
}
PLAY RECAP *********************************************************************
target01 : ok=5 changed=0 unreachable=0 failed=0
フィルタの種類はたくさんあるようです。公式リファレンスで確認して便利そうなやつを覚えておくといいかもしれませんね。
公式リファレンス: