vague memory

うろ覚えを無くしていこうともがき苦しむ人の備忘録

ansible pythonが無い場合の対処

Ubuntu 16.04.2 に対して ansible 実行しようとしたら python が見つからないよと怒られたのでメモ。

公式ドキュメントに記載があります。

python2 と python3 の共存問題のようです。



エラー内容と環境

  • エラー
fatal: [xxx.xxx.xxx.xxx]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Shared connection to xxx.xxx.xxx.xxx closed.\r\n",
    "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}
  • 環境
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.2 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.2 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
$ which python
$ which python3
/usr/bin/python3
$ ls -1 /usr/bin/python*
/usr/bin/python3
/usr/bin/python3.5
/usr/bin/python3.5m
/usr/bin/python3-jsondiff
/usr/bin/python3-jsonpatch
/usr/bin/python3-jsonpointer
/usr/bin/python3m

python2 で動かす

python2 をインストールします。 ansible で導入する場合は raw モジュールを使用します。

ansible コマンド例

ansible xxx.xxx.xxx.xxx --sudo -m raw -a "apt-get --yes install python python-simplejson" -i hosts
  • 導入後
$ which python
/usr/bin/python
$ python -V
Python 2.7.12

playbook 例

ansible-playbook で導入する場合は、 gather_facts: no の指定が必要です。

- hosts: xxxxxx
  become: yes
  gather_facts: no
  pre_tasks:
    - name: 'install python2'
      raw: apt-get --yes install python python-simplejson

python3 で動かす(Ansible 2.2以上)

※ 現時点ではプレビューとのことです。

インベントリで ansible_python_interpreter に python3 を指定します。

xxx.xxx.xxx.xxx ansible_python_interpreter=/usr/bin/python3

/usr/bin/python だけが無い場合

/usr/bin/python2 は有るが /usr/bin/python が無い場合などは、以下いずれかで対応できるかと思います。

Ansible 2.2以上

インベントリで ansible_python_interpreter に python2 を指定します。

xxx.xxx.xxx.xxx ansible_python_interpreter=/usr/bin/python2

Ansible 2.2未満

きちんと検証はしていませんが、シンボリックリンクで動作はするようです。

ln -s /usr/bin/python2 /usr/bin/python

参考URL