From 18a40131a682bad6d6574afbeeb7e225394647e1 Mon Sep 17 00:00:00 2001 From: Kylian Serrania Date: Fri, 6 Nov 2020 04:28:45 +0100 Subject: [PATCH] Add default values when variables are defined but nil (#303) * Add default values when variables are defined but nil * Fix typo --- tasks/agent-linux.yml | 2 +- tasks/main.yml | 6 +++--- tasks/pkg-windows-opts.yml | 4 ++-- tasks/set-parse-version.yml | 4 ++-- tasks/win_agent_latest.yml | 4 ++-- templates/datadog.yaml.j2 | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tasks/agent-linux.yml b/tasks/agent-linux.yml index 8b9cd24c..3d816342 100644 --- a/tasks/agent-linux.yml +++ b/tasks/agent-linux.yml @@ -4,7 +4,7 @@ - name: add "{{ datadog_user }}" user to additional groups user: name="{{ datadog_user }}" groups="{{ datadog_additional_groups }}" append=yes - when: datadog_additional_groups is defined and (datadog_additional_groups | length != 0) + when: datadog_additional_groups | default([], true) | length > 0 notify: restart datadog-agent - name: Create Datadog agent config directory diff --git a/tasks/main.yml b/tasks/main.yml index e5393d26..778e9f2f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -24,15 +24,15 @@ - name: Linux Configuration Tasks (Agent 5) include_tasks: agent5-linux.yml - when: datadog_agent_major_version|int == 5 and ansible_os_family != "Windows" + when: datadog_agent_major_version | int == 5 and ansible_os_family != "Windows" - name: Linux Configuration Tasks include_tasks: agent-linux.yml - when: datadog_agent_major_version|int > 5 and ansible_os_family != "Windows" + when: datadog_agent_major_version | int > 5 and ansible_os_family != "Windows" - name: Windows Configuration Tasks include_tasks: agent-win.yml - when: datadog_agent_major_version|int > 5 and ansible_os_family == "Windows" + when: datadog_agent_major_version | int > 5 and ansible_os_family == "Windows" - name: Integrations Tasks include_tasks: integration.yml diff --git a/tasks/pkg-windows-opts.yml b/tasks/pkg-windows-opts.yml index 112340ee..cbf7773f 100644 --- a/tasks/pkg-windows-opts.yml +++ b/tasks/pkg-windows-opts.yml @@ -1,12 +1,12 @@ - name: Set DD Username Arg set_fact: win_install_args: "{{ win_install_args }} DDAGENTUSER_NAME={{ datadog_windows_ddagentuser_name }}" - when: datadog_windows_ddagentuser_name | length > 0 + when: datadog_windows_ddagentuser_name | default('', true) | length > 0 - name: Set DD Password Arg set_fact: win_install_args: "{{ win_install_args }} DDAGENTUSER_PASSWORD={{ datadog_windows_ddagentuser_password }}" - when: datadog_windows_ddagentuser_password | length > 0 + when: datadog_windows_ddagentuser_password | default('', true) | length > 0 # check the registry. On upgrade, the location of the config file root will # be set here. diff --git a/tasks/set-parse-version.yml b/tasks/set-parse-version.yml index 8cf8d93d..d5d2fd91 100644 --- a/tasks/set-parse-version.yml +++ b/tasks/set-parse-version.yml @@ -1,10 +1,10 @@ --- - name: Convert datadog_agent_major_version to string set_fact: - datadog_agent_major_version: "{{ datadog_agent_major_version | string }}" + datadog_agent_major_version: "{{ datadog_agent_major_version | default('', true) | string }}" - include_tasks: parse-version.yml - when: datadog_agent_version | length > 0 + when: datadog_agent_version | default('', true) | length > 0 - name: Set Agent default major version set_fact: diff --git a/tasks/win_agent_latest.yml b/tasks/win_agent_latest.yml index 0a1299d2..282e1fd6 100644 --- a/tasks/win_agent_latest.yml +++ b/tasks/win_agent_latest.yml @@ -3,10 +3,10 @@ - name: (Custom) Set agent download filename to latest set_fact: dd_download_url: "{{ datadog_windows_download_url }}" - when: datadog_windows_download_url | length > 0 + when: datadog_windows_download_url | default('', true) | length > 0 - name: Set agent download filename to latest set_fact: dd_download_url: "{% if datadog_agent_major_version|int == 7 %}{{ datadog_windows_agent7_latest_url }} {% else %}{{ datadog_windows_agent6_latest_url }}{% endif %}" - when: datadog_windows_download_url | length == 0 + when: datadog_windows_download_url | default('', true) | length == 0 diff --git a/templates/datadog.yaml.j2 b/templates/datadog.yaml.j2 index 3b705e9c..a58141f1 100644 --- a/templates/datadog.yaml.j2 +++ b/templates/datadog.yaml.j2 @@ -14,6 +14,6 @@ dd_url: {{ datadog_url }} api_key: {{ datadog_api_key | default('youshouldsetthis') }} {% endif %} -{% if datadog_config is defined and datadog_config|length > 0 -%} +{% if datadog_config | default({}, true) | length > 0 -%} {{ datadog_config | to_nice_yaml }} {% endif %}