Question about required_if documentation

Hello,

I have a question about required_if:
From the documentation (Ansible module architecture — Ansible Community Documentation):


required_if:

Must be a sequence of sequences. Every inner sequence describes one conditional dependency. Every sequence must have three or four values. The first two values are the option’s name and the option’s value which describes the condition. The further elements of the sequence are only needed if the option of that name has precisely this value.

If you want that all options in a list of option names are specified if the condition is met, use one of the following forms:

('option_name', option_value, ('option_a', 'option_b', ...)),
('option_name', option_value, ('option_a', 'option_b', ...), False),

So I made a spec with the following (partial) content:

        update=dict(
            type="dict",
            required=True,
            options=dict(
                accountname=dict(type="str", required=False),
                passwordpolicy=dict(type="str", required=False),
                reset_passwordpolicy=dict(type="bool", required=False),
                totp_secret=dict(type="str", required=False, no_log=True),
                reset_totp=dict(type="bool", required=False),
                disable_passwordreset=dict(type="bool", required=False),
                notes=dict(type="list", required=False, elements="str"),
                append_notes=dict(type="bool", required=False, default=False),
            ),
            mutually_exclusive=[
                ("passwordpolicy", "resetpasswordpolicy"),
                ("totp_secret", "reset_totp"),
            ],
            required_if=[("append_notes", True, ("notes"))],
        )

However, this results in: append_notes is True but all of the following are missing: n, o, t, e, s found in update.

When I change the required_if to required_if=[("append_notes", True, ["notes"])] (so with [] around the 3rd parameter instead of ()) everything works.

Is this a documentation error? An error on my side?

Python version used: 3.12.11
Ansible version used: 2.18.9

("notes") is a string, only wrapped in parentheses; so it is a sequence of individual characters in the string which is what the error message says: n, o, t, e, s.

["notes"] is a list of strings, just one in this example and that gives you the result you expected.

I believe your original intent was to use a tuple, to do that you need to differentiate between a string wrapped in parentheses and a tuple by putting a comma there: ("notes",).

So required_if=[("append_notes", True, ("notes",))] should work too.

1 Like