refactor(cadvisor): delegate common tasks to _common role

Signed-off-by: gardar <gardar@users.noreply.github.com>
This commit is contained in:
gardar 2024-10-15 17:03:59 +00:00
parent bf1d349d02
commit 3259947a89
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
8 changed files with 48 additions and 199 deletions

View file

@ -1,9 +1,7 @@
---
cadvisor_version: 0.49.1
cadvisor_binary_local_dir: ""
cadvisor_binary_url: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/\
cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}"
cadvisor_skip_install: false
cadvisor-v{{ cadvisor_version }}-{{ ansible_system | lower }}-{{ _cadvisor_go_ansible_arch }}"
cadvisor_listen_ip: "0.0.0.0"
cadvisor_port: "8080"
@ -16,9 +14,10 @@ cadvisor_whitelisted_container_labels: []
cadvisor_store_container_labels: true
cadvisor_docker_only: false
cadvisor_binary_install_dir: "/usr/local/bin"
cadvisor_system_group: "root"
cadvisor_system_user: "{{ cadvisor_system_group }}"
# Local path to stash the archive and its extraction
cadvisor_archive_path: /tmp
cadvisor_local_cache_path: "/tmp/cadvisor-{{ ansible_system | lower }}-{{ _cadvisor_go_ansible_arch }}/{{ cadvisor_version }}"
cadvisor_binary_install_dir: "/usr/local/bin"

View file

@ -11,18 +11,9 @@ argument_specs:
cadvisor_version:
description: "cAdvisor package version. Also accepts latest as parameter."
default: "0.49.1"
cadvisor_skip_install:
description: "cAdvisor installation tasks gets skipped when set to true."
type: bool
default: false
cadvisor_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(cadvisor) binary is stored on the host where ansible is run."
- "This overrides the I(cadvisor_version) parameter"
cadvisor_binary_url:
description: "URL of the cadvisor binary file"
default: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/cadvisor-{{ cadvisor_version }}-linux-{{ go_arch }}"
default: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/cadvisor-v{{ cadvisor_version }}-{{ ansible_system | lower }}-{{ _cadvisor_go_ansible_arch }}"
cadvisor_listen_ip:
description: "Address on which cadvisor will listen"
default: "0.0.0.0"
@ -80,11 +71,6 @@ argument_specs:
description: "do not report raw cgroup metrics, except the root cgroup"
type: "bool"
default: false
cadvisor_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install cadvisor binary"
default: "/usr/local/bin"
cadvisor_system_group:
description:
- "I(Advanced)"
@ -95,6 +81,11 @@ argument_specs:
- "I(Advanced)"
- "cAdvisor user"
default: "root"
cadvisor_archive_path:
cadvisor_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install binaries"
default: "/usr/local/bin"
cadvisor_local_cache_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"
default: "/tmp/cadvisor-{{ ansible_system | lower }}-{{ _cadvisor_go_ansible_arch }}/{{ cadvisor_version }}"

View file

@ -1,19 +0,0 @@
---
- name: Copy the cadvisor systemd service file
ansible.builtin.template:
src: cadvisor.service.j2
dest: /etc/systemd/system/cadvisor.service
owner: root
group: root
mode: 0644
notify: restart cadvisor
- name: Allow cadvisor port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ cadvisor_port }}"
proto: tcp
setype: http_port_t
state: present
when:
- ansible_version.full is version_compare('2.4', '>=')
- ansible_selinux.status == "enabled"

View file

@ -1,59 +0,0 @@
---
- name: Create the cadvisor group
ansible.builtin.group:
name: "{{ cadvisor_system_group }}"
state: present
system: true
when: cadvisor_system_group != "root"
- name: Create the cadvisor user
ansible.builtin.user:
name: "{{ cadvisor_system_user }}"
groups: "{{ cadvisor_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: cadvisor_system_user != "root"
- name: Get binary
when:
- cadvisor_binary_local_dir | length == 0
- not cadvisor_skip_install
block:
- name: Download cadvisor binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ cadvisor_binary_url }}"
dest: "{{ cadvisor_archive_path }}/cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Propagate cadvisor binaries
ansible.builtin.copy:
src: "{{ cadvisor_archive_path }}/cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}"
dest: "{{ cadvisor_binary_install_dir }}/cadvisor"
mode: 0755
owner: root
group: root
notify: restart cadvisor
when: not ansible_check_mode
- name: Propagate locally distributed cadvisor binary
ansible.builtin.copy:
src: "{{ cadvisor_binary_local_dir }}/cadvisor"
dest: "{{ cadvisor_binary_install_dir }}/cadvisor"
mode: 0755
owner: root
group: root
when:
- cadvisor_binary_local_dir | length > 0
- not cadvisor_skip_install
notify: restart cadvisor

View file

@ -2,51 +2,41 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- cadvisor_install
- cadvisor_configure
- cadvisor_run
tags:
- cadvisor_install
- cadvisor_configure
- cadvisor_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- cadvisor_install
when:
( not __cadvisor_is_installed.stat.exists ) or
( (__cadvisor_current_version_output.stderr_lines | length > 0)
and ((__cadvisor_current_version_output.stderr_lines[0].split(" ")[2] | replace('v', '')) != cadvisor_version) ) or
( (__cadvisor_current_version_output.stdout_lines | length > 0)
and ((__cadvisor_current_version_output.stdout_lines[0].split(" ")[2] | replace('v', '')) != cadvisor_version) ) or
( cadvisor_binary_local_dir | length > 0 )
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ cadvisor_local_cache_path }}"
_common_binary_name: "{{ _cadvisor_binaries | first }}"
_common_binaries: "{{ _cadvisor_binaries }}"
_common_binary_install_dir: "{{ cadvisor_binary_install_dir }}"
_common_binary_url: "{{ cadvisor_binary_url }}"
_common_system_group: "{{ cadvisor_system_group }}"
_common_system_user: "{{ cadvisor_system_user }}"
_common_config_dir: "{{ cadvisor_config_dir }}"
tags:
- cadvisor_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- cadvisor_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ cadvisor_port }}"
when: ansible_selinux.status == "enabled"
tags:
- cadvisor_configure
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- cadvisor_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: configure.yml
tags:
- cadvisor_configure

View file

@ -1,41 +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: Check if cadvisor is installed
ansible.builtin.stat:
path: "{{ cadvisor_binary_install_dir }}/cadvisor"
register: __cadvisor_is_installed
check_mode: false
tags:
- cadvisor_install
- name: Gather currently installed cadvisor version (if any)
ansible.builtin.command: "{{ cadvisor_binary_install_dir }}/cadvisor --version"
changed_when: false
register: __cadvisor_current_version_output
check_mode: false
when: __cadvisor_is_installed.stat.exists
tags:
- cadvisor_install
- name: Common preflight
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: preflight.yml
- name: Discover latest version
ansible.builtin.set_fact:
@ -46,5 +13,9 @@
retries: 10
when:
- cadvisor_version == "latest"
- cadvisor_binary_local_dir | length == 0
- not cadvisor_skip_install
tags:
- cadvisor
- install
- cadvisor_install
- download
- cadvisor_download

View file

@ -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"

View file

@ -1,10 +1,9 @@
---
go_arch_map:
x86_64: 'amd64'
aarch64: 'arm64'
armv7l: 'arm'
armv6l: 'arm'
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
_cadvisor_go_ansible_arch: "{{ {'i386': '386',
'x86_64': 'amd64',
'aarch64': 'arm64',
'armv7l': 'armv7',
'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
_cadvisor_repo: "google/cadvisor"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_cadvisor_binaries: ['cadvisor']