From 6036b8fde11820ced5c3dffb990df4668892d049 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Wed, 14 Oct 2020 14:36:05 -0600 Subject: [PATCH] Add a simple Golang role Add a simpel role to install Golang on a Linux system. Update documentation. Signed-off-by: Scott Lowe --- ansible/README.md | 2 ++ ansible/golang-role/README.md | 20 +++++++++++++ ansible/golang-role/tasks/main.yml | 47 ++++++++++++++++++++++++++++++ ansible/golang-role/vars/main.yml | 5 ++++ 4 files changed, 74 insertions(+) create mode 100644 ansible/golang-role/README.md create mode 100644 ansible/golang-role/tasks/main.yml create mode 100644 ansible/golang-role/vars/main.yml diff --git a/ansible/README.md b/ansible/README.md index 38ec18b..246a20f 100644 --- a/ansible/README.md +++ b/ansible/README.md @@ -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. diff --git a/ansible/golang-role/README.md b/ansible/golang-role/README.md new file mode 100644 index 0000000..18f15d1 --- /dev/null +++ b/ansible/golang-role/README.md @@ -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/` (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. diff --git a/ansible/golang-role/tasks/main.yml b/ansible/golang-role/tasks/main.yml new file mode 100644 index 0000000..a3322ff --- /dev/null +++ b/ansible/golang-role/tasks/main.yml @@ -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 diff --git a/ansible/golang-role/vars/main.yml b/ansible/golang-role/vars/main.yml new file mode 100644 index 0000000..b913502 --- /dev/null +++ b/ansible/golang-role/vars/main.yml @@ -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"