[CentOS8]Ansibleでエラー[/bin/sh: /usr/bin/python: No such file or directory]が出る時

カテゴリ: Ansible, Mac, python | タグ: ,

発生したエラー

手元のmacOSからCentOS8.0をセットアップしようとansibleコマンドを実行しようとしたところ、下記のエラーが発生した。

$ ansible server-centos8 -m ping
192.168.33.10 | FAILED! => {
    "changed": false,
    "module_stderr": "Connection to 192.168.33.90 closed.\r\n",
    "module_stdout": "/bin/sh: /usr/bin/python: No such file or directory\r\n",
    "msg": "MODULE FAILURE",
    "rc": 127
}

原因1

セットアップ先のCentOS8にpythonが入っていなかった。

対応

CentOS8にpythonをインストール

sudo dnf -y install python36

python3コマンドが追加されたことを確認

[root@centos8 vagrant]# which python
/usr/bin/which: no python in (/sbin:/bin:/usr/sbin:/usr/bin)

[root@centos8 vagrant]# which python3
/bin/python3

原因2

macOS上のAnsibleが古すぎて、セットアップ先OSに(pythonではなく)python3のみがインストールされている場合に対応できていなかった。

対応

エラーが出た時のバージョンは2.5.1だったのでansibleコマンドをアップグレードする

$ ansible --version
ansible 2.5.1

brewコマンドでアップグレードしようとしたら、依存パッケージのpython3.8のインストールでエラーがでる

$ brew upgrade ansible

...
==> Upgrading 1 outdated package:
ansible 2.5.1 -> 2.9.6_1

==> Upgrading ansible 2.5.1 -> 2.9.6_1
Error: The following formula
  [#<Dependency: "python@3.8" []>, #<Options: []>]
cannot be installed as binary package and must be built from source.
Install the Command Line Tools:
  xcode-select --install

エラーメッセージに従い、python3.8のビルドに必要なツールをインストールする

xcode-select --install

再度upgradeすると成功

$ brew upgrade ansible
...
If you need to have python@3.8 first in your PATH run:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.bash_profile
...

インストール中にbash_profileにパス設定を追加するよう指示されるので実行

$ python3 --version
Python 3.7.4

$ echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.bash_profile
$ source  ~/.bash_profile

$ python3 --version
Python 3.8.2

バージョンアップしていることを確認

$ ansible --version
ansible 2.9.6
...
  python version = 3.8.2 (default, Mar 11 2020, 00:29:50) [Clang 11.0.0 (clang-1100.0.33.17)]

動作確認

対応後に再実行し、エラーが出ていないことを確認

$ ansible server-centos8 -m ping
192.168.33.90 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です