summaryrefslogtreecommitdiffstats
path: root/roles/python/tasks/main.yml
blob: cb41e1ca0a2843d37e44bb429b14c45da9e9aae1 (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
---
- 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: python_installed_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: "{{ python_installed_versions.stdout_lines | default([]) }}"
  when:
    - python_installed_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