Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pyenv in Ansible

I'm using Ansible to set up Ubuntu-based Vagrant and DigitalOcean boxes, and want to use pyenv to manage environments for a few different websites.

I'm having a problem with permissions when trying to install a version of python using the pyenv I've installed, which is probably down to my lack of basic *nix knowledge.

I have a deploy user and group, who I've installed pyenv for, but obviously something's up with which user's doing things as the final task below fails (all variables replaced with strings for clarity):

- name: Install pyenv
  git:
    repo: https://github.com/yyuu/pyenv.git
    dest: "/home/deploy/.pyenv"

- name: Install pyenv-virtualenv plugin
  git:
    repo: https://github.com/yyuu/pyenv-virtualenv.git
    dest: "/home/deploy/.pyenv/plugins/pyenv-virtualenv"

- name: Add path etc to .bashrc.
  lineinfile:
    dest: "/home/deploy/.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=/home/deploy/.pyenv
        recurse=yes
        owner=deploy
        group=deploy
        state=directory

- name: Install default python version
  become: yes
  become_user: 'deploy'
  shell: . /home/deploy/.bashrc && pyenv install 3.5.1
    creates="/home/deploy/.pyenv/versions/3.5.1"

When doing vagrant up it goes fine until:

TASK [python : Install default python version] ********************************* fatal: [192.168.33.15]: FAILED! => {"changed": true, "cmd": ". /home/deploy/.bashrc && pyenv install 3.5.1", "delta": "0:00:00.002111", "end": "2016-02-16 11:48:26.930971", "failed": true, "rc": 127, "start": "2016-02-16 11:48:26.928860", "stderr": "/bin/sh: 1: pyenv: not found", "stdout": "", "stdout_lines": [], "warnings": []}

UPDATE: In case it's important, in this instance (the Vagrant box) my vagrant.yml playbook is setting remote_user to vagrant:

- name: Create a virtual machine via vagrant
  hosts: all
  become: yes
  become_method: sudo
  remote_user: vagrant
  ...

UPDATE 2: If I ssh into the Vagrant VM as the deploy user then I can use pyenv OK. If I ssh in as vagrant and then sudo -u deploy bash -i I get pyenv: command not found...

UPDATE 3: The root of the problem might be that neither /home/deploy/.bashrc or /home/deploy/.profile are sourced when switching to the deploy user with sudo (tested by echoing from each file), but are when logging in as deploy. But, I think /home/deploy/.bashrc is being sourced by the failing task - echo'd text appears in stdout.

like image 897
Phil Gyford Avatar asked Feb 16 '16 12:02

Phil Gyford


1 Answers

As your error is:

pyenv: not found

just try simply to use absoulute path to pyenv in your task. This is a recommended way to handle shell tasks anyway:

Log in to your machine and find out the path to pyenv

which pyenv

then change your task to

 # /path/to/pyenv is the result of the previous command
 ... 
 shell: . /home/deploy/.bashrc && /path/to/pyenv install 3.5.1
 ...
like image 120
ProfHase85 Avatar answered Oct 16 '22 22:10

ProfHase85