From 039dc99782d31a88693b83cbe5a4a12c7acfc2ef Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Tue, 4 Nov 2025 13:55:58 +0200 Subject: [PATCH] ansible: use go role in rpm playbook --- data/ansible/rpm_packages/cgrates.spec.j2 | 10 +- data/ansible/rpm_packages/go.yaml | 122 ---------------------- data/ansible/rpm_packages/golang.sh.j2 | 12 --- data/ansible/rpm_packages/main.yaml | 100 +++++++++++------- data/ansible/rpm_packages/nginx.yaml | 1 + 5 files changed, 67 insertions(+), 178 deletions(-) delete mode 100644 data/ansible/rpm_packages/go.yaml delete mode 100644 data/ansible/rpm_packages/golang.sh.j2 diff --git a/data/ansible/rpm_packages/cgrates.spec.j2 b/data/ansible/rpm_packages/cgrates.spec.j2 index 7af02d1b7..802e46054 100644 --- a/data/ansible/rpm_packages/cgrates.spec.j2 +++ b/data/ansible/rpm_packages/cgrates.spec.j2 @@ -85,11 +85,11 @@ cp -rpf {{ cgrates_dir }}/data/storage/mongo $RPM_BUILD_ROOT%{_datarootdir}/%{na cp -rpf {{ cgrates_dir }}/data/storage/mysql $RPM_BUILD_ROOT%{_datarootdir}/%{name}/storage cp -rpf {{ cgrates_dir }}/data/storage/postgres $RPM_BUILD_ROOT%{_datarootdir}/%{name}/storage install -D -m 0644 -p {{ cgrates_dir }}/data/conf/%{name}/%{name}.json $RPM_BUILD_ROOT%{_sysconfdir}/%{name}/%{name}.json -install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-console $RPM_BUILD_ROOT%{_bindir}/cgr-console -install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-engine $RPM_BUILD_ROOT%{_bindir}/cgr-engine -install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-loader $RPM_BUILD_ROOT%{_bindir}/cgr-loader -install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-tester $RPM_BUILD_ROOT%{_bindir}/cgr-tester -install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-migrator $RPM_BUILD_ROOT%{_bindir}/cgr-migrator +install -D -m 0755 -p /home/{{ user }}/go/bin/cgr-console $RPM_BUILD_ROOT%{_bindir}/cgr-console +install -D -m 0755 -p /home/{{ user }}/go/bin/cgr-engine $RPM_BUILD_ROOT%{_bindir}/cgr-engine +install -D -m 0755 -p /home/{{ user }}/go/bin/cgr-loader $RPM_BUILD_ROOT%{_bindir}/cgr-loader +install -D -m 0755 -p /home/{{ user }}/go/bin/cgr-tester $RPM_BUILD_ROOT%{_bindir}/cgr-tester +install -D -m 0755 -p /home/{{ user }}/go/bin/cgr-migrator $RPM_BUILD_ROOT%{_bindir}/cgr-migrator mkdir -p $RPM_BUILD_ROOT%{_logdir}/cdre/csv mkdir -p $RPM_BUILD_ROOT%{_logdir}/cdre/fwv mkdir -p $RPM_BUILD_ROOT%{_spooldir}/cdre/csv diff --git a/data/ansible/rpm_packages/go.yaml b/data/ansible/rpm_packages/go.yaml deleted file mode 100644 index c08fae40b..000000000 --- a/data/ansible/rpm_packages/go.yaml +++ /dev/null @@ -1,122 +0,0 @@ ---- -- name: create gopath directory - file: - state: directory - mode: 'u=rwx,go=rx' - dest: '{{ golang_gopath }}' - -- name: create download directory - file: - state: directory - mode: 'u=rwx,go=rx' - dest: '{{ golang_download_dir }}' - -- name: Register the current Go version (if any) - command: /usr/local/go/bin/go version - ignore_errors: yes - register: go_version - changed_when: false - -- name: Remove old installation of Go - become: yes - file: - path: /usr/local/go - state: absent - when: go_version is failed or go_version.stdout != go_version_target - -- name: download Go language SDK - get_url: - url: '{{ golang_mirror }}/{{ golang_redis_filename }}' - dest: '{{ golang_download_dir }}/{{ golang_redis_filename }}' - mode: 'u=rw,go=r' - -- name: create Go language SDK installation directory - become: yes - file: - state: directory - owner: root - group: root - mode: 'u=rwx,go=rx' - dest: '{{ golang_install_dir }}' - -- name: install Go language SDK - become: yes - unarchive: - src: '{{ golang_download_dir }}/{{ golang_redis_filename }}' - remote_src: yes - extra_opts: '--strip-components=1' - dest: '{{ golang_install_dir }}' - owner: root - group: root - creates: '{{ golang_install_dir }}/bin' - -# Set Go language SDK environment variables -- name: make sure /etc/profile.d exists - become: yes - file: - path: /etc/profile.d - state: directory - owner: root - group: root - mode: 'u=rwx,go=rx' - -- name: export Go language SDK environment variables - become: yes - template: - src: golang.sh.j2 - dest: /etc/profile.d/golang.sh - owner: root - group: root - mode: 'u=rw,go=r' - -- name: Export GOROOT for root - become: yes - lineinfile: - path: ~/.bashrc - line: export GOROOT='{{ golang_install_dir }}' - insertafter: last - -- name: Add GOROOT to PATH for root - become: yes - lineinfile: - dest: ~/.bashrc - line: export PATH=$PATH:$GOROOT/bin - insertafter: last - -- name: Export GOPATH for root - become: yes - lineinfile: - dest: ~/.bashrc - line: export GOPATH='{{ golang_gopath }}' - insertafter: last - -- name: Add GOPATH to PATH for root - become: yes - lineinfile: - dest: ~/.bashrc - line: export PATH=$PATH:$GOPATH/bin - insertafter: last - -- name: Export GOROOT - lineinfile: - dest: ~/.bashrc - line: export GOROOT='{{ golang_install_dir }}' - insertafter: last - -- name: Add GOROOT to PATH - lineinfile: - dest: ~/.bashrc - line: export PATH=$PATH:$GOROOT/bin - insertafter: last - -- name: Export GOPATH - lineinfile: - dest: ~/.bashrc - line: export GOPATH='{{ golang_gopath }}' - insertafter: last - -- name: Add GOPATH to PATH - lineinfile: - dest: ~/.bashrc - line: export PATH=$PATH:$GOPATH/bin - insertafter: last diff --git a/data/ansible/rpm_packages/golang.sh.j2 b/data/ansible/rpm_packages/golang.sh.j2 deleted file mode 100644 index 7bfc41c2e..000000000 --- a/data/ansible/rpm_packages/golang.sh.j2 +++ /dev/null @@ -1,12 +0,0 @@ - -#!/bin/sh - -{{ ansible_managed | comment('plain') }} - -export GOROOT='{{ golang_install_dir }}' -export PATH=$PATH:$GOROOT/bin - -{% if golang_gopath not in (None, '') %} -export GOPATH="{{ golang_gopath }}" -export PATH=$PATH:$GOPATH/bin -{% endif %} \ No newline at end of file diff --git a/data/ansible/rpm_packages/main.yaml b/data/ansible/rpm_packages/main.yaml index 332c53a93..a63dcf1c0 100644 --- a/data/ansible/rpm_packages/main.yaml +++ b/data/ansible/rpm_packages/main.yaml @@ -1,25 +1,54 @@ --- +# Before running this script you need the following: +# - 2 RockyLinux 9 minimal install servers with static ips defined +# in order to configure a static ip: +# 1. type nmtui in console +# 2. Edit a connection +# 3. Choose the interface (enp0s8 in my case) and select Edit... +# 4. Go to IPv4 CONFIGURATION and set from Automatic to Manual then select Show +# 5. Type the desired address alongside Addresses field then click OK then click Back +# 6. Go to Activate a connection +# 7. Deactivate the enp0s8 connection and activate it again to apply the changes +# - after the user creation below, to set up a passwordless ssh connection from the yum host created user to the pkg host created user(otherwise a yes/no prompt will be encountered +# and disrupt the script) + +- hosts: pkg, yum + vars: + ansible_user: root + tasks: + - name: Make sure we have a 'admin' group + group: + name: admin + state: present + + - name: Allow 'admin' group to have passwordless sudo + lineinfile: + dest: /etc/sudoers + state: present + regexp: '^%admin' + line: '%admin ALL=(ALL) NOPASSWD: ALL' + validate: 'visudo -cf %s' + + - name: Add sudoers users to admin group + user: + name={{ user }} + groups=admin + append=yes + state=present + createhome=yes + + - name: get the username running the deploy + become: false + local_action: command whoami + register: host_username + + - name: Set up authorized keys for the {{ user }} user + authorized_key: user={{ user }} key="{{ item }}" + with_file: + - '/home/{{ host_username.stdout }}/.ssh/id_rsa.pub' + - hosts: yum vars: - ansible_python_interpreter: auto # to disable deprication warning related to the use of python2 - ############################################################### - ##################### Golang Vars ############################# - ############################################################### - # Go language SDK version number - golang_version: '1.24' - go_version_target: "go version go{{ golang_version }} linux/amd64" - # Mirror to download the Go language SDK redistributable package from - golang_mirror: 'https://storage.googleapis.com/golang' - # Base installation directory the Go language SDK distribution - golang_install_dir: '/usr/local/go' - # Directory to store files downloaded for Go language SDK installation - golang_download_dir: "{{ x_ansible_download_dir | default(ansible_env.HOME + '/.ansible/tmp/downloads') }}" - # Location for GOPATH environment variable - golang_gopath: "/home/{{ user }}/go" - # Filename of Go language SDK redistributable package - golang_redis_filename: 'go{{ golang_version }}.linux-amd64.tar.gz' - - ############################################################### # CGRateS location cgrates_dir: "{{ golang_gopath }}/src/github.com/cgrates/cgrates" cgrates_branch: "1.0" @@ -38,9 +67,9 @@ rootUser : root - customPath: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + customPath: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" - remote_user: '{{ user }}' + ansible_user: '{{ user }}' tasks: - name: set cgrates cgrates_distribution set_fact: @@ -109,6 +138,7 @@ when: nginxConfig.stdout_lines|length < 1 - name: Enable and start nginx + become: true systemd: name: nginx state: started @@ -131,18 +161,10 @@ ########################################################################################################################### ########################################################################################################################### -# Install Golang - - name: install unarchive dependencies (zypper) - become: yes - zypper: - name: - - gzip - - tar - state: present - when: ansible_pkg_mgr == 'zypper' - - - name: Install golang - include: go.yaml +# Install Go +- name: Install Go + import_role: + name: ../roles/go ########################################################################################################################### ########################################################################################################################### @@ -171,26 +193,26 @@ become: yes shell: "go clean --cache" environment: - PATH: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" - name: clean go modcache become: yes shell: "go clean --modcache" environment: - PATH: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" - name: sync the go mod with vendor become: yes shell: "go mod vendor" environment: - PATH: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" args: chdir: '{{ cgrates_dir }}' - name: build cgrates shell: "sh {{ cgrates_dir }}/build.sh" environment: - PATH: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" args: chdir: '{{ cgrates_dir }}' @@ -257,7 +279,7 @@ become: yes shell: 'rpmbuild -bb --define "_topdir /home/{{ user }}/cgr_build" cgr_build/SPECS/cgrates.spec' environment: - PATH: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" - name: Take the name of the .rpm package become: yes @@ -317,7 +339,7 @@ - hosts: pkg vars: ansible_python_interpreter: auto # to disable deprication warning related to the use of python2 - remote_user: '{{ user }}' + ansible_user: '{{ user }}' tasks: - name: set cgrates cgrates_distribution set_fact: diff --git a/data/ansible/rpm_packages/nginx.yaml b/data/ansible/rpm_packages/nginx.yaml index fe9a828bf..e0a3e1a06 100644 --- a/data/ansible/rpm_packages/nginx.yaml +++ b/data/ansible/rpm_packages/nginx.yaml @@ -20,6 +20,7 @@ shell: "sudo firewall-cmd --reload" - name: Restart the nginx so the change take effects + become: true systemd: state: restarted name: nginx