Ansible

【Ansible】存在しないモジュールを利用しようとした時のエラーが分かりにくい

はじめに

Ansible Playbookは、モジュールが存在しない場合のエラーメッセージが分かりにくいことに気付いたため、メモします。

見た目上は、syntax problem、構文エラーのようなエラーメッセージが出力されるため、注意していないとハマります。

環境

OS: CentOS 7.2.1511
Ansible(バージョンアップ前): 2.3.0.0
Ansible(バージョンアップ後): 2.4.2.0

$ ansible-playbook --version
ansible-playbook 2.3.0.0
  config file = /srv/pioneer-iotcloud-ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

利用しようとしたモジュール

ec2_vpc_endpoint
http://docs.ansible.com/ansible/latest/ec2_vpc_endpoint_module.html

New in version 2.4.

このec2_vpc_endpointモジュールはAnsible 2.4以降でのみ利用できますが、実行環境が2.3であるため、利用できませんでした。

実行しようとしたAnsible Playbook

aws-net_vpc-with-endpoints.yml
### ※途中省略
    # VPC Endpoint
    - name: Create new vpc endpoint the default policy
      ec2_vpc_endpoint:
        state: present
        region: ap-southeast-2
        vpc_id: vpc-12345678
        service: com.amazonaws.ap-southeast-2.s3
        route_table_ids:
          - rtb-12345678
          - rtb-87654321
      register: new_vpc_endpoint

実行時のエラー

Ansible Playbook 2.3で、上記Playbookを実行しようとしたところ、下記のようなエラーメッセージが出力されます。
パッと見はsyntax errorのような文面なので、何度もPlaybookを見直してしまいました。

$ ansible-playbook aws-net_vpc-with-endpoints.yml -C -vvv

Using /path/to/ansible.cfg as config file
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/path/to/aws-net_vpc-with-endpoints.yml': line 54, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    # VPC Endpoint
    - name: Create new vpc endpoint the default policy
      ^ here


The error appears to have been in '/path/to/aws-net_vpc-with-endpoints.yml': line 54, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    # VPC Endpoint
    - name: Create new vpc endpoint the default policy
      ^ here

Ansibleをバージョンアップする(2.3 -> 2.4)

$ sudo yum update ansible
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp.tsukuba.wide.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch 0:2.3.0.0-3.el7 will be updated
---> Package ansible.noarch 0:2.4.2.0-1.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                                   Arch                                     Version                                            Repository                              Size
====================================================================================================================================================================================
Updating:
 ansible                                   noarch                                   2.4.2.0-1.el7                                      epel                                   7.6 M

Transaction Summary
====================================================================================================================================================================================
Upgrade  1 Package

Total download size: 7.6 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
ansible-2.4.2.0-1.el7.noarch.rpm                                                                                                                             | 7.6 MB  00:00:11
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : ansible-2.4.2.0-1.el7.noarch                                                                                                                                     1/2
  Cleanup    : ansible-2.3.0.0-3.el7.noarch                                                                                                                                     2/2
  Verifying  : ansible-2.4.2.0-1.el7.noarch                                                                                                                                     1/2
  Verifying  : ansible-2.3.0.0-3.el7.noarch                                                                                                                                     2/2

Updated:
  ansible.noarch 0:2.4.2.0-1.el7

Complete!
$ ansible-playbook --version
ansible-playbook 2.4.2.0
  config file = /srv/pioneer-iotcloud-ansible/ansible.cfg
  configured module search path = [u'/home/vagrant/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

解決!

$ ansible-playbook aws-net_vpc-with-endpoints.yml -C -vvv
※途中省略

PLAY RECAP *************************************************************************************************************************************************************************
localhost                  : ok=9    changed=1    unreachable=0    failed=0

教訓

モジュールの対応バージョンはちゃんと確認しよう。