まず事象として
タスクがskipされたときでもregisterにはその旨が格納されてしまう。
- hosts: localhost tasks: - debug: msg: "TRUE" register: true_register when: true - debug: var: true_register - debug: msg: "FALSE" register: false_register when: false - debug: var: false_register
$ docker compose run ansible ansible-playbook playbook.yaml [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************* ok: [localhost] TASK [debug] ***************************************************************************************************************************************************************** ok: [localhost] => { "msg": "TRUE" } TASK [debug] ***************************************************************************************************************************************************************** ok: [localhost] => { "true_register": { "changed": false, "failed": false, "msg": "TRUE" } } TASK [debug] ***************************************************************************************************************************************************************** skipping: [localhost] TASK [debug] ***************************************************************************************************************************************************************** ok: [localhost] => { "false_register": { "changed": false, "skip_reason": "Conditional result was False", "skipped": true } } PLAY RECAP ******************************************************************************************************************************************************************* localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
対策として
公式ドキュメントにも書いてあるとおり、タグで回避しましょう。
If a task fails or is skipped, Ansible still registers a variable with a failure or skipped status, unless the task is skipped based on tags. See Tags for information on adding and using tags.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables
- hosts: localhost tasks: - block: - debug: msg: "TRUE" register: true_register - debug: var: true_register tags: - true_flag - block: - debug: msg: "FALSE" register: false_register - debug: var: false_register tags: - false_flag
$ docker compose run ansible ansible-playbook playbook.yaml --tags=true_flag [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************* ok: [localhost] TASK [debug] ***************************************************************************************************************************************************************** ok: [localhost] => { "msg": "TRUE" } TASK [debug] ***************************************************************************************************************************************************************** ok: [localhost] => { "true_register": { "changed": false, "failed": false, "msg": "TRUE" } } PLAY RECAP ******************************************************************************************************************************************************************* localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0