1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ansible2.8.0 アップデート後 Amazon Linux のEC2インスタンスに yum module を含む playbook が通らないバグの一時的な解決策

1
Last updated at Posted at 2019-05-30

はじめに

Ansible2.8.0 にアップデートしたところ、yum module を含むプレイブックが通らなくなってしまいました。
それ以外の module は問題なくdryrunも適用もできました。

ansible user group の方々に質問させていただき、解決できましたが、同じように困っている人もいるのではないかと思い、記事にさせていただきました。

※2019/07/01 この記事の内容は、Ansible2.8.1で修正されたためアップデートすればバグは起こりません。

環境

MacBookAir
OS:Mojave 10.14.4
ローカル環境、ターミナルから
AWS EC2 amazon linuxのインスタンスに対して実行

ansible version 2.8.0

ansible-playbook 実行時のエラー

common role の main.yml で複数の task を include しています。その一つである base-packages.yml の yum module を実行するタイミングで、下記のエラーが出て実行出来ませんでした。

fatal: [xxx-xxxx-xxx01]: FAILED! => {"changed": false, "msg": "`python2-dnf` is not installed, but it is requiredfor the Ansible dnf module.", "results": []}
# ansible-playbook -i hosts xxxx-xxx.yml -l xxx-xxxx-xxx01 --check --diff

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

TASK [Gathering Facts] *******************************************************************************
ok: [xxx-xxxx-xxx01]

TASK [common : include tasks/base-packages.yml] ******************************************************
included: /Users/xxxx/git/xxxx_xxxx/xxxx_xxx/ansible/roles/common/tasks/base-packages.yml for xxx-xxxx-xxx01

TASK [common : yum install bacepackage] **************************************************************
fatal: [xxx-xxxx-xxx01]: FAILED! => {"changed": false, "msg": "`python2-dnf` is not installed, but it is requiredfor the Ansible dnf module.", "results": []}

PLAY RECAP *******************************************************************************************
xxx-xxxx-xxx01            : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
# cat xxxx-xxx.yml
- hosts: all
  become: True
  roles:
    - common
# cat roles/common/tasks/main.yml

- name: include tasks/base-packages.yml
  include_tasks: tasks/base-packages.yml

- name: include tasks/timezone.yml
  include_tasks: tasks/timezone.yml

- name: include tasks/hostname.yml
  include_tasks: tasks/hostname.yml
# cat roles/common/tasks/base-packages.yml

- name: yum install bacepackage
  yum:
    name: ['vim', 'zsh', 'git', 'tree', 'htop', 'mlocate', 'telnet', 'yum-plugin-changelog']
    state: present

エラーの概要

Amazon Linux に対して Ansible を実行した場合、パッケージマネージャーを dnf と認識しているようです。
yum モジュールはこの内容を元にパッケージマネージャーを dnf として扱っている状態です。
そのため、 dnf が依存している「python-dnf」が見つからないというエラーが起きています。

解決策

すでに Ansible の GitHub で issue に上がっていたようです。

こちらの中に解決できるコメントがありましたのでご紹介します。

このコメントのコードでは、 Ansible のファクト変数を set_fact で強制的に上書きをして、仮想マシンで利用できるパッケージマネージャーを dnf から yum に書き換えてる置き換えています。

具体的に私のコードでいうと、下記のように挿入しました。

エラー発生時のコード

# cat roles/common/tasks/base-packages.yml

- name: yum install bacepackage
  yum:
    name: ['vim', 'zsh', 'git', 'tree', 'htop', 'mlocate', 'telnet', 'yum-plugin-changelog']
    state: present

エラー解消したコード

cat roles/common/tasks/base-packages.yml
- set_fact:
    ansible_facts:
      pkg_mgr: yum


- name: yum install bacepackage
  yum:
    name: ['vim', 'zsh', 'git', 'tree', 'htop', 'mlocate', 'telnet', 'yum-plugin-changelog']
    state: present

yum module が無事に通りました

TASK [common : include tasks/base-packages.yml] *****************************************************
included: /Users/xxxx/git/xxxx_xxxx/xxxx_xxx/ansible/roles/common/tasks/base-packages.yml for xxx-xxxx-xxx01

TASK [common : set_fact] ****************************************************************************
ok: [xxx-xxxx-xxx01]

TASK [common : yum install bacepackage] *************************************************************
 [WARNING]: Platform linux on host xxx-xxxx-xxx01 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more
information.

ok: [xxx-xxxx-xxx01]

まとめ

質問に回答してくださった、 Ansible User Group の皆様ありがとうございました。
issue の最後の方で 2.8.1 で解消される予定というような内容が書いてあります。
急がないのであれば 2.8.1 のリリースを待ってもいいと思います。
待てない場合はpipでAnsibleバージョンを下げる対応策もあります。

常に最新のバージョンにすることで、バグを含んでいる可能性もありますが、バグも利用しているツールの理解につながります。
最新バージョンにアップデートをして新機能をいち早く使って、常に情報を仕入れるような姿勢でいたいと思いました。

Ansible2.8.0 で追加された新機能はこちらの記事にまとまっていますので、気になる方は読んでみてください。
https://tekunabe.hatenablog.jp/entry/2019/05/16/ansible28

こちらのイベントで Ansible2.8.0 の新機能の紹介やLTなどがあります!自分は東京からのため、リモート参加することにしました!
https://ansible-users.connpass.com/event/130456/

参考サイト

「Amazon Linux で ansible がdnfを推し始めて困ってます」

「Ansibleのファクト変数を確認する」

「Ansible 2.8.0 failing to install yum packages on Amazon Linux」
https://github.com/ansible/ansible/issues/56583

1
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?