ansible playbook 安装python版本管理神器-pyenv
环境:
腾讯云 Centos7.4 64位
基础安装
确认安装gcc, openssl openssl-devel升级到最新版本, yum -y install gcc openssl openssl-devel
ansible-playbook 脚本
[root@VM_32_7_centos playbook]# cat pyenv_python3.yml
---
- hosts: '{{ host }}'
remote_user: root
gather_facts: true
tasks:
- name: Install pyenv
git:
repo: https://github.com/pyenv/pyenv.git
dest: "/root/.pyenv"
- name: Install pyenv-virtualenv plugin
git:
repo: https://github.com/pyenv/pyenv-virtualenv.git
dest: "/root/.pyenv/plugins/pyenv-virtualenv"
- name: Add path etc to .bashrc.
lineinfile:
dest: "/root/.bashrc"
state: present
create: yes
line: "{{ item }}"
with_items:
- 'export PYENV_ROOT="$HOME/.pyenv"'
- 'export PATH="$PYENV_ROOT/bin:$PATH"'
- 'eval "$(pyenv init -)"'
- 'eval "$(pyenv virtualenv-init -)"'
- name: Ensure .pyenv permissions are set properly
file: path=/root/.pyenv
recurse=yes
owner=root
group=root
state=directory
- name: Install default python version
shell: . /root/.bashrc && pyenv install 3.5.1
tags:
- python3
- name:
shell: . /root/.bashrc && pyenv global 3.5.1
- name:
shell: pip install --upgrade pip
tags: pip
执行playbook
ansible-playbook pyenv_python3.yml --extra-vars "host=172.21.32.8"
验证
[root@VM_32_8_centos bin]# python3 --version
Python 3.5.1
[root@VM_32_8_centos bin]# pip3 --version
pip 18.1 from /root/.pyenv/versions/3.5.1/lib/python3.5/site-packages/pip (python 3.5)
pyenv 用法
[root@centos-32-7 playbook]# pyenv --help
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
版本切换
显示可选安装列表:
pyenv install -l
安装另一版本:
pyenv install 3.6.6
显示当前系统内可选择的版本:可以看到现在有两个版本,当前版本是3.6.6
[root@centos-140 ~]# pyenv versions
system
3.5.1
* 3.6.6 (set by /root/.pyenv/version)
查看当前版本:
[root@centos-140 ~]# pyenv version
3.6.6 (set by /root/.pyenv/version)
切换默认版本:
pyenv global 3.5.1
再次查看当前版本:
[root@centos-140 ~]# pyenv version
3.5.1 (set by /root/.pyenv/version)