Simplify go ansible role

This commit is contained in:
ionutboangiu
2023-08-11 11:22:31 -04:00
committed by Dan Christian Bogos
parent 9b0116516e
commit f2a3af6ddf
3 changed files with 31 additions and 84 deletions

View File

@@ -1,29 +1,7 @@
---
# Package manager to use
package_manager: apt
# Go version to install
golang_version: 1.20.6
# Expected go version output
go_version_target: "go version go{{ golang_version }} linux/amd64"
# Set the Go workspace directory
golang_gopath: /home/{{ ansible_user }}/go
# Set the directory to download the Go archive to
golang_download_dir: /tmp
# Set the mirror to download Go from
golang_mirror: https://storage.googleapis.com/golang
# Set the filename of the Go archive to download
golang_redis_filename: go{{ golang_version }}.linux-amd64.tar.gz
# Set the Go installation directory
golang_install_dir: /usr/local/go
# Go dependencies
go_dependencies:
- curl
- tar
go_version: "1.21.0"
go_platform: linux
go_arch: amd64
go_tarball: go{{ go_version }}.{{ go_platform }}-{{ go_arch }}.tar.gz
go_download_url: https://dl.google.com/go/{{ go_tarball }}
go_checksum: "d0398903a16ba2232b389fb31032ddf57cac34efda306a0eebac34f0965a0742"

View File

@@ -1,56 +1,36 @@
---
- name: Check if Go is already installed
- name: Check if Go is already installed.
command: /usr/local/go/bin/go version
ignore_errors: true
register: go_version_output
register: go_version_result
changed_when: false
- name: Install Go dependencies with apt
become: yes
apt:
name: '{{ go_dependencies }}'
state: present
update_cache: yes
when: package_manager == 'apt'
- name: Install Go dependencies with dnf
become: yes
dnf:
name: '{{ go_dependencies }}'
state: present
update_cache: yes
when: package_manager == 'dnf'
- name: Remove old installation of Go if needed
become: yes
- name: Remove current installation.
file:
path: /usr/local/go
state: absent
when: go_version_output.rc == 0 and go_version_output.stdout != go_version_target
path: /usr/local/go
when:
- go_version_result is succeeded
- go_version not in go_version_result.stdout
- name: Create Go language SDK installation directory
become: yes
file:
path: '{{ golang_install_dir }}'
state: directory
mode: '0755'
- name: Download Go language SDK
- name: Download Go.
get_url:
url: '{{ golang_mirror }}/{{ golang_redis_filename }}'
dest: '{{ golang_download_dir }}/{{ golang_redis_filename }}'
mode: '0644'
url: "{{ go_download_url }}"
dest: /usr/local/src/{{ go_tarball }}
checksum: "sha256:{{ go_checksum }}"
when: go_version_result is failed
or go_version not in go_version_result.stdout
- name: Install Go language SDK
become: yes
- name: Extract Go.
unarchive:
src: '{{ golang_download_dir }}/{{ golang_redis_filename }}'
remote_src: true
dest: '{{ golang_install_dir | dirname }}'
creates: '{{ golang_install_dir }}/bin'
src: /usr/local/src/{{ go_tarball }}
dest: /usr/local
copy: no
when: go_version_result is failed
or go_version not in go_version_result.stdout
- name: Set Go language SDK environment variables
become: yes
template:
src: golang.sh.j2
dest: /etc/profile.d/golang.sh
mode: '0644'
- name: Add Go to to system-wide $PATH.
copy:
dest: /etc/profile.d/go-path.sh
content: |-
export PATH=$PATH:/usr/local/go/bin

View File

@@ -1,11 +0,0 @@
#!/bin/sh
{# Comment that describes the purpose of the template #}
export GOROOT='{{ golang_install_dir }}'
export PATH=$PATH:$GOROOT/bin
{% if golang_gopath %}
export GOPATH="{{ golang_gopath }}"
export PATH=$PATH:$GOPATH/bin
{% endif %}