From 07d92e37dc6a928dc22a82e4bed4e130acd50e6c Mon Sep 17 00:00:00 2001 From: Alexander Meindl Date: Thu, 9 Nov 2023 19:33:04 +0100 Subject: [PATCH] add golang role --- CHANGELOG.md | 4 + README.md | 1 + molecule/golang/converge.yml | 21 +++++ molecule/golang/molecule.yml | 23 ++++++ roles/ansible_node/defaults/main.yml | 2 +- roles/common/defaults/main.yml | 6 ++ roles/common/tasks/main.yml | 7 ++ roles/gitlab/defaults/main.yml | 3 +- roles/gitlab/tasks/go.yml | 58 -------------- roles/gitlab/tasks/setup.yml | 8 +- .../templates/gitlab-shell/config.yml.j2 | 2 +- roles/golang/README.md | 20 +++++ roles/golang/defaults/main.yml | 38 +++++++++ roles/golang/meta/main.yml | 22 +++++ roles/golang/tasks/main.yml | 13 +++ roles/golang/tasks/remove.yml | 23 ++++++ roles/golang/tasks/setup.yml | 80 +++++++++++++++++++ roles/golang/vars/main.yml | 4 + roles/matomo/meta/main.yml | 2 + roles/nextcloud/meta/main.yml | 2 + roles/rocketchat/meta/main.yml | 1 + 21 files changed, 277 insertions(+), 63 deletions(-) create mode 100644 molecule/golang/converge.yml create mode 100644 molecule/golang/molecule.yml create mode 100644 roles/common/tasks/main.yml delete mode 100644 roles/gitlab/tasks/go.yml create mode 100644 roles/golang/README.md create mode 100755 roles/golang/defaults/main.yml create mode 100755 roles/golang/meta/main.yml create mode 100755 roles/golang/tasks/main.yml create mode 100644 roles/golang/tasks/remove.yml create mode 100644 roles/golang/tasks/setup.yml create mode 100644 roles/golang/vars/main.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fbf7f0..6773961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.1 + +- add golang role + ## 1.0.0 - merge of existing roles to a collection diff --git a/README.md b/README.md index 7b96418..111f550 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ All provided roles do not use docker as container system. - [alphanodes.setup.gitlab](roles/gitlab/) - [alphanodes.setup.glances](roles/glances/) - [alphanodes.setup.goaccess](roles/goaccess/) +- [alphanodes.setup.golang](roles/golang/) - [alphanodes.setup.hedgedoc](roles/hedgedoc/) - [alphanodes.setup.java](roles/java/) - [alphanodes.setup.jekyll](roles/jekyll/) diff --git a/molecule/golang/converge.yml b/molecule/golang/converge.yml new file mode 100644 index 0000000..d79b00f --- /dev/null +++ b/molecule/golang/converge.yml @@ -0,0 +1,21 @@ +--- +- name: Converge + hosts: all + become: true + + vars: + golang_version: '1.20.11' + golang_sha256_checksum: 'ced1b5cfd4e8aa7aff5e6a399ed111e5ed2903ee17aaa79ac0c93565fb229776' + + roles: + - role: alphanodes.setup.golang + + post_tasks: + - name: Run go version + ansible.builtin.command: go version + changed_when: false + register: go_version_info + + - name: Show instaled go version + ansible.builtin.debug: + var: go_version_info.stdout diff --git a/molecule/golang/molecule.yml b/molecule/golang/molecule.yml new file mode 100644 index 0000000..75e5a3b --- /dev/null +++ b/molecule/golang/molecule.yml @@ -0,0 +1,23 @@ +--- +dependency: + name: galaxy +driver: + name: docker +platforms: + - name: instance + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + pre_build_image: true + cgroupns_mode: host +provisioner: + name: ansible + options: + diff: true + config_options: + defaults: + interpreter_python: auto_silent + playbooks: + converge: ${MOLECULE_PLAYBOOK:-converge.yml} diff --git a/roles/ansible_node/defaults/main.yml b/roles/ansible_node/defaults/main.yml index a77bcad..280292d 100644 --- a/roles/ansible_node/defaults/main.yml +++ b/roles/ansible_node/defaults/main.yml @@ -47,7 +47,7 @@ ansible_node_disallowed_packages: # fixed version or latest # use 7.2 or 7.2.1 for minor fixed version -ansible_node_ansible_version: 8.5 +ansible_node_ansible_version: 8.6 # required switching from 2.9 to newer version ansible_node_remove_before_install: false diff --git a/roles/common/defaults/main.yml b/roles/common/defaults/main.yml index ed5b072..0dea6ff 100644 --- a/roles/common/defaults/main.yml +++ b/roles/common/defaults/main.yml @@ -2,3 +2,9 @@ ansible_pip_venv: ansible python_venv_parent: "{{ ansible_env.HOME }}/.venvs" + +# Directory, which is used for downloading software packages +software_dir: /srv/software +# software_dir_owner: +# software_dir_group: "{{ software_dir_owner | omit }}" +# software_dir_mode: diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml new file mode 100644 index 0000000..75e7161 --- /dev/null +++ b/roles/common/tasks/main.yml @@ -0,0 +1,7 @@ +- name: Ensures software_dir exists + ansible.builtin.file: + path: '{{ software_dir }}' + owner: '{{ software_dir_owner | default(omit) }}' + group: '{{ software_dir_group | default(omit) }}' + mode: '{{ software_dir_mode | default(omit) }}' + state: directory diff --git a/roles/gitlab/defaults/main.yml b/roles/gitlab/defaults/main.yml index ebbdd32..2770bbf 100644 --- a/roles/gitlab/defaults/main.yml +++ b/roles/gitlab/defaults/main.yml @@ -170,7 +170,8 @@ gitlab_packages: # see https://docs.gitlab.com/ee/install/installation.html#3-go # NOTE: version should be tests in gitlab pipeline # SEE https://gitlab.com/gitlab-org/gitlab/-/blob/master/.gitlab/ci/workhorse.gitlab-ci.yml#L30 -gitlab_go_version: '1.20.10' +gitlab_go_version: '1.20.11' +gitlab_go_sha256_checksum: ef79a11aa095a08772d2a69e4f152f897c4e96ee297b0dc20264b7dec2961abe gitlab_nginx_auth_realm: 'Code access is restricted' gitlab_nginx_client_max_body_size: '0' diff --git a/roles/gitlab/tasks/go.yml b/roles/gitlab/tasks/go.yml deleted file mode 100644 index e8972cd..0000000 --- a/roles/gitlab/tasks/go.yml +++ /dev/null @@ -1,58 +0,0 @@ ---- - -- name: Set fact gitlab_go_file - ansible.builtin.set_fact: - gitlab_go_file: "go{{ gitlab_go_version }}.linux-amd64.tar.gz" - -- name: Check gitlab_go_file exists - ansible.builtin.stat: - path: '{{ software_dir }}/{{ gitlab_go_file }}' - register: gitlab_go_file_exists - -- name: Check gitlab_go_path exists - ansible.builtin.stat: - path: '{{ gitlab_go_path }}/bin/go' - register: gitlab_go_path_exists - -- name: Delete existing path for update - {{ gitlab_go_path }} - ansible.builtin.file: - path: '{{ gitlab_go_path }}' - state: absent - when: - - not gitlab_go_file_exists.stat.exists - - gitlab_go_path_exists.stat.exists - -- name: Creating {{ gitlab_go_path }} - ansible.builtin.file: - path: '{{ gitlab_go_path }}' - state: directory - mode: 0755 - owner: root - group: root - when: not gitlab_go_path_exists.stat.exists - -- name: Download gitlab_go_file archive version {{ gitlab_go_version }} - ansible.builtin.get_url: - url: https://dl.google.com/go/{{ gitlab_go_file }} - dest: '{{ software_dir }}/{{ gitlab_go_file }}' - mode: 0644 - when: not gitlab_go_file_exists.stat.exists - -- name: Extract gitlab_go_file archive - ansible.builtin.unarchive: - src: '{{ software_dir }}/{{ gitlab_go_file }}' - dest: /usr/local - owner: root - group: root - copy: false - when: not gitlab_go_path_exists.stat.exists or not gitlab_go_file_exists.stat.exists - -- name: Create symlinks to go binaries - ansible.builtin.file: - src: /usr/local/go/bin/{{ item }} - dest: /usr/local/bin/{{ item }} - force: true - state: link - loop: - - go - - gofmt diff --git a/roles/gitlab/tasks/setup.yml b/roles/gitlab/tasks/setup.yml index 0fb4769..7dd0ca7 100644 --- a/roles/gitlab/tasks/setup.yml +++ b/roles/gitlab/tasks/setup.yml @@ -124,8 +124,12 @@ register: gitlab_git_changed when: gitlab_update -- name: Include Go tasks - ansible.builtin.include_tasks: go.yml +- name: Include Go role + ansible.builtin.include_role: + name: alphanodes.setup.golang + vars: + golang_version: '{{ gitlab_go_version | default(omit) }}' + golang_sha256_checksum: '{{ gitlab_go_sha256_checksum | default(omit) }}' - name: Remove system yarnpkg is removed ansible.builtin.apt: diff --git a/roles/gitlab/templates/gitlab-shell/config.yml.j2 b/roles/gitlab/templates/gitlab-shell/config.yml.j2 index 915bb02..26d158e 100644 --- a/roles/gitlab/templates/gitlab-shell/config.yml.j2 +++ b/roles/gitlab/templates/gitlab-shell/config.yml.j2 @@ -35,7 +35,7 @@ http_settings: auth_file: "{{ gitlab_home }}/.ssh/authorized_keys" # SSL certificate dir where custom certificates can be placed -# https://golang.org/pkg/crypto/x509/ +# https://pkg.go.dev/crypto/x509 # ssl_cert_dir: /opt/gitlab/embedded/ssl/certs/ # File that contains the secret key for verifying access to GitLab. diff --git a/roles/golang/README.md b/roles/golang/README.md new file mode 100644 index 0000000..f98cc76 --- /dev/null +++ b/roles/golang/README.md @@ -0,0 +1,20 @@ +# Ansible Role: go + +An Ansible Role that installs Go language on Debian and Ubuntu servers. + +## Role Variables + +Available variables can be found in [defaults/main.yml](defaults/main.yml) + +## Example Playbook + +```yaml + - hosts: all + + vars: + golang_version: '1.20.10' + golang_sha256_checksum: d355c5ae3a8f7763c9ec9dc25153aae373958cbcb60dd09e91a8b56c7621b2fc + + roles: + - alphanodes.setup.go +``` diff --git a/roles/golang/defaults/main.yml b/roles/golang/defaults/main.yml new file mode 100755 index 0000000..3edccda --- /dev/null +++ b/roles/golang/defaults/main.yml @@ -0,0 +1,38 @@ +--- + +# Go language SDK version number +# use fixed version e.g. '1.20.11' or latest +golang_version: latest +golang_arch: amd64 + +# use it with fixed version +# golang_sha256_checksum: ef79a11aa095a08772d2a69e4f152f897c4e96ee297b0dc20264b7dec2961abe + +# Mirror to download the Go language SDK redistributable package from +golang_mirror: 'https://dl.google.com/go' + +# Base installation directory the Go language SDK distribution +golang_install_dir: '/usr/local/go' + +# create link to /usr/local/bin to go binaries +# NOTE: only if golang_dist_packages = false +golang_link_to_local: true + +golang_link_binaries: + - go + - gofmt + +# Location for GOPATH environment variable +golang_gopath: + +# if true, golang is installed with packages from linux distribution +# (not from golang_mirror) +golang_dist_packages: false + +# only used with golang_dist_packages = true +golang_packages: + - golang-go + - golang-src + +# set it to true, if you want to remove go +golang_remove: false diff --git a/roles/golang/meta/main.yml b/roles/golang/meta/main.yml new file mode 100755 index 0000000..6bb3497 --- /dev/null +++ b/roles/golang/meta/main.yml @@ -0,0 +1,22 @@ +--- + +dependencies: + - role: alphanodes.setup.common + public: true + +galaxy_info: + description: Install go language. + author: alexandermeindl + company: AlphaNodes GmbH + license: Apache License 2.0 + min_ansible_version: '2.14' + platforms: + - name: Ubuntu + versions: + - focal + - jammy + - name: Debian + versions: + - buster + - bullseye + - bookworm diff --git a/roles/golang/tasks/main.yml b/roles/golang/tasks/main.yml new file mode 100755 index 0000000..02b17db --- /dev/null +++ b/roles/golang/tasks/main.yml @@ -0,0 +1,13 @@ +--- + +- name: Include setup tasks + ansible.builtin.import_tasks: setup.yml + tags: + - golang + when: not golang_remove | bool + +- name: Include remove tasks + ansible.builtin.import_tasks: remove.yml + tags: + - golang + when: golang_remove | bool diff --git a/roles/golang/tasks/remove.yml b/roles/golang/tasks/remove.yml new file mode 100644 index 0000000..a816d21 --- /dev/null +++ b/roles/golang/tasks/remove.yml @@ -0,0 +1,23 @@ +--- + +- name: Remove for non-dist installation + when: not golang_dist_packages + block: + - name: Remove golang files and directories + ansible.builtin.file: + path: '{{ golang_install_dir }}' + state: absent + + - name: Remove links + ansible.builtin.file: + path: "/usr/local/bin/{{ item }}" + state: absent + when: golang_link_to_local + loop: '{{ golang_link_binaries }}' + +- name: Be sure debian packages are removed + ansible.builtin.apt: + name: '{{ golang_packages }}' + purge: true + state: absent + when: golang_dist_packages diff --git a/roles/golang/tasks/setup.yml b/roles/golang/tasks/setup.yml new file mode 100644 index 0000000..9ecae37 --- /dev/null +++ b/roles/golang/tasks/setup.yml @@ -0,0 +1,80 @@ +--- + +- name: Be sure debian packages are installed - {{ golang_latest_version_url }} + ansible.builtin.apt: + name: '{{ golang_packages }}' + state: present + when: golang_dist_packages + +- name: Install for non-dist installation + when: not golang_dist_packages + block: + - name: Set golang_real_version with latest version + ansible.builtin.set_fact: + golang_real_version: "{{ lookup('url', golang_latest_version_url, wantlist=True) | first }}" + when: golang_version == 'latest' + + - name: Show backup volkstheater + ansible.builtin.debug: + var: golang_real_version + + - name: Set golang_real_version with fixed version + ansible.builtin.set_fact: + golang_real_version: "go{{ golang_version }}" + when: golang_version != 'latest' + + - name: Check golang_file exists - {{ golang_real_version }} + ansible.builtin.stat: + path: '{{ software_dir }}/{{ golang_file }}' + register: golang_file_exists + + - name: Check Go exists + ansible.builtin.stat: + path: '{{ golang_install_dir }}/bin/go' + register: golang_install_dir_exists + + - name: Delete existing path for update - {{ golang_install_dir }} + ansible.builtin.file: + path: '{{ golang_install_dir }}' + state: absent + when: + - not golang_file_exists.stat.exists + - golang_install_dir_exists.stat.exists + + - name: Creating {{ golang_install_dir }} + ansible.builtin.file: + path: '{{ golang_install_dir }}' + state: directory + mode: 0755 + owner: root + group: root + + - name: Download golang_file archive version {{ golang_real_version }} + ansible.builtin.get_url: + url: '{{ golang_mirror }}/{{ golang_file }}' + dest: '{{ software_dir }}/{{ golang_file }}' + mode: 0644 + checksum: "{{ 'sha256:' + golang_sha256_checksum | default(omit) }}" + force: false + use_proxy: true + validate_certs: true + when: not golang_file_exists.stat.exists + + - name: Extract golang_file archive + ansible.builtin.unarchive: + src: '{{ software_dir }}/{{ golang_file }}' + dest: '{{ golang_install_dir }}' + extra_opts: '--strip-components=1' + owner: root + group: root + remote_src: true + when: not golang_install_dir_exists.stat.exists or not golang_file_exists.stat.exists + + - name: Create symlinks to go binaries + ansible.builtin.file: + src: '{{ golang_install_dir }}/bin/{{ item }}' + dest: /usr/local/bin/{{ item }} + force: true + state: link + when: golang_link_to_local + loop: '{{ golang_link_binaries }}' diff --git a/roles/golang/vars/main.yml b/roles/golang/vars/main.yml new file mode 100644 index 0000000..a1fc5c6 --- /dev/null +++ b/roles/golang/vars/main.yml @@ -0,0 +1,4 @@ +--- + +golang_file: "{{ golang_real_version }}.linux-{{ golang_arch }}.tar.gz" +golang_latest_version_url: https://go.dev/VERSION?m=text diff --git a/roles/matomo/meta/main.yml b/roles/matomo/meta/main.yml index 10ed907..31440c2 100644 --- a/roles/matomo/meta/main.yml +++ b/roles/matomo/meta/main.yml @@ -1,6 +1,8 @@ --- dependencies: + - role: alphanodes.setup.common + public: true - role: alphanodes.setup.ssl when: matomo_with_nginx - role: alphanodes.setup.mysql_client diff --git a/roles/nextcloud/meta/main.yml b/roles/nextcloud/meta/main.yml index 594227d..b8f1672 100644 --- a/roles/nextcloud/meta/main.yml +++ b/roles/nextcloud/meta/main.yml @@ -1,5 +1,7 @@ --- dependencies: + - role: alphanodes.setup.common + public: true - role: alphanodes.setup.ssl - role: alphanodes.setup.php_fpm - role: alphanodes.setup.redis_server diff --git a/roles/rocketchat/meta/main.yml b/roles/rocketchat/meta/main.yml index bf1633c..82cce9c 100644 --- a/roles/rocketchat/meta/main.yml +++ b/roles/rocketchat/meta/main.yml @@ -1,6 +1,7 @@ --- dependencies: - role: alphanodes.setup.common + public: true - role: alphanodes.setup.ssl - role: alphanodes.setup.nginx