Skip to content

Instantly share code, notes, and snippets.

@yubessy
Last active Sep 12, 2017
Embed
What would you like to do?
Ansible pyenv role
---
pyenv_root: /opt/pyenv
pyenv_rcfiles: []
pyenv_versions: []
pyenv_global_version: system
pyenv_build_requirements:
# https://github.com/pyenv/pyenv/wiki/Common-build-problems
- zlib-devel
- bzip2
- bzip2-devel
- readline-devel
- sqlite
- sqlite-devel
- openssl-devel
---
- include: pyenv.yml
- include: versions.yml
---
- name: pyenv requirements are installed
yum:
name: '{{ item }}'
state: present
with_items: '{{ pyenv_build_requirements }}'
- name: pyenv repo exists
git:
repo: https://github.com/pyenv/pyenv.git
dest: '{{ pyenv_root }}'
- name: pyenv init commands are configured
blockinfile:
dest: '{{ item }}'
create: yes
content: |
{{ lookup('template', 'templates/pyenv_init.j2') }}
with_items: '{{ pyenv_rcfiles }}'
---
- name: pyenv temporal init script file is created
tempfile:
prefix: ansible.pyenv.init.
register: tmp_pyenv_init
changed_when: false
- name: pyenv temporal init commands are configured
blockinfile:
dest: '{{ tmp_pyenv_init.path }}'
content: |
{{ lookup('template', 'templates/pyenv_init.j2') }}
changed_when: false
- name: installed python versions are checked
shell: 'source {{ tmp_pyenv_init.path }} && pyenv versions --bare'
changed_when: false
register: tmp_pyenv_versions
- name: missing python versions are installed
shell: 'source {{ tmp_pyenv_init.path }} && pyenv install {{ item }}'
when: item not in tmp_pyenv_versions.stdout_lines
with_items: '{{ pyenv_versions }}'
- name: global python version is checked
shell: 'source {{ tmp_pyenv_init.path }} && pyenv global'
changed_when: false
register: tmp_pyenv_global_version
- name: global python version is set
shell: 'source {{ tmp_pyenv_init.path }} && pyenv global {{ pyenv_global_version }}'
when: pyenv_global_version != tmp_pyenv_global_version.stdout
- name: unused python versions are uninstalled
shell: 'source {{ tmp_pyenv_init.path }} && pyenv uninstall -f {{ item }}'
when: item not in pyenv_versions
with_items: '{{ tmp_pyenv_versions.stdout_lines }}'
# https://github.com/pyenv/pyenv#installation
export PYENV_ROOT="{{ pyenv_root }}"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment