diff options
| author | Ahmed AbdelHalim <[email protected]> | 2025-09-02 18:38:43 +0200 |
|---|---|---|
| committer | Ahmed AbdelHalim <[email protected]> | 2025-09-02 18:38:43 +0200 |
| commit | 4b39a934bafbd4d708919c2cdd3158d3c1f096aa (patch) | |
| tree | e3960b4143887960051ccedf9be2c859c1653af8 /roles/python | |
| parent | 99a0d72887d90e04a051a4b4df278115108be458 (diff) | |
Fix the python role to install and clean up in idempotent manner
Diffstat (limited to 'roles/python')
| -rw-r--r-- | roles/python/tasks/main.yml | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/roles/python/tasks/main.yml b/roles/python/tasks/main.yml index ea78bed0..d2dfc9a9 100644 --- a/roles/python/tasks/main.yml +++ b/roles/python/tasks/main.yml @@ -32,6 +32,30 @@ export PATH="$PATH:$HOME/.local/bin" fi +- name: "Get installed python versions" + ansible.builtin.command: + cmd: "pyenv versions --bare" + environment: + PATH: "/home/linuxbrew/.linuxbrew/bin:{{ ansible_env.PATH }}" + 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: "pyenv uninstall -f {{ item }}" + environment: + PATH: "/home/linuxbrew/.linuxbrew/bin:{{ ansible_env.PATH }}" + 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: "pyenv install {{ item }}" @@ -39,7 +63,7 @@ creates: "{{ ansible_env.HOME }}/.pyenv/versions/{{ item }}*/bin/python" environment: PATH: "/home/linuxbrew/.linuxbrew/bin:{{ ansible_env.PATH }}" - with_items: "{{ python_versions | default([]) }}" + with_items: "{{ python_versions }}" - name: "Set global python version" ansible.builtin.command: |
