タスクの実行順序

2019/05/06

Ansible

 タスクの実行順序

 Ansible Documentation の Playbook Keywords の記載です。タスクの実行順序が書かれています。
  • post_tasks
  • A list of tasks to execute after the tasks section.
  • pre_tasks
  • A list of tasks to execute before roles.
  • roles
  • A list of tasks to execute before roles.
  • tasks
  • Main list of tasks to execute in the play, they run after roles and before post_tasks.
 Handlers: Running Operations On Change に handlers の実行順序が書かれています。
  • handlers notified within [pre_tasks], [tasks], and [post_tasks] sections are automatically flushed in the end of section where they were notified;
  • handlers notified within [roles] section are automatically flushed in the end of [tasks] section, but before any [tasks] handlers.
上述の 2 つの説明を併せた説明が Using Roles に書かれています。
  • Any [pre_tasks] defined in the play.
  • Any handlers triggered so far will be run.
  • Each role listed in [roles] will execute in turn. Any role dependencies defined in the roles [meta/main.yml] will be run first, subject to tag filtering and conditionals.
  • Any [tasks] defined in the play.
  • Any handlers triggered so far will be run.
  • Any [post_tasks] defined in the play.
  • Any handlers triggered so far will be run.
ここまでをまとめると、タスクの実行順序は次のとおりです。
  1.  pre_tasks
  2.  pre_tasks の handlers
  3.  roles
  4.  tasks
  5.  roles の handlers
  6.  tasks の handlers
  7.  post_tasks
  8.  post_tasks の handlers

 確認

実行順序を確認するため、次の play を実行します。
■ site.yml
---
- hosts: all
  gather_facts: no

  handlers:
    - name: post_tasks の handler
      shell: "echo hello, world"
      listen: "post handler"
    - name: pre_tasks の handler
      shell: "echo hello, world"
      listen: "pre handler"
    - name: tasks の handler
      shell: "echo hello, world"
      listen: "tasks handler"
      
  post_tasks:
    - name: post_tasks のタスク No.1
      shell: "echo hello, world"
      notify: "post handler"
    - name: post_tasks のタスク No.2
      shell: "echo hello, world"

  pre_tasks:
    - name: pre_tasks のタスク No.1
      shell: "echo hello, world"
      notify: "pre handler"
    - name: pre_tasks のタスク No2.
      shell: "echo hello, world"

  tasks:
    - name: tasks のタスク No.1
      shell: "echo hello, world"
      notify: "tasks handler"
    - name: tasks のタスク No.2
      shell: "echo hello, world"

  roles:
    - roll-test
■ rolls/roll-test/tasks/main.yml
---
- name: post_tasks のタスク No.1
  shell: "echo hello, world"
  notify: "roll handler"
- name: post_tasks のタスク No.2
  shell: "echo hello, world"
■ rolls/roll-test/handlers/main.yml
---
- name: roll の handler
  shell: "echo hello, world"
  listen: "roll handler"
実行結果です。上述のタスクの実行順序のとおり実行されていることがわかります。
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml

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

TASK [pre_tasks のタスク No.1] *****************************************************
changed: [node-c0706]

TASK [pre_tasks のタスク No2.] *****************************************************
changed: [node-c0706]

RUNNING HANDLER [pre_tasks の handler] ******************************************
changed: [node-c0706]

TASK [roll-test : rolls のタスク No.1] *********************************************
changed: [node-c0706]

TASK [roll-test : rolls のタスク No.2] *********************************************
changed: [node-c0706]

TASK [tasks のタスク No.1] *********************************************************
changed: [node-c0706]

TASK [tasks のタスク No.2] *********************************************************
changed: [node-c0706]

RUNNING HANDLER [roll-test : roll の handler] ***********************************
changed: [node-c0706]

RUNNING HANDLER [tasks の handler] **********************************************
changed: [node-c0706]

TASK [post_tasks のタスク No.1] ****************************************************
changed: [node-c0706]

TASK [post_tasks のタスク No.2] ****************************************************
changed: [node-c0706]

RUNNING HANDLER [post_tasks の handler] *****************************************
changed: [node-c0706]

PLAY RECAP *********************************************************************
node-c0706                 : ok=12   changed=12   unreachable=0    failed=0   

[ansibleman@ansiblesv ansible]$ 

カテゴリー

目次

QooQ