Skip to content

Conversation

@mherold
Copy link
Contributor

@mherold mherold commented Oct 16, 2025

This PR fixes compatibility issues with Ansible 12 by updating conditional statements to use proper boolean evaluation. It fixes "Conditionals must have a boolean result" error.

Changes:

  • Updated conditionals from when: var | d() to when: (var | d("")) | is truthy for string variables

See https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_12.html#example-implicit-boolean-conversion

@nickjj
Copy link
Owner

nickjj commented Oct 16, 2025

Thanks a lot for this, I haven't upgraded to that version of Ansible yet.

Most of the changes are things like this (item.name | d() | length > 0).

What happens when d() is evaluated to None when the item doesn't exist? Will the call to length throw an exception?

I don't have a system to test this on.

If it's a problem, maybe we should be explicit and use d("") to say we want an empty string?

@nickjj
Copy link
Owner

nickjj commented Oct 16, 2025

Another thing to consider is the docs mention | length > 0 or | is truthy as being both valid solutions.

If both work, there is a readability consideration here.

The docs themselves use length > 0 in the examples. Personally I find this easier to mentally parse because then I don't need to think about what is truthy or not because I know a non-empty string is True.

With that said, we are dealing with a when condition and tying in the idea of is truthy is quite readable in this context because then it basically says "when this thing is true".

I'm just writing this out so it's documented. I'm not sure which option to pick yet. Do you have any considerations?

@mherold mherold force-pushed the ansible-12-compat branch 2 times, most recently from ea99a75 to aeac254 Compare October 17, 2025 08:31
@mherold
Copy link
Contributor Author

mherold commented Oct 17, 2025

@nickjj Thanks for the quick response + suggestions!

  1. Although I just tested that d() does work, I changed it to d("") ... I think it's good to be explicit here.
  2. I added a separate commit replacing the length test with is truthy. You can use it to compare ;-) It's shorter, and I personally like it a bit better reading it. If you agree, I can squash the two commits accordingly - or remove it otherwise.

WDYT?

@nickjj
Copy link
Owner

nickjj commented Oct 17, 2025

Thanks.

Let me think about it for a day or 2, but we'll merge one of them!

@nickjj
Copy link
Owner

nickjj commented Oct 18, 2025

Looks good, do you want to roll this up to 1 commit? We can go with the is truthy version.

Replace 'when: var | d()' with 'when: (var | d("")) | is truthy'
for string variables.
Fixes 'Conditionals must have a boolean result' error in
Ansible 12.

See https://docs.ansible.com/ansible/latest/porting_guides/
porting_guide_12.html#example-implicit-boolean-conversion
@mherold
Copy link
Contributor Author

mherold commented Oct 19, 2025

All rolled up.

@nickjj nickjj merged commit 36daf13 into nickjj:master Oct 19, 2025
2 checks passed
@nickjj
Copy link
Owner

nickjj commented Oct 19, 2025

Thanks a lot for this!

I just released v2.5.2 which has this available.

@mherold mherold deleted the ansible-12-compat branch October 19, 2025 16:04
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.

2 participants