true / false 判定
when に変数を渡せば true / false の判定が出来る。
when: some_variable
when: not some_variable
Boolean以外が入った変数も判定条件として使える。
たとえば文字列の場合は、値が存在すれば true
と、空であれば false
として評価される様子。
is defined
変数が定義済みかどうかを判定できる。
when: some_variable is defined
when: some_variable is not defined
is succeeded / is failed / is changed / is skipped
変数が辞書 (ハッシュ) の場合は、以下の特殊文法が使える。
when: dictionary is succeeded
when: dictionary is failed
when: dictionary is changed
when: dictionary is skipped
それぞれ
dictionary.failed
dictionary.change
dictionary.skipped
の値の true / false
を判定する。
( is succeeded
は dictionary.failed
の値を見ており、 failed が false だったり、 そもそも failed キーない場合にもマッチする模様 )
in
- 文字列の中に文字列が含まれていること
- 配列の中に要素があること
などを検証できる。
when: "'somebody' in 'Everybody here is somebody'"
when: "'banana' in fruits"
複合条件
when に複数の条件を配列で渡すことが出来る。
この場合、AND条件でひとつずつ評価される。
when:
- some_not_defined_variable is defined
- some_not_defined_variable # 評価されない
この例の場合だと、一個目の判定でそれ以降の評価を諦めるので、二個目は評価されない。
and
や or
で条件をつなげて、ひとつの when に 複数条件を渡すことも出来る。
when: some_not_defined_variable is defined and some_not_defined_variable
Playbookの例
- hosts: localhost
tasks:
- set_fact:
alice:
name: Alice
job: not engineer
age: 98
evil_mind: # empty string
changed: true
skipped: true
failed: false
kicked: true
# present string evaluate as true
- debug:
msg: "Alice's name is {{ alice.name }}"
when:
- alice.name
# not present string evaluate as false
- debug:
msg: "Alice is not evil"
when:
- not alice.evil_mind
# You can use "is succeeded" conditional syntax
# when alice dictionary has "failed" key and false value then evaluated as succeeded
- debug:
msg: "Alice has succeeded as not enginner"
when:
- alice is succeeded
- debug:
msg: "Alice is not evil"
when:
- not alice.evil_mind
# You can use "is changed" conditional syntax
# because alice dictionary has "changed" key and boolean value
- debug:
msg: "Alice was changed"
when:
- alice is changed
# You can use "is failed" conditional syntax
# because alice dictionary has "failed" key and false value
- debug:
msg: "Alice was not failed to swimming"
when:
- alice is not failed
# You can use "is skipped" conditional syntax
# because alice dictionary has "skipped" key and true value
- debug:
msg: "Alice skipped school programs when she was a child."
when:
- alice is skipped
# You can not use some new original conditional syntax
# The conditional check 'alice is kicked' failed. The error was: template error while templating string: no test named 'kicked'
# - debug:
# msg: "Alice has kicked from chat room"
# when:
# - alice is kicked
- name: This task will be skipped because alice has no not_found key in he dictionary
debug:
msg: "Can you see unknown information of Alice?"
when:
- alice.not_found is defined
- set_fact:
bob:
failed: true
- debug:
msg: "{{ bob }}"
- name: 'This task will be skipped because bob has failed key value on his dictionary'
debug:
msg: "Bob is succeeded even he does not say anything."
when:
- bob is succeeded
- name: 'This task will be skipped because bob has not changed key on his dictionary'
debug:
msg: "Bob is succeeded even he does not say anything."
when:
- bob is change
- set_fact:
carol:
name: carol
- debug:
msg: Carol is succeeded because she has no failed in her dictionary
when:
- carol is succeeded
- carol is not failed
環境
- ansible 2.6.0
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。