dehio3’s diary

仕事、生活、趣味のメモ

pyenv + virtualenv で 複数パターンのansible環境を作る

要件

  • 社員が共有で使うansible 環境を作る
  • pythonとansibleのバージョンアップに柔軟に対応できる環境を作る

参考

qiita.com

環境

$ cat /etc/redhat-release
CentOS release 6.9 (Final)
$ python --version
Python 2.6.9

pyenv install

# cd /usr/local/src/
# git clone https://github.com/yyuu/pyenv.git pyenv
Initialized empty Git repository in /usr/local/src/pyenv/.git/
remote: Counting objects: 15154, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 15154 (delta 0), reused 3 (delta 0), pack-reused 15147
Receiving objects: 100% (15154/15154), 2.70 MiB | 1.33 MiB/s, done.
Resolving deltas: 100% (10369/10369), done.

各ユーザーにて利用できるように権限を変更する (変えないと個別で環境作る時に作れない)

# ls -ld /usr/local/src/pyenv/
drwxr-xr-x. 10 root root 4096  7月  7 19:04 2017 /usr/local/src/pyenv/
# chmod 777 /usr/local/src/pyenv
# ls -ld /usr/local/src/pyenv/
drwxrwxrwx. 10 root root 4096  7月  7 19:04 2017 /usr/local/src/pyenv/

pyenv-virtualenv install

# cd /usr/local/src/pyenv/plugins/
# git clone https://github.com/yyuu/pyenv-virtualenv.git
Initialized empty Git repository in /usr/local/src/pyenv/plugins/pyenv-virtualenv/.git/
remote: Counting objects: 1892, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 1892 (delta 0), reused 1 (delta 0), pack-reused 1887
Receiving objects: 100% (1892/1892), 543.54 KiB | 693 KiB/s, done.
Resolving deltas: 100% (1294/1294), done.

ユーザー初期設定

$ echo 'export PYENV_ROOT=$HOME/.pyenv' >> ~/.bash_profile
$ echo 'export PYENV_ROOT=/usr/local/src/pyenv' >> ~/.bash_profile
$ echo 'export PATH=$PYENV_ROOT/bin:$PATH' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ tail -3  ~/.bash_profile
export PYENV_ROOT=/usr/local/src/pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
$ source ~/.bash_profile
$ which pyenv
/usr/local/pyenv/bin/pyenv

python install

$ pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
(省略)

上記でインストールできるバージョンを確認する

$ pyenv install 2.7.5
Downloading Python-2.7.5.tgz...
-> https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
Installing Python-2.7.5...
patching file ./Modules/readline.c
Hunk #1 succeeded at 204 (offset -2 lines).
Hunk #2 succeeded at 747 (offset -2 lines).
Hunk #3 succeeded at 857 (offset -2 lines).
Hunk #4 succeeded at 905 (offset -13 lines).
patching file ./Lib/site.py
patching file ./Lib/ssl.py
Hunk #2 succeeded at 430 (offset -5 lines).
patching file ./Modules/_ssl.c
Hunk #1 succeeded at 65 (offset -2 lines).
Hunk #2 succeeded at 304 (offset -4 lines).
Hunk #3 succeeded at 1729 (offset -83 lines).
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Installed Python-2.7.5 to /usr/local/src/pyenv/versions/2.7.5
$ pyenv versions
* system (set by /usr/local/src/pyenv/version)
  2.7.5

virtualenv 環境作成

$ pyenv virtualenv 2.7.5 py-2.7.5-ansible-2.3.1.0
Collecting virtualenv
/usr/local/src/pyenv/versions/2.7.5/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/src/pyenv/versions/2.7.5/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100%  | 1.8MB 565kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
New python executable in /usr/local/src/pyenv/versions/2.7.5/envs/py-2.7.5-ansible-2.3.1.0/bin/python2.7
Also creating executable in /usr/local/src/pyenv/versions/2.7.5/envs/py-2.7.5-ansible-2.3.1.0/bin/python
Installing setuptools, pip, wheel...done.
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Requirement already up-to-date: pip in /usr/local/src/pyenv/versions/2.7.5/envs/py-2.7.5-ansible-2.3.1.0/lib/python2.7/site-packages
$ pyenv versions
* system (set by /usr/local/src/pyenv/version)
  2.7.5
  2.7.5/envs/py-2.7.5-ansible-2.3.1.0
  py-2.7.5-ansible-2.3.1.0

ansible install

ローカル環境を選択

$ pyenv local py-2.7.5-ansible-2.3.1.0
$ pyenv versions
  system
  2.7.5
  2.7.5/envs/py-2.7.5-ansible-2.3.1.0
* py-2.7.5-ansible-2.3.1.0 (set by /home/tom_tanaka/.python-version)
$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (36.0.1)
wheel (0.29.0)
$ pip install ansible
$ ansible --version
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Jul  7 2017, 19:36:14) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]