2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansible Tips: loopにおけるwhenにてregister変数を使用する場合、failと評価されるのは次の回

Last updated at Posted at 2020-09-30

備忘

loopにおけるwhenにてregister変数を使用する場合、register変数はそのloopの回の最後に設定/更新されるので、次の回の先頭でのwhenの評価時に意味をもつ

loop_when.yml
- hosts: all
  gather_facts: false
  tasks:
    - loop: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      shell: echo {{ item }}
      register: r
      when: r.stdout | default(0) | int < 5  # <= このr.stdoutは前ループの値

実行結果

% ansible-playbook -i localhost, -c local loop_when.yml      

PLAY [all] *****************************************************************************************************************************

TASK [shell] ***************************************************************************************************************************
changed: [localhost] => (item=0)
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
changed: [localhost] => (item=3)
changed: [localhost] => (item=4)
changed: [localhost] => (item=5)
skipping: [localhost] => (item=6) 
skipping: [localhost] => (item=7) 
skipping: [localhost] => (item=8) 
skipping: [localhost] => (item=9) 

PLAY RECAP *****************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


  • itemに0から9までを設定してループが実行される
  • itemをそのままstdoutとして返すコマンドを実行
  • whenにてコマンド実行結果が5未満の場合に実行する様に指定している様に見えるが、
    • rは各ループ終了時に確定するため、r.stdoutの値は前ループのもの。すなわち、itemが5の回では、r.stdoutは4であり、実行される
    • また、初回はrが未設定のため、default()により値を与えるなどの対応が必要

旧ドキュメント

loop_when.yml
---
- hosts: all
  gather_facts: false
  tasks:
  - vars:
      loop_list: [ 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 ]
      r:
        msg: "{{ loop_list|first * 2 }}" # r.msgの初期値を設定
    loop: "{{ loop_list }}"
    when: "(r.msg)|int < 5"              # このr.msgは初回を除いて前ループにより作成される
    debug:
      msg: "{{ 2 * item }}"
    register: r
  • itemに0から5まで、そこから0までを設定してループが実行される
  • この例では、r.msgの初期値に初回loopの結果を先取りさせている為、初回のwhenのみ、その回の(先取り)結果にて判断
  • 2倍した数が最初に5以上となるまで、つまり0, 2, 4, 6まで実行され、以降はskipされる
  • 言い換えると、when節がfalseであるべき最初の1回は、whenの評価がloopの最初なので前loop回の結果より実行され、かつそれ以降は全てskipされる

実行結果

$ ansible-playbook -i localhost, -c local loop_when.yml 

PLAY [all] **********************************************************************************************************

TASK [debug] ********************************************************************************************************
ok: [localhost] => (item=0) => {
    "msg": "0"
}
ok: [localhost] => (item=1) => {
    "msg": "2"
}
ok: [localhost] => (item=2) => {
    "msg": "4"
}
ok: [localhost] => (item=3) => {
    "msg": "6"
}
skipping: [localhost] => (item=4) 
skipping: [localhost] => (item=5) 
skipping: [localhost] => (item=6) 
skipping: [localhost] => (item=7) 
skipping: [localhost] => (item=8) 
skipping: [localhost] => (item=9) 
skipping: [localhost] => (item=10) 

PLAY RECAP **********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?