--- - name: "Install pyenv and pipx" ansible.builtin.command: cmd: "/home/linuxbrew/.linuxbrew/bin/brew install {{ item }}" creates: "/home/linuxbrew/.linuxbrew/bin/{{ item }}" with_items: - "pyenv" - "pipx" - name: "Add pyenv to shell profile" ansible.builtin.blockinfile: path: "{{ ansible_env.HOME }}/.bashrc" state: "present" prepend_newline: true append_newline: true marker: "# ==== {mark} ANSIBLE PYENV CONFIG" block: | if command -v pyenv 1>/dev/null 2>&1; then export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init - bash)" fi - name: "Add pipx to shell profile" ansible.builtin.blockinfile: path: "{{ ansible_env.HOME }}/.bashrc" state: "present" prepend_newline: true append_newline: true marker: "# ==== {mark} ANSIBLE PIPX CONFIG" block: | if command -v pipx 1>/dev/null 2>&1; then export PATH="$PATH:$HOME/.local/bin" fi - name: "Get installed python versions" ansible.builtin.command: cmd: "/home/linuxbrew/.linuxbrew/bin/pyenv versions --bare" register: installed_python_versions changed_when: false failed_when: false - name: "Remove unwanted python versions" vars: major_minor: "{{ item | regex_replace('^([0-9]+\\.[0-9]+).*', '\\1') }}" ansible.builtin.command: cmd: "/home/linuxbrew/.linuxbrew/bin/pyenv uninstall -f {{ item }}" loop: "{{ installed_python_versions.stdout_lines | default([]) }}" when: - installed_python_versions.rc == 0 - item is regex('^[0-9]+\.[0-9]+') - item not in python_versions - major_minor not in python_versions changed_when: true - name: "Install python versions" ansible.builtin.command: cmd: "/home/linuxbrew/.linuxbrew/bin/pyenv install {{ item }}" args: creates: "{{ ansible_env.HOME }}/.pyenv/versions/{{ item }}*/bin/python" with_items: "{{ python_versions }}" - name: "Set global python version" ansible.builtin.command: cmd: "/home/linuxbrew/.linuxbrew/bin/pyenv global {{ python_global_version }}" changed_when: true