In one of our previous post we covered how to install Python 2.7 on Centos using pyenv which is a Python versions management tool. Today we will show how to use ansible to do that for you. (If you are on Ubuntu you might want to check ansible-galaxy-pyenv .) Manual installation process is described here. This is how it looks like:
git clone git://github.com/yyuu/pyenv.git .pyenv echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init -)"' >> ~/.bash_profile exec $SHELL pyenv install 2.7.6 pyenv rehash
Pretty simple. But why bother to do it on each server command by command if you can use ansible?
It’s supposed you have some user (we use devops) which is able to log in to our target host (example.com) and is also able to run commands as root via sudo.
Here’s how to do that using a playbook:
- hosts: example.com user: devops vars: home: home/devops tasks: - name: pyenv | check pyenv installed command: test -x {{ home }}/.pyenv/bin/pyenv register: pyenv_present ignore_errors: yes - name: pyenv | install devel packages sudo: yes yum: pkg: "{{ item }}" state: present with_items: - gcc - patch - sqlite-devel - readline-devel - openssl-devel # http://hakunin.com/six-ansible-practices - name: ensure github.com is a known host lineinfile: dest: "{{ home }}/.ssh/known_hosts" create: yes state: present line: "{{ lookup('pipe', 'ssh-keyscan -t rsa github.com') }}" regexp: "^github\\.com" - name: pyenv | update pyenv repo git: repo=git://github.com/yyuu/pyenv.git dest="{{ home }}/.pyenv/" when: pyenv_present|failed ignore_errors: yes - name: define environment variable 1 shell: echo 'export PYENV_ROOT="$HOME/.pyenv"' >> "{{ home }}/.bash_profile" when: pyenv_present|failed - name: define environment variable 2 shell: echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> "{{ home }}/.bash_profile" when: pyenv_present|failed - name: add pyenv init to your shell to enable shims and autocompletion. shell: echo 'eval "$(pyenv init -)"' >> "{{ home }}/.bash_profile" when: pyenv_present|failed - name: install python versions into $PYENV_ROOT/versions sudo: no shell: " . {{ home }}/.bash_profile && pyenv install 2.7.6" when: pyenv_present|failed - name: rebuild the shim binaries sudo: no shell: " . {{ home }}/.bash_profile && pyenv rehash" when: pyenv_present|failed - name: list installed ruby version sudo: no shell: " . {{ home }}/.bash_profile && pyenv versions " register: py_versions - debug: var=py_versions
Check:
ansible-playbook install_pyenv.yml -vvv -s --check
and run:
ansible-playbook install_pyenv.yml -vvv -s
After it’s done you can start using Python 2.7 on Centos:
[devops@example.com ~]$ python -V Python 2.7.6 [devops@example.com ~]$