LoginSignup
5
5

More than 5 years have passed since last update.

Macでpyenv使ってPythonの環境設定をするansible playbook

Posted at

Pythonを始めました。
rbenvと同様にpyenvという便利なものがあることを発見。
使い方もほぼほぼおんなじだったのでplaybookにしてみました。

ansibleは2.0.2。入れるPythonは3.5.1。

これがそのplaybookだ

main.yml
---
- hosts: localhost # hostname
  become: no
  gather_facts: no
  vars:
      python_version: "3.5.1"

  tasks:
    - include: tasks/python.yml
tasks/python.yml
---
- name: Install basic python packages
  homebrew: name=pyenv

- name: Check installed Python version
  shell: "pyenv versions | grep {{python_version}} > /dev/null; echo $?"
  ignore_errors: true
  register: python_is_installed
  changed_when: python_is_installed.stdout != '0'

- name: Install Python
  shell: "pyenv install {{python_version}}"
  args:
    creates: ~/.pyenv/versions/{{python_version}}/bin/python

- name: Check Python version
  shell: "python --version | grep {{python_version}} > /dev/null; echo $?"
  register: is_correct_version
  changed_when: is_correct_version.stdout != '0'

- name: Switch Python version
  shell: "pyenv global {{python_version}} && pyenv rehash"
  when: is_correct_version.stdout != '0'

- name: pyenv init
  shell: 'eval "$(pyenv init -)"'
  changed_when: is_correct_version.stdout != '0'

とっても簡単。(あんまり綺麗じゃないシェルスクリプトですが)

ひとまずこれを叩くとpyenvを入れてpython 3.5.1を入れてそれをセットアップするものができます。

あとは手動で.bash_profileなどにeval $(pyenv init)を書いておくと完了です。

僕の場合はdotfilesを色んな所に持ち出しているので

if [ `which pyenv` ]; then
    eval "$(pyenv init - --no-rehash)" # adding --no-rehash makes this faster
fi

としています。

おまけ

このPythonの環境設定を含むMacをプロビジョニングするもの作ったので、興味のある方はどうぞ。

ikuwow/mac-provision: Provisioner of My Mac.
https://github.com/ikuwow/mac-provision

5
5
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
5
5