逆引きマニュアル: Ansible 2.7でyum + with_itemsを使うと出る警告の直し方

投稿日:

この記事はQiitaに書いたもののコピーです。

警告の内容

Ansible 2.7でyumモジュールを使っていたところ、以下の警告が出ました。

[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via 
squash_actions is deprecated. Instead of using a loop to supply multiple items 
and specifying `name: {{ item }}`, please use `name: ['git', 'gcc']` and remove
 the loop. This feature will be removed in version 2.11. Deprecation warnings 
can be disabled by setting deprecation_warnings=False in ansible.cfg.

要はループを使うなと言うわけですが、squash_actionsって何か知らなかったので調べてみました。

squash_actionsとは

ANSIBLE_SQUASH_ACTIONSに以下のように記載されています。

ざっくり言うと、with_によるループを使っているときに、リストで渡すように最適化する機能です。

Ansible can optimise actions that call modules that support list parameters when using with_ looping. Instead of calling the module once for each item, the module is called once with the full list.The default value for this setting is only for certain package managers, but it can be used for any module.Currently, this is only supported for modules that have a name or pkg parameter, and only when the item is the only thing being passed to the parameter.

具体的には、以下のように書かれているときに、 yumモジュールを2回実行するのではなく、['git', 'gcc']のように自動的にリストにして、1回で実行してくれます。便利ですね。

- yum: name={{ item }}
  with_items:
    - git
    - gcc

DEFAULT_SQUASH_ACTIONSによれば、yumの他に、apt、homebrewなどパッケージマネージャがデフォルトで定義されています。

修正方法

Ansible 2.7 Porting Guideyumに書かれているように、以下のようにします。

- yum:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
    - git
    - gcc

また、yumモジュールに関しては、以下のようにも書けます。こちらの方がシンプルですね。

- yum:
    name:
      - git
      - gcc

マニュアル