LoginSignup
13
6

More than 3 years have passed since last update.

Ansible blockディレクティブよりもinclude_tasksで処理をまとめよう!

Last updated at Posted at 2019-12-16

まえがき

実は1日目の「Ansibleタスクの再利用性を高めるためのタスクの粒度について」を書いてて出てきた副産物ですw
以下の内容も前述の内容を踏まえてのものになっています。

概要

一連の処理をまとめる方法としてblockを使われるかと思いますが、
include_tasksのほうが処理速度が速く可読性が高くなります。

処理速度

処理結果が出力されるまでのことを指します。
以下のコードを実行して体感できるかと思います。
ただし、block内処理が短い場合はさほど効果がない。

可読性

  • 処理結果
    下記の実行結果のように、block内のタスクがすべてskipとして出力されます。
    playbookが長く、対象ホストが多い場合は、万単位のログが出力されますので、できれば無駄なログを減らしたいところです。
    include_tasksを用いるとskipが1行のみ出力されるので、無駄なログを減らすことができます。
  • コード
    block内のタスクが多くなると、コードの可読性が下がります。
    include_tasksで外だしすることで、スッキリ管理することができます。

実行結果

block はblock内のすべてのタスクがSkipとして出力されるが、include_tasksは1行のみになっています。
これだけでもログが圧倒的にクリーンになると感じて頂けたのではないでしょうか。

blockの例

block.yml
---
- name: Ansible block sample
  hosts: localhost
  connection: local
  tasks:
    - block:
        - debug: msg="1"
        - debug: msg="2"
        - debug: msg="3"
        - debug: msg="4"
        - debug: msg="5"
        - debug: msg="6"
        - debug: msg="7"
        - debug: msg="8"
        - debug: msg="9"
        - debug: msg="10"
      when: no

ログに各タスクのskipが表示されます。

実行結果

PLAY [Ansible localhost sample] **************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
ok: [localhost]

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

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

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

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

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

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

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

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

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

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

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

include_tasksの例

debugモジュールはタスクファイルcount.ymlにまとめます。

include_tasks.yml
---
- name: Ansible include_tasks sample
  hosts: localhost
  connection: local
  tasks:
    - include_tasks: count.yml
      when: no
count.yml
---

- debug: msg="1"
- debug: msg="2"
- debug: msg="3"
- debug: msg="4"
- debug: msg="5"
- debug: msg="6"
- debug: msg="7"
- debug: msg="8"
- debug: msg="9"
- debug: msg="10"

実行結果には1つのskipのみが表示されます!

実行結果
PLAY [Ansible localhost sample] **************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
ok: [localhost]

TASK [include_tasks] *************************************************************************************************************************************************************************************
skipping: [localhost]

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

コミュニティ

これから始める方なら安心の日本語でコミュニケーションができるので、興味がある方は是非ご参加ください。

  • Slack・・・日本のユーザ会(800名以上)になります。
  • connpass・・・Ansible関連イベントの募集を行っています!
13
6
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
13
6