From 992801a4376ab90f745af63b2744bf4d7c16af4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Bavelier?= <97530782+tbavelier@users.noreply.github.com> Date: Thu, 24 Oct 2024 15:01:48 +0200 Subject: [PATCH] [AGENT-12513] Fix Ansible check mode when installer/apm injection is enabled (#619) * Move ansible_check_mode to "parent" condition instead of individual blocks * Do not attempt bootstrap/installer commands nor report telemetry in check mode * do not check for possible non-existing services in check_mode --- tasks/agent-linux.yml | 2 +- tasks/apm-inject-install.yml | 10 +++++----- tasks/installer-setup.yml | 20 +++++++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/tasks/agent-linux.yml b/tasks/agent-linux.yml index daabcbdb..1221c336 100644 --- a/tasks/agent-linux.yml +++ b/tasks/agent-linux.yml @@ -95,7 +95,7 @@ name: "{{ item }}" state: stopped enabled: false - when: not datadog_skip_running_check and not datadog_enabled + when: not datadog_skip_running_check and not datadog_enabled and not ansible_check_mode with_list: - datadog-agent - datadog-agent-process diff --git a/tasks/apm-inject-install.yml b/tasks/apm-inject-install.yml index 7d83cb13..b5566628 100644 --- a/tasks/apm-inject-install.yml +++ b/tasks/apm-inject-install.yml @@ -8,26 +8,26 @@ when: not ansible_check_mode and ansible_facts.os_family in ["RedHat", "Rocky", "AlmaLinux"] - name: Run dd-host-install - when: not datadog_installer_enabled or not datadog_installer_owns_injector + when: not ansible_check_mode and (not datadog_installer_enabled or not datadog_installer_owns_injector) block: - name: Check if dd-host-install needs to run command: dd-host-install --no-config-change --no-agent-restart --dry-run register: agent_dd_host_install_cmd changed_when: false - when: not ansible_check_mode and datadog_apm_instrumentation_enabled in ["all", "host"] + when: datadog_apm_instrumentation_enabled in ["all", "host"] failed_when: agent_dd_host_install_cmd.rc >= 2 - name: Run APM host injection setup script command: dd-host-install --no-config-change --no-agent-restart notify: restart datadog-agent - when: not ansible_check_mode and datadog_apm_instrumentation_enabled in ["all", "host"] and agent_dd_host_install_cmd.rc == 1 + when: datadog_apm_instrumentation_enabled in ["all", "host"] and agent_dd_host_install_cmd.rc == 1 changed_when: true - name: Check if dd-container-install needs to run command: dd-container-install --dry-run register: agent_dd_container_install_cmd changed_when: false - when: not ansible_check_mode and datadog_apm_instrumentation_enabled in ["all", "docker"] + when: datadog_apm_instrumentation_enabled in ["all", "docker"] failed_when: agent_dd_container_install_cmd.rc >= 2 - name: Create Docker APM injection config file @@ -42,5 +42,5 @@ - name: Run APM host-docker injection (Docker) setup script # this command will change /etc/docker/daemon.json and reload docker if changes are made. command: dd-container-install - when: not ansible_check_mode and datadog_apm_instrumentation_enabled in ["all", "docker"] and agent_dd_container_install_cmd.rc == 1 + when: datadog_apm_instrumentation_enabled in ["all", "docker"] and agent_dd_container_install_cmd.rc == 1 changed_when: true diff --git a/tasks/installer-setup.yml b/tasks/installer-setup.yml index b0eb55f4..19238881 100644 --- a/tasks/installer-setup.yml +++ b/tasks/installer-setup.yml @@ -50,38 +50,38 @@ DD_AGENT_MAJOR_VERSION: "{{ datadog_agent_major_version | default('') }}" DD_AGENT_MINOR_VERSION: "{{ datadog_agent_minor_version | default('') }}" ignore_errors: true - when: not datadog_installer_install_result.failed + when: not ansible_check_mode and not datadog_installer_install_result.failed changed_when: true - name: Check if installer owns datadog-agent package command: datadog-installer is-installed "{{ datadog_agent_flavor }}" failed_when: datadog_installer_owns_agent.rc != 0 and datadog_installer_owns_agent.rc != 10 register: datadog_installer_owns_agent - when: not datadog_installer_bootstrap_result.failed + when: not ansible_check_mode and not datadog_installer_bootstrap_result.failed changed_when: true - name: Check if installer owns apm injector package command: datadog-installer is-installed "{{ datadog_inject_apm_flavor }}" failed_when: datadog_installer_owns_injector_result.rc != 0 and datadog_installer_owns_injector_result.rc != 10 register: datadog_installer_owns_injector_result - when: not datadog_installer_bootstrap_result.failed + when: not ansible_check_mode and not datadog_installer_bootstrap_result.failed changed_when: true - name: Register if the installer owns apm injector package set_fact: datadog_installer_owns_injector: "{{ datadog_installer_owns_injector_result.rc == 0 }}" - when: not datadog_installer_bootstrap_result.failed + when: not ansible_check_mode and not datadog_installer_bootstrap_result.failed - name: Disable agent install if owned by installer set_fact: agent_datadog_skip_install: true - when: not datadog_installer_bootstrap_result.failed and datadog_installer_owns_agent.rc == 0 + when: not ansible_check_mode and not datadog_installer_bootstrap_result.failed and datadog_installer_owns_agent.rc == 0 - name: Query APM packages owned by installer command: datadog-installer is-installed "datadog-apm-library-{{ item }}" register: datadog_installer_owned_apm_packages loop: "{{ datadog_apm_instrumentation_libraries }}" - when: not datadog_installer_bootstrap_result.failed + when: not ansible_check_mode and not datadog_installer_bootstrap_result.failed failed_when: datadog_installer_owned_apm_packages.rc != 0 and datadog_installer_owned_apm_packages.rc != 10 changed_when: true environment: @@ -95,6 +95,7 @@ set_fact: datadog_apm_instrumentation_libraries: "{{ datadog_apm_instrumentation_libraries | difference([item.item]) }}" when: + - not ansible_check_mode - item.rc == 0 - not datadog_installer_bootstrap_result.failed loop: "{{ datadog_installer_owned_apm_packages.results }}" @@ -106,6 +107,7 @@ + [(item.item | regex_replace('[:]latest', '') | regex_replace('^datadog-apm-library-', '') )]) }}" loop: "{{ datadog_installer_owned_apm_packages.results }}" when: + - not ansible_check_mode - item.rc == 0 - not datadog_installer_bootstrap_result.failed @@ -135,6 +137,7 @@ packages_to_install=datadog_apm_instrumentation_libraries_unfiltered, packages_to_install_filtered=datadog_apm_instrumentation_libraries, )) }}" + when: not ansible_check_mode - name: Send Installer telemetry traces uri: @@ -145,6 +148,7 @@ headers: DD-Api-Key: "{{ datadog_api_key }}" body_format: json + when: not ansible_check_mode failed_when: false - name: Setup logs body @@ -166,6 +170,7 @@ stderr=datadog_installer_install_result.stderr | default('') + datadog_installer_bootstrap_result.stderr | default(''),\n ) )}}" + when: not ansible_check_mode - name: Send Installer telemetry logs uri: @@ -176,9 +181,10 @@ headers: DD-Api-Key: "{{ datadog_api_key }}" body_format: json + when: not ansible_check_mode failed_when: false - name: Propagate failures after telemetry fail: msg: "Installer bootstrap failed: {{ datadog_installer_bootstrap_result.stderr }}" - when: datadog_installer_bootstrap_result.failed + when: not ansible_check_mode and datadog_installer_bootstrap_result.failed