Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default values when variables are defined but nil #303

Merged
merged 2 commits into from
Nov 6, 2020

Conversation

KSerrania
Copy link
Contributor

What does this PR do?

Adds guards when trying to read variable lengths, to avoid reading the length of null, which results in a crash.

Motivation

It is possible to get into situations where a variable is set, but has a null value (eg. by adding key: in the yaml vars). If we check the length of such a variable (which should thus be a list, string or dictionary), we should first default to a reasonable value if a null value is given.

default needs to be used with boolean=True to also match falsey values (such as null), otherwise it only matches undefined variables.

@KSerrania KSerrania requested a review from a team as a code owner October 1, 2020 19:43
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's two consecutive | on this line, is that on purpose ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a typo indeed 😄

Copy link
Contributor

@julien-lebot julien-lebot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with Ansible but those changes LGTM !

@derekwbrown derekwbrown merged commit 18a4013 into master Nov 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants