title: Using pyenv in Ansible
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I have a <code>deploy</code> 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):</p>
  
  <pre><code>- 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 &amp;&amp; pyenv install 3.5.1
      creates="/home/deploy/.pyenv/versions/3.5.1"
  </code></pre>
  
  <p>When doing <code>vagrant up</code> it goes fine until:</p>
  
  <blockquote>
    <p>TASK [python : Install default python version] *********************************
    fatal: [192.168.33.15]: FAILED! => {"changed": true, "cmd": ". /home/deploy/.bashrc &amp;&amp; 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": []}</p>
  </blockquote>
  
  <p><strong>UPDATE:</strong> In case it's important, in this instance (the Vagrant box) my <code>vagrant.yml</code> playbook is setting <code>remote_user</code> to <code>vagrant</code>:</p>
  
  <pre><code>- name: Create a virtual machine via vagrant
    hosts: all
    become: yes
    become_method: sudo
    remote_user: vagrant
    ...
  </code></pre>
  
  <p><strong>UPDATE 2:</strong> If I ssh into the Vagrant VM as the <code>deploy</code> user then I can use <code>pyenv</code> OK. If I ssh in as <code>vagrant</code> and then <code>sudo -u deploy bash -i</code> I get <code>pyenv: command not found</code>...</p>
  
  <p><strong>UPDATE 3:</strong> The root of the problem <em>might</em> be that neither <code>/home/deploy/.bashrc</code> or <code>/home/deploy/.profile</code> are sourced when switching to the <code>deploy</code> user with <code>sudo</code> (tested by <code>echo</code>ing from each file), but are when logging in as <code>deploy</code>. But, I think <code>/home/deploy/.bashrc</code> <em>is</em> being sourced by the failing task - echo'd text appears in stdout.</p>