Search code examples
python-3.xansiblepython-3.4pyenv

How to Install python3.4.3 with Ansible


I want to install python3.x by use pyenv with ansible.

- name: install pyenv
  git: >
    repo=https://github.com/pyenv/pyenv.git
    dest=/home/www/.pyenv
    accept_hostkey=yes
    become: yes
    become_user: www

- name: enable pyenv
  shell: |
    echo 'export PYENV_ROOT="/home/www/.pyenv"' >> /home/www/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> /home/www/.bashrc
    echo 'eval "$(pyenv init -)"' >> /home/www/.bashrc
- name: install python
  shell: pyenv install 3.4.3

How to install python3.x with ansible?


Solution

  • Rather than using the shell module to set environment variables on the remote host, Ansible has the environment keyword, which can set per task or even per playbook.

    Assuming the www user already exists I managed to get this working with some more specific path setting:

    - name: enable pyenv and install python
      shell: /home/www/.pyenv/bin/pyenv init - && /home/www/.pyenv/bin/pyenv install 3.4.3 chdir=/home/www
      environment:
        pyenv_root: /home/www/.pyenv
        path: "{{ pyenv_root }}/bin:$PATH"
      become: yes
      become_user: www
    

    You will need to run the playbook with:

    ansible-playbook --ask-become-pass <playbook-name>
    

    and supply the password for the www user on request.

    If that doesn't work, you might have to post the whole playbook here for us to look at :)