Files
dsk-iac/ansible/01_old/roles/datadog.datadog/tasks/parse-version.yml
2023-12-19 13:36:16 +09:00

105 lines
5.0 KiB
YAML

---
- name: Parse Agent version
set_fact:
agent_version: "{{ datadog_agent_version | regex_search(regexp, '\\g<epoch>', '\\g<major>', '\\g<minor>', '\\g<bugfix>', '\\g<suffix>', '\\g<release>') }}"
vars:
regexp: '(?:(?P<epoch>[0-9]+):)?(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<bugfix>[0-9]+)(?P<suffix>(?:~|-)[^0-9\s-]+[^-\s]*)?(?:-(?P<release>[0-9]+))?'
- name: Set version vars
set_fact:
datadog_epoch: "{{ agent_version.0 | default('', true) | string }}"
datadog_major: "{{ agent_version.1 | default('', true) | string }}"
datadog_minor: "{{ agent_version.2 | default('', true) | string }}"
datadog_bugfix: "{{ agent_version.3 | default('', true) | string }}"
datadog_suffix: "{{ agent_version.4 | default('', true) | string }}"
datadog_release: "{{ agent_version.5 | default('', true) | string }}"
- name: Fill empty version epoch with default
set_fact:
datadog_epoch: "1"
when: datadog_epoch | length == 0
- name: Fill empty version release with default
set_fact:
datadog_release: "1"
when: datadog_release | length == 0
- name: Stop play if datadog_agent_version and datadog_agent_major_version are not compatible
fail:
msg: "The provided major version {{ datadog_agent_major_version }} is not compatible with the
version {{ datadog_major }} deduced from datadog_agent_version ({{ datadog_agent_version }}).
Aborting play."
when: datadog_agent_major_version | length > 0 and datadog_major != datadog_agent_major_version
- name: Set datadog_agent_major_version to deduced value from datadog_agent_version
set_fact:
datadog_agent_major_version: "{{ datadog_major }}"
- name: Set helper variables
set_fact:
datadog_agent_linux_version: "{{ datadog_epoch }}:{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}-{{ datadog_release }}"
datadog_rpm_version_finding_cmd: "rpm -q --qf '%{EPOCH}:%{VERSION}-%{RELEASE}' {{ datadog_agent_flavor }}"
- name: Set OS-specific versions
# NOTE: if changing these, make sure the format correspond with values in datadog_version_finding_cmds below
set_fact:
datadog_agent_debian_version: "{{ datadog_agent_linux_version }}"
datadog_agent_redhat_version: "{{ datadog_agent_linux_version }}"
datadog_agent_suse_version: "{{ datadog_agent_linux_version }}"
datadog_agent_windows_version: "{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}"
datadog_agent_macos_version: "{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}"
- name: Construct commands to find Agent version
set_fact:
datadog_version_finding_cmds:
Debian: "dpkg -s {{ datadog_agent_flavor }} | grep '^Version:' | awk '{print $2}'"
RedHat: "{{ datadog_rpm_version_finding_cmd }}"
Rocky: "{{ datadog_rpm_version_finding_cmd }}"
AlmaLinux: "{{ datadog_rpm_version_finding_cmd }}"
Suse: "{{ datadog_rpm_version_finding_cmd }}"
- name: Create OS-specific version dict
set_fact:
datadog_agent_os2version:
Debian: "{{ datadog_agent_debian_version }}"
RedHat: "{{ datadog_agent_redhat_version }}"
Rocky: "{{ datadog_agent_redhat_version }}"
AlmaLinux: "{{ datadog_agent_redhat_version }}"
Suse: "{{ datadog_agent_suse_version }}"
Windows: "{{ datadog_agent_windows_version }}"
Darwin: "{{ datadog_agent_macos_version }}"
- name: Get Linux Agent version
shell: "{{ datadog_version_finding_cmds[ansible_facts.os_family] }}" # noqa 305 - Ansible lint thinks we could use command, but we need shell because some of the cmds have pipes
register: datadog_version_check_linux
changed_when: false
failed_when: false
check_mode: no
when: ansible_facts.system is defined and ansible_facts.system == "Linux"
# The task is win_shell, so if users don't have the "ansible.windows" collection installed,
# parsing the task would fail even if the host is not Windows. By hiding the task inside
# a conditionally included file, we can prevent this.
- name: Include Windows Agent version tasks
include_tasks: parse-version-windows.yml
when: ansible_facts.os_family == "Windows"
- name: Include macOS Agent version tasks
include_tasks: parse-version-macos.yml
when: ansible_facts.os_family == "Darwin"
- name: Set skip install flag if version already installed (Linux)
set_fact:
datadog_skip_install: "{{ datadog_version_check_linux.stdout | trim == datadog_agent_os2version[ansible_facts.os_family] }}"
when: ansible_facts.system is defined and ansible_facts.system == "Linux"
- name: Set skip install flag if version already installed (Windows)
set_fact:
datadog_skip_install: "{{ datadog_version_check_win.stdout | trim == datadog_agent_os2version[ansible_facts.os_family] }}"
when: ansible_facts.os_family == "Windows"
- name: Set skip install flag if version already installed (macOS)
set_fact:
datadog_skip_install: "{{ datadog_version_check_macos.stdout | trim == datadog_agent_os2version[ansible_facts.os_family] }}"
when: ansible_facts.os_family == "Darwin"