Add a simple Golang role

Add a simpel role to install Golang on a Linux system. Update documentation.

Signed-off-by: Scott Lowe <scott.lowe@scottlowe.org>
This commit is contained in:
Scott Lowe 2020-10-14 14:36:05 -06:00
parent ebdafe37fc
commit 6036b8fde1
No known key found for this signature in database
GPG key ID: 949F43F6E6C11780
4 changed files with 74 additions and 0 deletions

View file

@ -10,6 +10,8 @@ This folder contains tools, resources, and examples to help with learning how to
**extract-gh-archive**: This folder contains a Vagrant+Ansible environment that demonstrates a few techniques for using Ansible to download a binary release from GitHub, extract it into a temporary directory, copy out the relevant file(s), and then clean up.
**golang-role:** This directory contains a simple Ansible role for installing Go onto a Linux system.
**kubeadm-etcd-template**: This folder contains some example Jinja2 templates and an Ansible playbook for generating `kubeadm` config files for generating an etcd cluster.
**kubeadm-template**: In this folder is an example Jinja2 template and Ansible playbook that provide an example of how to create a templated Kubeadm configuration file.

View file

@ -0,0 +1,20 @@
# Installing Go on Linux
This directory contains a simple Ansible role for installing Go onto a Linux system. It's been tested with Debian 10.4 and Ubuntu 18.04, but should work with just about any Linux distribution.
## Contents
* **tasks/main.yml:** This contains the list of Ansible tasks to be performed when the role is executed.
* **vars/main.yml:** This contains some variables for the role.
## Instructions
Simply copy the contents of this directory into the correct location for your Ansible roles (this will vary based on use case), and then include the role in your Ansible playbook.
## More Information
* The role defaults to installing Go 1.15.2. You can change the version by changing the value of the `golang_version` variable in `vars/main.yml`.
* The role defaults to installing Go into the `/usr/local/go` directory. You can modify the prefix (say, to install to `/opt/go`) by changing the value of the `golang_install_path` variable in `vars/main.yml`.
* The role downloads the Go installation archive to `/usr/local/src/go<version>/` (creating this directory if necessary), and if the archive already exists it won't download it again.
* If `/usr/local/go/bin/go` exists, then the role won't extract the Go installation archive to install Go. If you've changed the `golang_install_path` variable, then the role uses this to check for the presence of the Go binary.

View file

@ -0,0 +1,47 @@
---
- name: "Fail if used on a non-Linux system"
fail:
msg: "This role is only compatible with Linux."
when:
ansible_system != "Linux"
- name: "Fail if used on a non-x86_64-based system"
fail:
msg: "This role only supports x86_64-based systems."
when:
ansible_architecture != "x86_64"
- name: "Determine if Go has already been installed"
stat:
path: "{{ golang_install_path }}/go/bin/go"
register: go_binary
- name: "Determine if Go archive has been downloaded"
stat:
path: "/usr/local/src/go{{ golang_version }}/{{ golang_file }}"
register: go_archive
- name: "Create a location to store Go archive"
file:
path: "/usr/local/src/go{{ golang_version }}"
state: "directory"
mode: 0755
when: not go_archive.stat.exists
- name: "Download Go archive"
get_url:
url: "{{ golang_dl_url }}"
dest: "/usr/local/src/go{{ golang_version }}/{{ golang_file }}"
owner: "root"
group: "root"
mode: 0755
when: not go_archive.stat.exists
- name: "Extract Go archive"
unarchive:
src: "/usr/local/src/go{{ golang_version }}/{{ golang_file }}"
dest: "{{ golang_install_path }}/"
remote_src: yes
owner: "root"
group: "root"
when: not go_binary.stat.exists

View file

@ -0,0 +1,5 @@
---
golang_version: "1.15.2"
golang_file: "go{{ golang_version }}.linux-amd64.tar.gz"
golang_dl_url: "https://golang.org/dl/{{ golang_file }}"
golang_install_path: "/usr/local"