はじめに
Ansibleを使ってPythonをインストールしたら3.5系が入ってしまった。
3.6系を入れたい。
$ python -V
Python 3.5.2
実行環境
- Ubuntu16.04 LTS
- Ansible2.5.3
参考情報
リポジトリを追加
公式ページの手順に従いリポジトリを追加する。
- name: Add Python3.6 repository from PPA
apt_repository:
repo: 'ppa:jonathonf/python-3.6'
apt-update
- name: Run the equivalent of "apt-get update" as a separate step
become: yes
apt:
update_cache: yes
仮想環境のインストール
python3-venv
ではなく、python3.6-venv
なので注意。
- name: install python36-virtualenv
become: yes
apt:
name: python3.6-venv
state: latest
apt-installする
明示的にpython3.6
、python3.6-dev
を指定する。
- name: Install the packages
become: yes
apt:
name:
- python-dev
- python-pip
- libxml2-dev
- libxslt1-dev
- zlib1g-dev
- libffi-dev
- libssl-dev
- python3.6
- python3.6-dev
state: present
仮想環境の作成
venv_name
は任意の名称を入れる。
- name: create virtualenv
shell: python3.6 -m venv venv_name
args:
chdir: /home/vagrant
最終的なplaybook
- hosts: all
tasks:
- name: Add Python3.6 repository from PPA
apt_repository:
repo: 'ppa:jonathonf/python-3.6'
- name: Update all packages to the latest version
become: yes
apt:
upgrade: dist
- name: Run the equivalent of "apt-get update" as a separate step
become: yes
apt:
update_cache: yes
- name: Install the packages
become: yes
apt:
name:
- python-dev
- python-pip
- libxml2-dev
- libxslt1-dev
- zlib1g-dev
- libffi-dev
- libssl-dev
- python3.6
- python3.6-dev
state: present
- name: install python36-virtualenv
become: yes
apt:
name: python3.6-venv
state: latest
- name: create virtualenv
shell: python3.6 -m venv venv_name
args:
chdir: /home/vagrant
実行結果確認
無事にvenv環境でPython3.6の環境ができました!
$ . venv_name/bin/activate
(venv_name) $ python -V
Python 3.5.2
(venv_name) $ python3.6 -V
Python 3.6.5