From 1585114795b3bd3fe83b66896ce1c749cebc42c2 Mon Sep 17 00:00:00 2001 From: Phil Skentelbery Date: Thu, 26 Sep 2019 17:58:30 -0500 Subject: [PATCH] added pyenv role --- roles/avanov.pyenv/.gitignore | 1 + roles/avanov.pyenv/.travis.yml | 16 +++ roles/avanov.pyenv/LICENSE | 21 +++ roles/avanov.pyenv/README.md | 70 ++++++++++ roles/avanov.pyenv/defaults/main.yml | 55 ++++++++ roles/avanov.pyenv/handlers/main.yml | 2 + roles/avanov.pyenv/meta/.galaxy_install_info | 2 + roles/avanov.pyenv/meta/main.yml | 129 ++++++++++++++++++ roles/avanov.pyenv/tasks/Darwin.yml | 5 + roles/avanov.pyenv/tasks/Debian.yml | 12 ++ roles/avanov.pyenv/tasks/RedHat.yml | 8 ++ roles/avanov.pyenv/tasks/install.yml | 59 ++++++++ roles/avanov.pyenv/tasks/main.yml | 20 +++ roles/avanov.pyenv/templates/.pyenvrc.j2 | 5 + roles/avanov.pyenv/test.yml | 8 ++ roles/avanov.pyenv/test_update.yml | 9 ++ roles/avanov.pyenv/tests/test_update_vars.yml | 2 + roles/avanov.pyenv/vars/main.yml | 1 + site.yml | 12 ++ 19 files changed, 437 insertions(+) create mode 100644 roles/avanov.pyenv/.gitignore create mode 100644 roles/avanov.pyenv/.travis.yml create mode 100644 roles/avanov.pyenv/LICENSE create mode 100644 roles/avanov.pyenv/README.md create mode 100644 roles/avanov.pyenv/defaults/main.yml create mode 100644 roles/avanov.pyenv/handlers/main.yml create mode 100644 roles/avanov.pyenv/meta/.galaxy_install_info create mode 100644 roles/avanov.pyenv/meta/main.yml create mode 100644 roles/avanov.pyenv/tasks/Darwin.yml create mode 100644 roles/avanov.pyenv/tasks/Debian.yml create mode 100644 roles/avanov.pyenv/tasks/RedHat.yml create mode 100644 roles/avanov.pyenv/tasks/install.yml create mode 100644 roles/avanov.pyenv/tasks/main.yml create mode 100644 roles/avanov.pyenv/templates/.pyenvrc.j2 create mode 100644 roles/avanov.pyenv/test.yml create mode 100644 roles/avanov.pyenv/test_update.yml create mode 100644 roles/avanov.pyenv/tests/test_update_vars.yml create mode 100644 roles/avanov.pyenv/vars/main.yml diff --git a/roles/avanov.pyenv/.gitignore b/roles/avanov.pyenv/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/roles/avanov.pyenv/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/roles/avanov.pyenv/.travis.yml b/roles/avanov.pyenv/.travis.yml new file mode 100644 index 0000000..5653b85 --- /dev/null +++ b/roles/avanov.pyenv/.travis.yml @@ -0,0 +1,16 @@ +--- +language: python +python: "2.7" +#env: +before_install: + - sudo apt-get update -qq + - sudo apt-get install -qq python-apt python-pycurl +install: + - pip install ansible==1.9.4 +script: + - echo localhost > inventory + - ansible-playbook -i inventory test.yml --syntax-check + - ansible-playbook -i inventory test.yml --connection=local --sudo -vvv --diff + - ansible-playbook -i inventory test_update.yml --syntax-check + - ansible-playbook -i inventory test_update.yml --connection=local --sudo -vvv --diff +#after_failure: diff --git a/roles/avanov.pyenv/LICENSE b/roles/avanov.pyenv/LICENSE new file mode 100644 index 0000000..b837b3a --- /dev/null +++ b/roles/avanov.pyenv/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Maxim Avanov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/roles/avanov.pyenv/README.md b/roles/avanov.pyenv/README.md new file mode 100644 index 0000000..6eab9ec --- /dev/null +++ b/roles/avanov.pyenv/README.md @@ -0,0 +1,70 @@ +avanov.pyenv +============ + +[![Build Status](https://travis-ci.org/avanov/ansible-galaxy-pyenv.svg)](https://travis-ci.org/avanov/ansible-galaxy-pyenv) + + +Ansible Galaxy role for [pyenv](https://github.com/yyuu/pyenv) on Ubuntu / RedHat / OSX. + +Install it with the following command: + +```bash +$ ansible-galaxy install avanov.pyenv +``` + +Requirements +------------ + +None + +Role Variables +-------------- + +Here is the list of all variables and their default values: + +* ``pyenv_env: "user"`` +* ``pyenv_path: "{% if pyenv_env == 'user' %}{{ ansible_env.HOME }}/pyenv{% else %}/usr/local/pyenv{% endif %}"`` +* ``pyenv_owner: "{{ ansible_env.USER }}"`` +* ``pyenv_python_versions: ["3.4.1"]`` +* ``pyenv_virtualenvs: [{ venv_name: "latest", py_version: "3.4.1" }]`` +* ``pyenv_global: "3.4.1"`` +* ``pyenv_update_git_install: no`` +* ``pyenv_enable_autocompletion: no`` +* ``pyenv_setting_path: "{% if pyenv_env == 'user' %}~/.bashrc{% else %}/etc/profile.d/pyenv.sh{% endif %}"`` + +Dependencies +------------ + +None + +Example Playbook +------------------------- + + - hosts: servers + roles: + - role: avanov.pyenv + pyenv_path: "{{ home }}/pyenv" + pyenv_owner: "{{ instance_owner }}" + pyenv_global: "3.6.5" + pyenv_update_git_install: no + pyenv_enable_autocompletion: no + pyenv_python_versions: + - "3.6.5" + - "3.4.1" + - "2.7.8" + pyenv_virtualenvs: + - venv_name: "latest_v3" + py_version: "3.4.1" + + - venv_name: "latest_v2" + py_version: "2.7.8" + +License +------- + +MIT + +Author Information +------------------ + +Maxim Avanov (https://maximavanov.com/) diff --git a/roles/avanov.pyenv/defaults/main.yml b/roles/avanov.pyenv/defaults/main.yml new file mode 100644 index 0000000..10f8c3e --- /dev/null +++ b/roles/avanov.pyenv/defaults/main.yml @@ -0,0 +1,55 @@ +--- +# defaults file for pyenv +pyenv_env: "user" +pyenv_path: "{% if pyenv_env == 'user' %}{{ ansible_env.HOME }}/pyenv{% else %}/usr/local/pyenv{% endif %}" +pyenv_owner: "{{ ansible_env.USER }}" +pyenv_setting_path: "{% if pyenv_env == 'user' %}~/.bashrc{% else %}/etc/profile.d/pyenv.sh{% endif %}" +pyenv_update_git_install: no +pyenv_enable_autocompletion: no +pyenv_python_versions: + - 3.5.0 +pyenv_virtualenvs: + - { venv_name: "latest", py_version: "3.5.0" } +# For a system install, the shims dir will not be writable by users, disable rehashing +pyenv_init_options: "{% if pyenv_env != 'user' %}--no-rehash{% endif %}" + +pyenv_update: no + +# additional options for the build process, e.g "--enable-shared" +pyenv_python_configure_opts: "" + +pyenv_debian_packages: + - build-essential + - git + # On Ubuntu 12.04 build may fail with the following error: + # python-build: wget (< 1.14) doesn't support Server Name Indication. + # Please install curl (>= 7.18.1) and try again + - curl + - libssl-dev + - libbz2-dev + - libsqlite3-dev + - libreadline-dev + - zlib1g-dev + - wget + - llvm + - libncurses5-dev + - xz-utils + - tk-dev + - libxml2-dev + - libxmlsec1-dev + - libffi-dev +pyenv_redhat_packages: + - make + - git + - gcc + - libselinux-python + - zlib-devel + - openssl-devel + - bzip2-devel + - readline-devel + - libffi-devel + - sqlite-devel + - gdbm-devel +pyenv_osx_packages: + - readline + - xz diff --git a/roles/avanov.pyenv/handlers/main.yml b/roles/avanov.pyenv/handlers/main.yml new file mode 100644 index 0000000..37fdc14 --- /dev/null +++ b/roles/avanov.pyenv/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for pyenv diff --git a/roles/avanov.pyenv/meta/.galaxy_install_info b/roles/avanov.pyenv/meta/.galaxy_install_info new file mode 100644 index 0000000..81a0d9d --- /dev/null +++ b/roles/avanov.pyenv/meta/.galaxy_install_info @@ -0,0 +1,2 @@ +install_date: Thu Sep 26 20:02:57 2019 +version: 1.1.0 diff --git a/roles/avanov.pyenv/meta/main.yml b/roles/avanov.pyenv/meta/main.yml new file mode 100644 index 0000000..db0a19d --- /dev/null +++ b/roles/avanov.pyenv/meta/main.yml @@ -0,0 +1,129 @@ +--- +galaxy_info: + author: Maxim Avanov + description: Ansible Galaxy role for pyenv (https://github.com/yyuu/pyenv). + company: + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: MIT + min_ansible_version: 2.0 + # + # Below are all platforms currently available. Just uncomment + # the ones that apply to your role. If you don't see your + # platform on this list, let us know and we'll get it added! + # + platforms: + - name: macOS + versions: + - Sierra + + - name: EL + versions: + # - all + # - 5 + - 6 + - 7 + #- name: GenericUNIX + # versions: + # - all + # - any + #- name: Fedora + # versions: + # - all + # - 16 + # - 17 + # - 18 + # - 19 + # - 20 + #- name: opensuse + # versions: + # - all + # - 12.1 + # - 12.2 + # - 12.3 + # - 13.1 + # - 13.2 + #- name: Amazon + # versions: + # - all + # - 2013.03 + # - 2013.09 + #- name: GenericBSD + # versions: + # - all + # - any + #- name: FreeBSD + # versions: + # - all + # - 8.0 + # - 8.1 + # - 8.2 + # - 8.3 + # - 8.4 + # - 9.0 + # - 9.1 + # - 9.1 + # - 9.2 + - name: Ubuntu + versions: + # - all + # - lucid + # - maverick + # - natty + # - oneiric + # - precise + # - quantal + # - raring + # - saucy + - trusty + - xenial + - yakkety + #- name: SLES + # versions: + # - all + # - 10SP3 + # - 10SP4 + # - 11 + # - 11SP1 + # - 11SP2 + # - 11SP3 + #- name: GenericLinux + # versions: + # - all + # - any + #- name: Debian + # versions: + # - all + # - etch + # - lenny + # - squeeze + # - wheezy + # + # Below are all categories currently available. Just as with + # the platforms above, uncomment those that apply to your role. + # + categories: + #- cloud + #- cloud:ec2 + #- cloud:gce + #- cloud:rax + #- clustering + #- database + #- database:nosql + #- database:sql + - development + #- monitoring + #- networking + #- packaging + - system + #- web +dependencies: [] + # List your role dependencies here, one per line. Only + # dependencies available via galaxy should be listed here. + # Be sure to remove the '[]' above if you add dependencies + # to this list. diff --git a/roles/avanov.pyenv/tasks/Darwin.yml b/roles/avanov.pyenv/tasks/Darwin.yml new file mode 100644 index 0000000..79d9cb8 --- /dev/null +++ b/roles/avanov.pyenv/tasks/Darwin.yml @@ -0,0 +1,5 @@ +- name: Install development packages necessary for building Python + homebrew: + name: "{{ pyenv_osx_packages }}" + state: present + become: false \ No newline at end of file diff --git a/roles/avanov.pyenv/tasks/Debian.yml b/roles/avanov.pyenv/tasks/Debian.yml new file mode 100644 index 0000000..7df9589 --- /dev/null +++ b/roles/avanov.pyenv/tasks/Debian.yml @@ -0,0 +1,12 @@ +--- +- name: Ensure apt cache is up to date + apt: + update_cache: yes + cache_valid_time: "{{ 48 * 60 * 60 }}" # consider the cache to be valid within 48 hours + become: true + +- name: Install development packages necessary for building Python + apt: + pkg: "{{ pyenv_debian_packages }}" + state: present + become: true diff --git a/roles/avanov.pyenv/tasks/RedHat.yml b/roles/avanov.pyenv/tasks/RedHat.yml new file mode 100644 index 0000000..2cf498c --- /dev/null +++ b/roles/avanov.pyenv/tasks/RedHat.yml @@ -0,0 +1,8 @@ +--- +# Use action module which can interchangably use yum or dnf depending on what is available ( Eg. dnf on Fedora vs yum on Centos ) +# Note: special variable ansible_pkg_mgr requires fact_gathering to be enabled +# More: https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/installing_packages/ +- name: Install development packages necessary for building Python + action: > + {{ ansible_pkg_mgr }} name="{{ pyenv_redhat_packages }}" state=present + become: true diff --git a/roles/avanov.pyenv/tasks/install.yml b/roles/avanov.pyenv/tasks/install.yml new file mode 100644 index 0000000..2351254 --- /dev/null +++ b/roles/avanov.pyenv/tasks/install.yml @@ -0,0 +1,59 @@ +--- +- name: Install PyEnv + git: + repo: https://github.com/pyenv/pyenv.git + dest: "{{ pyenv_path }}" + update: "{{ pyenv_update_git_install }}" + +- name: Install PyEnv-virtualenv plugin + git: + repo: https://github.com/yyuu/pyenv-virtualenv.git + dest: "{{ pyenv_path }}/plugins/pyenv-virtualenv" + update: "{{ pyenv_update_git_install }}" + +- name: Install PyEnv-update plugin + git: + repo: https://github.com/pyenv/pyenv-update.git + dest: "{{ pyenv_path }}/plugins/pyenv-update" + update: "{{ pyenv_update_git_install }}" + when: pyenv_update + +- name: Install .pyenvrc + template: + src: ".pyenvrc.j2" + dest: "{{ pyenv_path }}/.pyenvrc" + owner: "{{ pyenv_owner }}" + mode: "0644" + +- name: "Load pyenv env variables in {{ pyenv_setting_path }}" + lineinfile: dest="{{ pyenv_setting_path }}" + regexp="\.pyenvrc$" + line="source {{ pyenv_path }}/.pyenvrc" + state=present + create=yes + +- name: "Add pyenv autocomplete in {{ pyenv_setting_path }}" + lineinfile: dest="{{ pyenv_setting_path }}" + regexp="pyenv\.bash$" + line="source {{ pyenv_path }}/completions/pyenv.bash" + state=present + when: pyenv_enable_autocompletion + +- name: Update Pyenv interpreter list + shell: . {{ pyenv_path }}/.pyenvrc && pyenv update + when: pyenv_update + +- name: Install Python interpreters "{{ pyenv_python_versions }}" + shell: . {{ pyenv_path }}/.pyenvrc && env PYTHON_CONFIGURE_OPTS="{{ pyenv_python_configure_opts }}" pyenv install {{ item }} + creates="{{ pyenv_path }}/versions/{{ item }}/bin/python" + with_items: "{{ pyenv_python_versions }}" + +- name: Create virtual environments + shell: . {{ pyenv_path }}/.pyenvrc && pyenv virtualenv {{ item.py_version }} {{ item.venv_name }} + creates="{{ pyenv_path }}/versions/{{ item.venv_name }}/bin/python" + with_items: "{{ pyenv_virtualenvs }}" + +- name: Set pyenv global + shell: . {{ pyenv_path }}/.pyenvrc && pyenv global {{ pyenv_global }} && pyenv rehash + when: pyenv_global is defined + diff --git a/roles/avanov.pyenv/tasks/main.yml b/roles/avanov.pyenv/tasks/main.yml new file mode 100644 index 0000000..60cff72 --- /dev/null +++ b/roles/avanov.pyenv/tasks/main.yml @@ -0,0 +1,20 @@ +--- +- include: Darwin.yml + when: ansible_os_family == "Darwin" + +- include: Debian.yml + when: ansible_os_family == "Debian" + +- include: RedHat.yml + when: ansible_os_family == "RedHat" + + +- include: install.yml + become: true + become_user: "{{ pyenv_owner }}" + when: pyenv_env == "user" + +- include: install.yml + become: true + when: pyenv_env == "system" + diff --git a/roles/avanov.pyenv/templates/.pyenvrc.j2 b/roles/avanov.pyenv/templates/.pyenvrc.j2 new file mode 100644 index 0000000..762f604 --- /dev/null +++ b/roles/avanov.pyenv/templates/.pyenvrc.j2 @@ -0,0 +1,5 @@ +# Include this file into your ~/.bashrc +# ------------------------------------- +export PYENV_ROOT="{{ pyenv_path }}" +export PATH="$PYENV_ROOT/bin:$PATH" +eval "$(pyenv init - {{ pyenv_init_options}})" \ No newline at end of file diff --git a/roles/avanov.pyenv/test.yml b/roles/avanov.pyenv/test.yml new file mode 100644 index 0000000..155c8dd --- /dev/null +++ b/roles/avanov.pyenv/test.yml @@ -0,0 +1,8 @@ +- hosts: all + gather_facts: True + vars_files: + - 'defaults/main.yml' + tasks: + - debug: + msg: "Owner: {{ pyenv_owner }}" + - include: 'tasks/main.yml' diff --git a/roles/avanov.pyenv/test_update.yml b/roles/avanov.pyenv/test_update.yml new file mode 100644 index 0000000..3d7cd1a --- /dev/null +++ b/roles/avanov.pyenv/test_update.yml @@ -0,0 +1,9 @@ +- hosts: all + gather_facts: True + vars_files: + - 'defaults/main.yml' + - 'tests/test_update_vars.yml' + tasks: + - debug: + msg: "Owner: {{ pyenv_owner }}" + - include: 'tasks/main.yml' diff --git a/roles/avanov.pyenv/tests/test_update_vars.yml b/roles/avanov.pyenv/tests/test_update_vars.yml new file mode 100644 index 0000000..a10908f --- /dev/null +++ b/roles/avanov.pyenv/tests/test_update_vars.yml @@ -0,0 +1,2 @@ +--- +pyenv_update: yes \ No newline at end of file diff --git a/roles/avanov.pyenv/vars/main.yml b/roles/avanov.pyenv/vars/main.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/roles/avanov.pyenv/vars/main.yml @@ -0,0 +1 @@ +--- diff --git a/site.yml b/site.yml index 69eeb16..0d21c04 100644 --- a/site.yml +++ b/site.yml @@ -5,4 +5,16 @@ users: - username: '{{ primary_user }}' - nvim + - role: avanov.pyenv + pyenv_path: $HOME/pyenv + pyenv_owner: "{{ primary_user }}" + pyenv_global: "system" + pyenv_python_versions: + - "3.7.4" + - "2.7.8" + pyenv_virtualenvs: + - venv_name: "latest_v3" + py_version: "3.7.4" + - venv_name: "latest_v2" + py_version: "2.7.8" vars_files: ~/projects/ansible_vars/vars.yml