- set_fact:
    python: {}

- name: check pyenv
  command: |
    bash -lc 'which pyenv'
  register: which_pyenv
  ignore_errors: yes

- name: install pyenv
  command: |
    bash -lc 'anyenv install pyenv'
  when: which_pyenv.rc > 0

- name: get installed versions
  command: |
    bash -lc 'pyenv versions'
  register: installed_versions

- name: install python
  command: |
    bash -lc 'pyenv install {{ item | quote }}'
  when: |
    python.versions and installed_versions.stdout.find(item) == -1
  with_items: '{{ python.versions }}'

- name: set global python
  command: |
    bash -lc 'pyenv global {{ python.versions[0] | quote }}'
  when: python.versions

- name: install packages
  pip:
    name:               '{{ item.name | default(item) }}'
    extra_args:         '{{ item.extra_args | default("") }}'
    state:              latest
    virtualenv_command: pyenv
  become: yes
  with_items: '{{ python.packages }}'
  when: python.packages

- set_fact:
    python: {}

- name: check pyenv
  command: |
    bash -lc 'which pyenv'
  register: which_pyenv
  ignore_errors: yes

- name: install pyenv
  command: |
    bash -lc 'anyenv install pyenv'
  when: which_pyenv.rc > 0

- name: get installed versions
  command: |
    bash -lc 'pyenv versions'
  register: installed_versions

- name: install python
  command: |
    bash -lc 'pyenv install {{ item | quote }}'
  when: |
    python.versions and installed_versions.stdout.find(item) == -1
  with_items: '{{ python.versions }}'

- name: set global python
  command: |
    bash -lc 'pyenv global {{ python.versions[0] | quote }}'
  when: python.versions

- name: rehash
  shell: |
    if [ -f "$DOTSHIM_FILE" ]; then
      rm -f "$DOTSHIM_FILE"
    fi
    bash -lc 'pyenv rehash'
  environment:
    DOTSHIM_FILE: '{{ anyenv.envs_dir }}/pyenv/.pyenv-shim'

- name: install packages
  pip:
    name:               '{{ item.name | default(item) }}'
    extra_args:         '{{ item.extra_args | default("") }}'
    state:              latest
    virtualenv_command: pyenv
  become: yes
  with_items: '{{ python.packages }}'
  when: python.packages

Make rehash work for pyenv
Make rehash work for pyenv
