diff --git a/roles/chrony_exporter/defaults/main.yml b/roles/chrony_exporter/defaults/main.yml index e67c8a74..bbe5d6bf 100644 --- a/roles/chrony_exporter/defaults/main.yml +++ b/roles/chrony_exporter/defaults/main.yml @@ -1,10 +1,8 @@ --- chrony_exporter_version: 0.10.1 -chrony_exporter_binary_local_dir: "" chrony_exporter_binary_url: "https://github.com/{{ _chrony_exporter_repo }}/releases/download/v{{ chrony_exporter_version }}/\ - chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}.tar.gz" + chrony_exporter-{{ chrony_exporter_version }}.{{ ansible_system | lower }}-{{ _chrony_exporter_go_ansible_arch }}.tar.gz" chrony_exporter_checksums_url: "https://github.com/{{ _chrony_exporter_repo }}/releases/download/v{{ chrony_exporter_version }}/sha256sums.txt" -chrony_exporter_skip_install: false chrony_exporter_web_listen_address: "0.0.0.0:9123" chrony_exporter_web_telemetry_path: "/metrics" @@ -21,9 +19,12 @@ chrony_exporter_enabled_collectors: [] chrony_exporter_disabled_collectors: [] -chrony_exporter_binary_install_dir: "/usr/local/bin" -chrony_exporter_system_group: "chrony-exp" -chrony_exporter_system_user: "{{ chrony_exporter_system_group }}" +chrony_exporter_system_user: "chrony-exp" +chrony_exporter_system_group: "{{ chrony_exporter_system_user }}" + # Local path to stash the archive and its extraction -chrony_exporter_archive_path: /tmp +chrony_exporter_local_cache_path: "/tmp/chrony_exporter-{{ ansible_system | lower }}-{{ _chrony_exporter_go_ansible_arch }}/{{ chrony_exporter_version }}" + +chrony_exporter_binary_install_dir: "/usr/local/bin" +chrony_exporter_config_dir: "/etc/chrony_exporter" diff --git a/roles/chrony_exporter/meta/argument_specs.yml b/roles/chrony_exporter/meta/argument_specs.yml index 5e57c921..8bace65e 100644 --- a/roles/chrony_exporter/meta/argument_specs.yml +++ b/roles/chrony_exporter/meta/argument_specs.yml @@ -11,18 +11,9 @@ argument_specs: chrony_exporter_version: description: "Chrony exporter package version. Also accepts latest as parameter." default: "0.10.1" - chrony_exporter_skip_install: - description: "Chrony exporter installation tasks gets skipped when set to true." - type: bool - default: false - chrony_exporter_binary_local_dir: - description: - - "Enables the use of local packages instead of those distributed on github." - - "The parameter may be set to a directory where the C(chrony_exporter) binary is stored on the host where ansible is run." - - "This overrides the I(chrony_exporter_version) parameter" chrony_exporter_binary_url: description: "URL of the chrony_exporter binaries .tar.gz file" - default: "https://github.com/{{ _chrony_exporter_repo }}/releases/download/v{{ chrony_exporter_version }}/chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}.tar.gz" + default: "https://github.com/{{ _chrony_exporter_repo }}/releases/download/v{{ chrony_exporter_version }}/chrony_exporter-{{ chrony_exporter_version }}.{{ ansible_system | lower }}-{{ _chrony_exporter_go_ansible_arch }}.tar.gz" chrony_exporter_checksums_url: description: "URL of the chrony_exporter checksums file" default: "https://github.com/{{ _chrony_exporter_repo }}/releases/download/v{{ chrony_exporter_version }}/sha256sums.txt" @@ -58,11 +49,6 @@ argument_specs: chrony_exporter_basic_auth_users: description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt." type: "dict" - chrony_exporter_binary_install_dir: - description: - - "I(Advanced)" - - "Directory to install chrony_exporter binary" - default: "/usr/local/bin" chrony_exporter_system_group: description: - "I(Advanced)" @@ -73,6 +59,14 @@ argument_specs: - "I(Advanced)" - "Chrony exporter user" default: "chrony-exp" - chrony_exporter_archive_path: + chrony_exporter_binary_install_dir: + description: + - "I(Advanced)" + - "Directory to install binaries" + default: "/usr/local/bin" + chrony_exporter_local_cache_path: description: 'Local path to stash the archive and its extraction' - default: "/tmp" + default: "/tmp/chrony_exporter-{{ ansible_system | lower }}-{{ _chrony_exporter_go_ansible_arch }}/{{ chrony_exporter_version }}" + chrony_exporter_config_dir: + description: "Path to directory with chrony_exporter configuration" + default: "/etc/chrony_exporter" diff --git a/roles/chrony_exporter/molecule/alternative/molecule.yml b/roles/chrony_exporter/molecule/alternative/molecule.yml index 6e55e96e..d0944c50 100644 --- a/roles/chrony_exporter/molecule/alternative/molecule.yml +++ b/roles/chrony_exporter/molecule/alternative/molecule.yml @@ -5,7 +5,8 @@ provisioner: inventory: group_vars: all: - chrony_exporter_binary_local_dir: "/tmp/chrony_exporter-linux-amd64" + chrony_exporter_version: 0.6.0 + chrony_exporter_local_cache_path: "/tmp/chrony_exporter-linux-amd64/{{ chrony_exporter_version }}" chrony_exporter_web_listen_address: - '127.0.0.1:8080' - '127.0.1.1:8080' @@ -20,7 +21,5 @@ provisioner: http2: true chrony_exporter_basic_auth_users: randomuser: examplepassword - go_arch: amd64 - chrony_exporter_version: 0.6.0 chrony_exporter_binary_url: "https://github.com/superq/chrony_exporter/releases/download/v{{ chrony_exporter_version\ - \ }}/chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}.tar.gz" + \ }}/chrony_exporter-{{ chrony_exporter_version }}.linux-amd64.tar.gz" diff --git a/roles/chrony_exporter/molecule/default/tests/test_default.py b/roles/chrony_exporter/molecule/default/tests/test_default.py index f3d272a1..1979c44b 100644 --- a/roles/chrony_exporter/molecule/default/tests/test_default.py +++ b/roles/chrony_exporter/molecule/default/tests/test_default.py @@ -46,7 +46,6 @@ def test_user(host): assert host.group("chrony-exp").exists assert "chrony-exp" in host.user("chrony-exp").groups assert host.user("chrony-exp").shell == "/usr/sbin/nologin" - assert host.user("chrony-exp").home == "/" def test_service(host): diff --git a/roles/chrony_exporter/tasks/configure.yml b/roles/chrony_exporter/tasks/configure.yml deleted file mode 100644 index 517e624c..00000000 --- a/roles/chrony_exporter/tasks/configure.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Copy the chrony_exporter systemd service file - ansible.builtin.template: - src: chrony_exporter.service.j2 - dest: /etc/systemd/system/chrony_exporter.service - owner: root - group: root - mode: 0644 - notify: restart chrony_exporter - -- name: Create chrony_exporter config directory - ansible.builtin.file: - path: "/etc/chrony_exporter" - state: directory - owner: root - group: root - mode: u+rwX,g+rwX,o=rX - -- name: Copy the chrony_exporter web config file - ansible.builtin.template: - src: web_config.yaml.j2 - dest: /etc/chrony_exporter/web_config.yaml - owner: root - group: root - mode: 0644 - notify: restart chrony_exporter - -- name: Allow chrony_exporter port in SELinux on RedHat OS family - community.general.seport: - ports: "{{ chrony_exporter_web_listen_address.split(':')[-1] }}" - proto: tcp - setype: http_port_t - state: present - when: - - ansible_version.full is version_compare('2.4', '>=') - - ansible_selinux.status == "enabled" diff --git a/roles/chrony_exporter/tasks/install.yml b/roles/chrony_exporter/tasks/install.yml deleted file mode 100644 index 347def86..00000000 --- a/roles/chrony_exporter/tasks/install.yml +++ /dev/null @@ -1,69 +0,0 @@ ---- -- name: Create the chrony_exporter group - ansible.builtin.group: - name: "{{ chrony_exporter_system_group }}" - state: present - system: true - when: chrony_exporter_system_group != "root" - -- name: Create the chrony_exporter user - ansible.builtin.user: - name: "{{ chrony_exporter_system_user }}" - groups: "{{ chrony_exporter_system_group }}" - append: true - shell: /usr/sbin/nologin - system: true - create_home: false - home: / - when: chrony_exporter_system_user != "root" - -- name: Get binary - when: - - chrony_exporter_binary_local_dir | length == 0 - - not chrony_exporter_skip_install - block: - - - name: Download chrony_exporter binary to local folder - become: false - ansible.builtin.get_url: - url: "{{ chrony_exporter_binary_url }}" - dest: "{{ chrony_exporter_archive_path }}/chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}.tar.gz" - checksum: "sha256:{{ __chrony_exporter_checksum }}" - mode: '0644' - register: _download_binary - until: _download_binary is succeeded - retries: 5 - delay: 2 - delegate_to: localhost - check_mode: false - - - name: Unpack chrony_exporter binary - become: false - ansible.builtin.unarchive: - src: "{{ chrony_exporter_archive_path }}/chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}.tar.gz" - dest: "{{ chrony_exporter_archive_path }}" - creates: "{{ chrony_exporter_archive_path }}/chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}/chrony_exporter" - delegate_to: localhost - check_mode: false - - - name: Propagate chrony_exporter binaries - ansible.builtin.copy: - src: "{{ chrony_exporter_archive_path }}/chrony_exporter-{{ chrony_exporter_version }}.linux-{{ go_arch }}/chrony_exporter" - dest: "{{ chrony_exporter_binary_install_dir }}/chrony_exporter" - mode: 0755 - owner: root - group: root - notify: restart chrony_exporter - when: not ansible_check_mode - -- name: Propagate locally distributed chrony_exporter binary - ansible.builtin.copy: - src: "{{ chrony_exporter_binary_local_dir }}/chrony_exporter" - dest: "{{ chrony_exporter_binary_install_dir }}/chrony_exporter" - mode: 0755 - owner: root - group: root - when: - - chrony_exporter_binary_local_dir | length > 0 - - not chrony_exporter_skip_install - notify: restart chrony_exporter diff --git a/roles/chrony_exporter/tasks/main.yml b/roles/chrony_exporter/tasks/main.yml index b382e24e..e2d8bd57 100644 --- a/roles/chrony_exporter/tasks/main.yml +++ b/roles/chrony_exporter/tasks/main.yml @@ -2,51 +2,49 @@ - name: Preflight ansible.builtin.include_tasks: file: preflight.yml - apply: - tags: - - chrony_exporter_install - - chrony_exporter_configure - - chrony_exporter_run tags: - chrony_exporter_install - chrony_exporter_configure - chrony_exporter_run - name: Install - ansible.builtin.include_tasks: - file: install.yml - apply: - become: true - tags: - - chrony_exporter_install - when: - ( not __chrony_exporter_is_installed.stat.exists ) or - ( (__chrony_exporter_current_version_output.stderr_lines | length > 0) - and (__chrony_exporter_current_version_output.stderr_lines[0].split(" ")[2] != chrony_exporter_version) ) or - ( (__chrony_exporter_current_version_output.stdout_lines | length > 0) - and (__chrony_exporter_current_version_output.stdout_lines[0].split(" ")[2] != chrony_exporter_version) ) or - ( chrony_exporter_binary_local_dir | length > 0 ) + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: install.yml + vars: + _common_local_cache_path: "{{ chrony_exporter_local_cache_path }}" + _common_binaries: "{{ _chrony_exporter_binaries }}" + _common_binary_install_dir: "{{ chrony_exporter_binary_install_dir }}" + _common_binary_url: "{{ chrony_exporter_binary_url }}" + _common_checksums_url: "{{ chrony_exporter_checksums_url }}" + _common_system_group: "{{ chrony_exporter_system_group }}" + _common_system_user: "{{ chrony_exporter_system_user }}" + _common_config_dir: "{{ chrony_exporter_config_dir }}" + _common_binary_unarchive_opts: ['--strip-components=1'] tags: - chrony_exporter_install - name: SELinux - ansible.builtin.include_tasks: - file: selinux.yml - apply: - become: true - tags: - - chrony_exporter_configure + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: selinux.yml + vars: + _common_selinux_port: "{{ chrony_exporter_web_listen_address | urlsplit('port') }}" when: ansible_selinux.status == "enabled" tags: - chrony_exporter_configure - name: Configure - ansible.builtin.include_tasks: - file: configure.yml - apply: - become: true - tags: - - chrony_exporter_configure + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: configure.yml + vars: + _common_system_user: "{{ chrony_exporter_system_user }}" + _common_system_group: "{{ chrony_exporter_system_group }}" + _common_config_dir: "{{ chrony_exporter_config_dir }}" + _common_tls_server_config: "{{ chrony_exporter_tls_server_config }}" + _common_http_server_config: "{{ chrony_exporter_http_server_config }}" + _common_basic_auth_users: "{{ chrony_exporter_basic_auth_users }}" tags: - chrony_exporter_configure diff --git a/roles/chrony_exporter/tasks/preflight.yml b/roles/chrony_exporter/tasks/preflight.yml index 2d0d93d5..77057bd6 100644 --- a/roles/chrony_exporter/tasks/preflight.yml +++ b/roles/chrony_exporter/tasks/preflight.yml @@ -1,24 +1,8 @@ --- -- name: Assert usage of systemd as an init system - ansible.builtin.assert: - that: ansible_service_mgr == 'systemd' - msg: "This role only works with systemd" - -- name: Install package fact dependencies - become: true - ansible.builtin.package: - name: "{{ _pkg_fact_req }}" - state: present - when: (_pkg_fact_req) - vars: - _pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\ - {{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }} - {% else %}\ - {% endif %}" - -- name: Gather package facts - ansible.builtin.package_facts: - when: "not 'packages' in ansible_facts" +- name: Common preflight + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: preflight.yml - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -72,23 +56,6 @@ - "__chrony_exporter_cert_file.stat.exists" - "__chrony_exporter_key_file.stat.exists" -- name: Check if chrony_exporter is installed - ansible.builtin.stat: - path: "{{ chrony_exporter_binary_install_dir }}/chrony_exporter" - register: __chrony_exporter_is_installed - check_mode: false - tags: - - chrony_exporter_install - -- name: Gather currently installed chrony_exporter version (if any) - ansible.builtin.command: "{{ chrony_exporter_binary_install_dir }}/chrony_exporter --version" - changed_when: false - register: __chrony_exporter_current_version_output - check_mode: false - when: __chrony_exporter_is_installed.stat.exists - tags: - - chrony_exporter_install - - name: Discover latest version ansible.builtin.set_fact: chrony_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _chrony_exporter_repo }}/releases/latest', headers=_github_api_headers, @@ -98,24 +65,9 @@ retries: 10 when: - chrony_exporter_version == "latest" - - chrony_exporter_binary_local_dir | length == 0 - - not chrony_exporter_skip_install - -- name: Get chrony_exporter binary checksum - when: - - chrony_exporter_binary_local_dir | length == 0 - - not chrony_exporter_skip_install - block: - - name: Get checksum list from github - ansible.builtin.set_fact: - __chrony_exporter_checksums: "{{ lookup('url', chrony_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}" - run_once: true - until: __chrony_exporter_checksums is search('linux-' + go_arch + '.tar.gz') - retries: 10 - - - name: "Get checksum for {{ go_arch }}" - ansible.builtin.set_fact: - __chrony_exporter_checksum: "{{ item.split(' ')[0] }}" - with_items: "{{ __chrony_exporter_checksums }}" - when: - - "('linux-' + go_arch + '.tar.gz') in item" + tags: + - chrony_exporter + - install + - chrony_exporter_install + - download + - chrony_exporter_download diff --git a/roles/chrony_exporter/tasks/selinux.yml b/roles/chrony_exporter/tasks/selinux.yml deleted file mode 100644 index 754cbd0a..00000000 --- a/roles/chrony_exporter/tasks/selinux.yml +++ /dev/null @@ -1,23 +0,0 @@ ---- -- name: Install selinux python packages [RedHat] - ansible.builtin.package: - name: "{{ ['libselinux-python', 'policycoreutils-python'] - if ansible_python_version is version('3', '<') else - ['python3-libselinux', 'python3-policycoreutils'] }}" - state: present - register: _install_selinux_packages - until: _install_selinux_packages is success - retries: 5 - delay: 2 - when: ansible_os_family | lower == "redhat" - -- name: Install selinux python packages [clearlinux] - ansible.builtin.package: - name: sysadmin-basic - state: present - register: _install_selinux_packages - until: _install_selinux_packages is success - retries: 5 - delay: 2 - when: - - ansible_distribution | lower == "clearlinux" diff --git a/roles/chrony_exporter/templates/chrony_exporter.service.j2 b/roles/chrony_exporter/templates/chrony_exporter.service.j2 index 4e3ac352..d2fea9b5 100644 --- a/roles/chrony_exporter/templates/chrony_exporter.service.j2 +++ b/roles/chrony_exporter/templates/chrony_exporter.service.j2 @@ -24,7 +24,7 @@ ExecStart={{ chrony_exporter_binary_install_dir }}/chrony_exporter \ '--no-collector.{{ collector }}' \ {% endfor %} {% if chrony_exporter_tls_server_config | length > 0 or chrony_exporter_http_server_config | length > 0 or chrony_exporter_basic_auth_users | length > 0 %} - '--web.config.file=/etc/chrony_exporter/web_config.yaml' \ + '--web.config.file={{ chrony_exporter_config_dir }}/web_config.yml' \ {% endif %} {% if chrony_exporter_version is version('0.5.0', '>=') and chrony_exporter_web_listen_address is iterable and diff --git a/roles/chrony_exporter/templates/web_config.yaml.j2 b/roles/chrony_exporter/templates/web_config.yaml.j2 deleted file mode 100644 index 85874bb4..00000000 --- a/roles/chrony_exporter/templates/web_config.yaml.j2 +++ /dev/null @@ -1,18 +0,0 @@ ---- -{{ ansible_managed | comment }} -{% if chrony_exporter_tls_server_config | length > 0 %} -tls_server_config: -{{ chrony_exporter_tls_server_config | to_nice_yaml | indent(2, true) }} -{% endif %} - -{% if chrony_exporter_http_server_config | length > 0 %} -http_server_config: -{{ chrony_exporter_http_server_config | to_nice_yaml | indent(2, true) }} -{% endif %} - -{% if chrony_exporter_basic_auth_users | length > 0 %} -basic_auth_users: -{% for k, v in chrony_exporter_basic_auth_users.items() %} - {{ k }}: {{ v | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }} -{% endfor %} -{% endif %} diff --git a/roles/chrony_exporter/vars/main.yml b/roles/chrony_exporter/vars/main.yml index de3755ed..0314b49e 100644 --- a/roles/chrony_exporter/vars/main.yml +++ b/roles/chrony_exporter/vars/main.yml @@ -1,11 +1,10 @@ --- -go_arch_map: - i386: '386' - x86_64: 'amd64' - aarch64: 'arm64' - armv7l: 'armv7' - armv6l: 'armv6' - -go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}" +_chrony_exporter_go_ansible_arch: "{{ {'i386': '386', + 'x86_64': 'amd64', + 'aarch64': 'arm64', + 'armv7l': 'armv7', + 'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}" _chrony_exporter_repo: "superq/chrony_exporter" _github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" +chrony_exporter_binary_install_dir: "/usr/local/bin" +_chrony_exporter_binaries: ['chrony_exporter']