summaryrefslogtreecommitdiffstats
path: root/roles
diff options
context:
space:
mode:
authorAhmed Abdelhalim <[email protected]>2025-09-02 18:38:43 +0200
committerAhmed Abdelhalim <[email protected]>2025-09-02 18:38:43 +0200
commit9c775d0d9ceb52b74d44decfb8757b7750969750 (patch)
treee3960b4143887960051ccedf9be2c859c1653af8 /roles
parentdf383276d03786dcb4d703366b932421a9e9b74f (diff)
Fix the python role to install and clean up in idempotent manner
Diffstat (limited to 'roles')
-rw-r--r--roles/python/tasks/main.yml26
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: