summaryrefslogtreecommitdiffstats
path: root/roles/go/tasks/main.yml
blob: 73f065f311619220dac3f501efd1004e0a0d58ce (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
74
75
76
77
78
79
80
---
- name: "Ensure bash is installed"
  become: true
  ansible.builtin.package:
    name: "bash"
    state: "present"

- name: "Add Go to shell profile"
  ansible.builtin.blockinfile:
    path: "{{ ansible_env.HOME }}/.bashrc"
    state: "present"
    prepend_newline: true
    append_newline: true
    marker: "# ==== {mark} ANSIBLE GO CONFIG"
    block: |
      if command -v go 1>/dev/null 2>&1; then
          export PATH="$HOME/go/bin:$PATH"
      fi

- name: "Get installed Go versions"
  ansible.builtin.shell: |
    set -o pipefail
    /home/linuxbrew/.linuxbrew/bin/brew list | grep '^go@' | sed 's/go@//'
  args:
    executable: /bin/bash
  register: go_installed_versions
  changed_when: false
  failed_when: false

- name: "Remove unwanted Go versions"
  ansible.builtin.command:
    cmd: "/home/linuxbrew/.linuxbrew/bin/brew uninstall go@{{ item }}"
  with_items: "{{ go_installed_versions.stdout_lines | default([]) }}"
  when:
    - go_installed_versions.rc == 0
    - item not in go_versions
  changed_when: true

- name: "Install and configure Go latest"
  when: go_versions | length == 0
  block:
    - name: "Install latest Go"
      ansible.builtin.command:
        cmd: "/home/linuxbrew/.linuxbrew/bin/brew install go"
        creates: "/home/linuxbrew/.linuxbrew/bin/go"

    - name: "Set latest Go as global version"
      ansible.builtin.command:
        cmd: "/home/linuxbrew/.linuxbrew/bin/brew link --force go"
      # The set global version always run and is always changing the system
      # It's intentional to ignore this task for idempotency test
      changed_when: true
      tags:
        - molecule-idempotence-notest

- name: "Install and configure Go versions"
  when: go_versions | length > 0
  block:
    - name: "Install specific Go versions"
      ansible.builtin.command:
        cmd: "/home/linuxbrew/.linuxbrew/bin/brew install go@{{ item }}"
        creates: "/home/linuxbrew/.linuxbrew/Cellar/go@{{ item }}"
      with_items: "{{ go_versions }}"

    - name: "Unlink existing Go package"
      ansible.builtin.command:
        cmd: "/home/linuxbrew/.linuxbrew/bin/brew unlink go"
      when: go_global_version != ""
      failed_when: false
      changed_when: false

    - name: "Set specific Go version as global"
      ansible.builtin.command:
        cmd: "/home/linuxbrew/.linuxbrew/bin/brew link --overwrite go@{{ go_global_version }}"
      when: go_global_version != ""
      # The set global version always run and is always changing the system
      # It's intentional to ignore this task for idempotency test
      changed_when: true
      tags:
        - molecule-idempotence-notest