Ansible

Ansible playbook | register での変数定義はタスクのスキップ時にもおこなわれる

疑問 : タスクが skip される時、 register での変数定義はされるのか?

例:

tasks:
  - debug:
      msg: debug
    register: debugged_register
    when: false

答え : される

変数は未定義にならない。
スキップ理由などが記録される模様。

{
  "changed": false,
  "skip_reason": "Conditional result was False",
  "skipped": true
}

Playbookの例

register.yml
- hosts: localhost
  tasks:
    - name: add register with execute debug module
      debug:
        msg: add debug module result into debugged_register variable
      register: debugged_register

    - name: show register variable values set by execute debug module
      debug:
        msg: "{{ debugged_register }}"

    - name: skip execute task but register got variable
      debug:
        msg: add register of debug module result!
      register: not_executed_module_register
      when: false

    - name: show not_executed_module_register variable
      debug:
        msg: "{{ not_executed_module_register }}"

    - assert:
        that:
          - not_executed_module_register is defined
          - not_executed_module_register is not changed
          - not_executed_module_register is skipped

実行例

TASK [add register with execute debug module] ****************************************************************************************************
ok: [localhost] => {
    "msg": "add debug module result into debugged_register variable"
}

TASK [show register variable values set by execute debug module] *********************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false, 
        "failed": false, 
        "msg": "add debug module result into debugged_register variable"
    }
}

TASK [skip execute task but register got variable] ***********************************************************************************************
skipping: [localhost]

TASK [show not_executed_module_register variable] ************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false, 
        "skip_reason": "Conditional result was False", 
        "skipped": true
    }
}

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

PLAY RECAP ***************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0   

環境

  • ansible 2.6.0