summaryrefslogtreecommitdiffstats
path: root/roles/python/tasks/main.yml
blob: d2dfc9a9f01b41d79e04478cf4739be329504e8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
- name: "Install pyenv and pipx"
  community.general.homebrew:
    name:
      - "pyenv"
      - "pipx"
    state: "present"

- 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: "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 }}"
  args:
    creates: "{{ ansible_env.HOME }}/.pyenv/versions/{{ item }}*/bin/python"
  environment:
    PATH: "/home/linuxbrew/.linuxbrew/bin:{{ ansible_env.PATH }}"
  with_items: "{{ python_versions }}"

- name: "Set global python version"
  ansible.builtin.command:
    cmd: "pyenv global {{ python_global_version }}"
  environment:
    PATH: "/home/linuxbrew/.linuxbrew/bin:{{ ansible_env.PATH }}"
  changed_when: false