Skip to content

Commit

Permalink
Move Go-package-installing gubbins to a new role.
Browse files Browse the repository at this point in the history
  • Loading branch information
damiendart committed Oct 7, 2023
1 parent a977159 commit fa515d6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 13 deletions.
10 changes: 10 additions & 0 deletions roles/development/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@
loop_control:
label: '{{ item.filename | default(item.url | basename) }}'

- name: 'Ensure base development-related Go packages are installed'
ansible.builtin.include_role:
name: 'golang_package'
vars:
golang_package__packages:
- 'github.com/damiendart/pathshorten/cmd/pathshorten@latest'
- 'github.com/damiendart/snippets/snippet-placeholder@latest'
- 'golang.org/x/lint/golint@latest'
- 'honnef.co/go/tools/cmd/staticcheck@latest'

- name: 'Include dotfiles installation tasks'
ansible.builtin.include_tasks: 'dotfiles.yml'

Expand Down
3 changes: 1 addition & 2 deletions roles/golang/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
golang
======

Installs Go from a binary archive, and optionally installs Go packages
into a user's home directory with `go install`.
Installs Go from a binary archive.


## Configuration and usage
Expand Down
4 changes: 0 additions & 4 deletions roles/golang/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ golang__version: '1.21.0'
# A string containing the directory where downloaded files are stored to
# prevent unnecessary downloading when tasks are rerun.
golang__download_cache_directory: '{{ undef() }}'

# A string specifying the host account username that will "go install"
# the packages listed in "golang__installable_packages".
golang__user_username: '{{ undef() }}'
26 changes: 19 additions & 7 deletions roles/golang/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,22 @@
remote_src: true
src: '{{ golang__download_cache_directory }}/go{{ golang__version }}.linux-amd64.tar.gz'

- name: 'Ensure Go-related tools are installed'
ansible.builtin.command: '/usr/local/go/bin/go install {{ item }}'
become: true
become_user: '{{ golang__user_username }}'
register: __go_install
changed_when: __go_install.stderr is search('downloading')
loop: '{{ golang__installable_packages }}'
- name: 'Ensure Ansible local facts directory exists'
ansible.builtin.file:
mode: 0755
path: '/etc/ansible/facts.d'
state: 'directory'

- name: 'Ensure Go-related local facts exists'
ansible.builtin.template:
dest: '/etc/ansible/facts.d/golang.fact'
group: 'root'
mode: 0644
owner: 'root'
src: 'golang.fact.j2'
register: __golang_facts

- name: 'Ensure Ansible facts are up-to-date'
ansible.builtin.setup: # noqa: no-handler
filter: 'ansible_local'
when: __golang_facts is changed
8 changes: 8 additions & 0 deletions roles/golang/templates/golang.fact.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{#
This file was written by Damien Dart, <[email protected]>. This is
free and unencumbered software released into the public domain. For
more information, please refer to the accompanying "UNLICENCE" file.
#}
{
"bin_dir": "/usr/local/go/bin"
}
12 changes: 12 additions & 0 deletions roles/golang_package/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# "golang_package" Ansible role variables.
#
# This file was written by Damien Dart, <[email protected]>. This is
# free and unencumbered software released into the public domain. For
# more information, please refer to the accompanying "UNLICENCE" file.

---
golang_package__gopath: '/home/{{ golang_package__user }}/.go'

golang_package__packages: []

golang_package__user: '{{ undef() }}'
7 changes: 7 additions & 0 deletions roles/golang_package/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was written by Damien Dart, <[email protected]>. This is
# free and unencumbered software released into the public domain. For
# more information, please refer to the accompanying "UNLICENCE" file.

---
dependencies:
- role: 'golang'
16 changes: 16 additions & 0 deletions roles/golang_package/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Compiles and installs Go packages using "go install".
#
# This file was written by Damien Dart, <[email protected]>. This is
# free and unencumbered software released into the public domain. For
# more information, please refer to the accompanying "UNLICENCE" file.

---
- name: 'Ensure Go packages are compiled and installed for user "{{ golang_package__user }}"'
ansible.builtin.command: '{{ ansible_local.golang.bin_dir }}/go install {{ item }}'
become: true
become_user: '{{ golang_package__user }}'
environment:
GOPATH: '{{ golang_package__gopath }}'
register: __go_install
changed_when: __go_install.stderr is search('downloading')
loop: '{{ golang_package__packages }}'

0 comments on commit fa515d6

Please sign in to comment.