Ansible playbook for setting up Python preferences using pyenv on Mac

I started Python. I found that there is a convenient thing called pyenv as well as rbenv. The usage was almost the same, so I made it a playbook.

ansible is 2.0.2. Python to put is 3.5.1.

This is the 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'

Very easy. (Although it is a shell script that is not very beautiful)

If you hit this for the time being, you can put pyenv and put python 3.5.1 and set it up.

After that, manually write ʻeval $ (pyenv init)in.bash_profile` etc. and you're done.

In my case, I take dotfiles to various places, so

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

It is said.

bonus

I've made a Mac provisioner that includes this Python preference, so if you're interested, please.

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

Recommended Posts

Ansible playbook for setting up Python preferences using pyenv on Mac
Notes on setting pyenv and python environment using Homebrew on Mac OS Marvericks
Set Up for Mac (Python)
Build a Python environment on your Mac using pyenv
Minimum memo when using Python on Mac (pyenv edition)
Introducing Python using pyenv on Ubuntu 20.04
Notes on installing Python using PyEnv
Install Python on CentOS using Pyenv
Install Python on CentOS using pyenv
(Beginner) Notes on using pyenv on Mac
Notes for using OpenCV on Windows10 Python 3.8.3.
Put Anaconda on your Mac using Pyenv
Setting up Basic authentication using Python @Lambda
Notes on building Python and pyenv on Mac
Notes for setting up pydicom on macOS
Video processing using Python + OpenCV on Mac
Setting up Digest authentication using Python @Lambda
[Definitive Edition] Building an environment for learning "machine learning" using Python on Mac
A memo when setting up a Docker container for using JUMAN ++, KNP, python
python on mac
Install Python 3.8.6 on macOS Big Sur using pyenv
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
How to build an environment for using multiple versions of Python on Mac
Install pyenv on mac
I tried Python on Mac for the first time.
How to set up a Python environment using pyenv
Pyenv + virtualenv on Mac
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
Install Ansible on Mac
[Note] The story of setting up the SDK for Python of Azure IoT Hub on Linux
Preferences to generate animated GIFs from Python on Mac
Install Python on Mac
Install Python 3 on Mac
Build a Python development environment using pyenv on MacOS
Install Python 3.4 on Mac
Install Python environment on local PC (pyenv, venv on Mac)
Minimum notes when using Python on Mac (Homebrew edition)
Build an environment for machine learning using Python on MacOSX
Using multiple versions of Python on Mac OS X (2) Usage
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
Building a Python environment on a Mac and using Jupyter lab
Temporary fix for osx pyenv python not using arrow keys
Access google spreadsheet using python on raspberry pi (for myself)
Build a Python extension for E-Cell 4 on Mac OSX (Yosemite)
Initial setting of Mac ~ Python (pyenv) installation is the fastest
Create a virtual environment for python on mac [Very easy]
Easy on Mac! Plot of unit step response using Python
Until building a Python development environment using pyenv on Ubuntu 20.04
Set up Python 3.4 on Ubuntu
Setting up grub on CentOS 8
Install pygame on python3.4 on mac
Python environment construction For Mac
Change python version using pyenv
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Python 3 series installation for mac
Pyenv on Mac OSX Mavericks
Catalina on Mac and pyenv