てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] スキップされたタスクの結果は is skipped でもあり is succeeded でもある

はじめに

Ansible では、タスクの実行結果を変数に保存し、その結果を後続のタスクで確認する機能があります。

docs.ansible.com

そういえばどうなんだろうシリーズといいますか、「スキップされたタスクって succeeded 扱いだっけ?」と思ったことがありました。

結果としては、is skipped でもあり is succeeded でもありました。

試した結果をまとめます。

  • 検証環境
    • ansible 5.4.0
    • ansible-core 2.12.3

Playbook

こんな Playbook で試します。

---
- hosts: localhost
  gather_facts: false

  tasks:
    # (1) 必ずスキップされるタスク
    - name: skipped task
      ansible.builtin.debug:
        msg: this task is skipped
      register: resister_skipped
      when:
        - false

    # (2) 1が skipped 扱いの場合に実行されるタスク
    - name: debug result skipped
      ansible.builtin.debug:
        msg: resister_skipped is skipped
      when:
        - resister_skipped is skipped

    # (3) 1が succeeded 扱いの場合に実行されるタスク
    - name: debug result succeeded
      ansible.builtin.debug:
        msg: resister_skipped is succeeded
      when:
        - resister_skipped is succeeded

一番確かめたいたいのは (3) のタスクです。

結果

実行結果は以下のとおりです。 is succeeded を条件にしたタスク3も実行されました。

$ ansible-playbook -i localhost, success.yml     

PLAY [localhost] ***************************************************************************************

TASK [skipped task] ************************************************************************************
skipping: [localhost]

TASK [debug result skipped] ****************************************************************************
ok: [localhost] => {
    "msg": "resister_skipped is skipped"
}

TASK [debug result succeeded] **************************************************************************
ok: [localhost] => {
    "msg": "resister_skipped is succeeded"
}

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

なお resister_skipped 変数をデバッグ表示すると以下のとおりです。

    "resister_skipped": {
        "changed": false,
        "failed": false,
        "msg": "this task is skipped"
    }

私の解釈

コードでいうとこのあたりでしょうか。

github.com

failed でない場合は succeededまたは successsuccessful )のようです。

今回の場合は「正常に判断処理がされた結果、スキップできた」ことをもって succeeded なのかなと捉えています。

一方たとえば、(1) のタスクで、when に未定義の変数をした場合(確認のためにignore_errors: true も併用)はさすがに succeeded にはなりませんでした。

---
- hosts: localhost
  gather_facts: false

  tasks:
    # (1) when が異常
    - name: skipped task
      ansible.builtin.debug:
        msg: this task is skipped
      register: resister_skipped
      when:
        - hogehoge_undefined_variable
      ignore_errors: true
        
    # (2) 1が skipped 扱いの場合に実行されるタスク
    - name: debug result skipped
      ansible.builtin.debug:
        msg: resister_skipped is skipped
      when:
        - resister_skipped is skipped

    #  (3) 1がsucceeded 扱いの場合に実行されるタスク
    - name: debug result succeeded
      ansible.builtin.debug:
        msg: resister_skipped is succeeded
      when:
        - resister_skipped is succeeded

実行結果

PLAY [localhost] ***************************************************************************************************

TASK [skipped task] ************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'hogehoge_undefined_variable' failed. The error was: error while evaluating conditional (hogehoge_undefined_variable): 'hogehoge_undefined_variable' is undefined\n\nThe error appears to be in '/Users/akira/Documents/git/general/vagrant/nwlab/qa/success.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    # (1) when が異常\n    - name: skipped task\n      ^ here\n"}
...ignoring

TASK [debug result skipped] ****************************************************************************************
skipping: [localhost]

TASK [debug result succeeded] **************************************************************************************
skipping: [localhost]

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