1

Sometimes, when I browse an Ansible role, I see that a developer has put double underscores for a variable.

As Ansible is based on Python, I assumed it was a Pythonic thingy.

When I read the docs of Ansible, it states:

In many programming languages, variables that begin with an underscore are private. This is not true in Ansible. Variables that begin with an underscore are treated exactly the same as any other variable. Do not rely on this convention for privacy or security.

But, as I look at the variables, I'm actually not sure why this is done.

Q: Can someone explain to me in detail what the reason is to apply double underscores for varaibles in Ansible?

2
  • 3
    It would appear to just be a convention the author of the sap-preconfigure role chose to use. Ansible doesn't treat them differently, but someone using this role is expected not to change their values; they are set in various OS-specific var files. Commented Sep 1, 2021 at 14:05
  • It is just a coding style and probably based on naming convention. You may have a look into the use of underscore in variable and method names. Commented Nov 17, 2021 at 6:52

1 Answer 1

1

As suggested in "Red Hat Communities of Practice" website :

[...] internal variables (those that are not expected to be set by users) are to be prefixed by two underscores: __foo_variable.

I couldn't find any official ansible documentation.

It might be an inspired practice from python where private attributes are prefixed with __ for "name mangling" see more.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.