3
2

More than 3 years have passed since last update.

【Ansible】checkオプション使用時に特定のタスクを実行しないようにする

Last updated at Posted at 2020-03-27

はじめに

<バージョン>
ansible 2.9.1

ansible-playbookコマンド実行時、--checkオプションを追加した場合はDryRunになり、
変更はせずにお試しで実行してくれるのでPlaybookの動作確認時に大変便利です。
しかし、コマンドの結果を元にした処理やディレクトリの作成など
 実際に特定のタスクを実行するからこそ、次のタスクを実行する意味がある場合
は注意が必要です。例としては以下の手順が挙げられます。

処理手順
タスク1:現在時刻を取得する
タスク2:名前が現在時刻のディレクトリを作成する
タスク3:タスク2で作成したディレクトリにファイルを作成する

通常通りに実行すれば上手くいくのですが、DryRunだとタスク1で現在時刻が取得出来ていないので
タスク2でディレクトリを作成することが出来ずにエラーになります。
このような場合はどうすればよいのでしょうか?

check_test1.yml
---
- name: check TEST
  hosts: localhost
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: 'task1 - get date'
      local_action: 'shell date +%Y_%m_%d_%H_%M_%S'
      register: date
      run_once: true
      changed_when: false

    - name: 'task2 - make direcrory'
      file: path=/home/ec2-user/ansible/result/{{ date.stdout }}
            state=directory
            mode=0755

    - name: 'task3 - make file'
      copy:
        content: "aaa"
        dest: "/home/ec2-user/ansible/result/{{ date.stdout }}/result.cfg"
出力1
[ec2-user@ip-<addr> ansible]$ ansible-playbook check_test1.yml --check    #checkモードで実行
[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 [check TEST] ***************************************************************************

TASK [task1 - get date] *********************************************************************
skipping: [localhost]

TASK [task2 - make direcrory] ***************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. 
~詳細なエラーメッセージは省略~

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

変更後Playbook

ansible_check_modeという変数が存在しており、この変数には
 通常時:false
 checkオプション使用時 :true
が格納されているのでこれを使って条件分岐が出来そうです。

check_test2.yml
---
- name: check TEST
  hosts: localhost
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: 'show ansible_check_mode'
      debug:
        var: ansible_check_mode    #変数ansible_check_modeの中身を確認する

    - name: 'task1 - get date'
      local_action: 'shell date +%Y_%m_%d_%H_%M_%S'
      register: date
      run_once: true
      changed_when: false

    - name: 'task2 - make direcrory'
      file: path=/home/ec2-user/ansible/result/{{ date.stdout }}
            state=directory
            mode=0755
      when: not ansible_check_mode     #ポイント:通常時のみタスクを実行する

    - name: 'task3 - make file'
      copy:
        content: "aaa"
        dest: "/home/ec2-user/ansible/result/{{ date.stdout }}/result.cfg"
      when: not ansible_check_mode     #ポイント:通常時のみタスクを実行する

実行結果(通常時)

通常時はansible_check_modeにfalseが格納されているので、全てのタスクが実行されます。

出力2
[ec2-user@ip-<addr> ansible]$ ansible-playbook check_test2.yml
[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 [check TEST] ***************************************************************************

TASK [show ansible_check_mode] **************************************************************
ok: [localhost] => {
    "ansible_check_mode": false    #通常モードで実行している
}

TASK [task1 - get date] *********************************************************************
ok: [localhost -> localhost]

TASK [task2 - make direcrory] ***************************************************************
changed: [localhost]

TASK [task3 - make file] ********************************************************************
changed: [localhost]

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

実行結果(checkオプション使用時)

checkオプション使用時は、タスク1が実行されていなくても(skippedになった)、
ansible_check_modeにtrueが格納されているため、タスク2とタスク3はwhenの条件によって
skipされてエラーを回避することが出来ます。

出力3
[ec2-user@ip-<addr> ansible]$ ansible-playbook check_test2.yml --check
[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 [check TEST] ***************************************************************************

TASK [show ansible_check_mode] **************************************************************
ok: [localhost] => {
    "ansible_check_mode": true    #checkモードで実行している
}

TASK [task1 - get date] *********************************************************************
skipping: [localhost]

TASK [task2 - make direcrory] ***************************************************************
skipping: [localhost]

TASK [task3 - make file] ********************************************************************
skipping: [localhost]

PLAY RECAP **********************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
3
2
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
3
2