{{ data_dirs | json_query(query) | b64decode }} returns a list of two Base64 strings and then decodes them. Why does this only work by luck, and what is the robust version?
b64decode expects one string, so the list is turned into the text [['dG90…'], ['dG90…']]; Python's decoder skips the brackets and quotes and decodes the joined strings, which fails with "Incorrect padding" as soon as the first output's Base64 ends in =. The robust version decodes each item: | flatten | map('b64decode') | join('').
json_query returns one list per target ([[a], [b]]), not one string. When a filter that wants a string gets a list, Ansible uses its text representation. Python's Base64 decoder, by default, silently discards characters outside the Base64 alphabet ([, ], ', ,, spaces), so a and b are glued together and decoded as one.
That only works while the first string has no padding. Base64 pads to a multiple of 4 characters with =, and a = in the middle of the glued string ends the data: the decode fails with Incorrect padding. Whether that happens depends on the length of the first target's output, so the playbook works on Monday and fails on Tuesday. (Checked with ansible-core 2.22: when the first output's Base64 has no padding the list decodes, when it ends in == the task fails.)
Decode element by element instead:
msg: "{{ data_dirs | community.general.json_query(query)
| flatten | map('b64decode') | join('') }}"
flatten turns [[a], [b]] into [a, b], map('b64decode') decodes each string on its own, and join combines the texts.
* Decode each Base64 string separately; never decode the text of a list. *
Go deeper:
b64decode filter (Ansible docs) — the filter takes a string.
Base64: decoding with padding (Wikipedia) — what the
=at the end of encoded data means to a decoder.