how to read the ansible.cfg file variables in ansible custom modules

27 views
Skip to first unread message

Naveen Kandakur

unread,
Aug 3, 2022, 3:33:00 AM8/3/22
to Ansible Development

I am new to Ansible Custom module development, can any one help me how i can access the variables defined in ansible.cfg file into my ansible modules python file.

Felix Fontein

unread,
Aug 3, 2022, 4:04:11 AM8/3/22
to ansibl...@googlegroups.com
Hi,

> I am new to Ansible Custom module development, can any one help me
> how i can access the variables defined in ansible.cfg file into my
> ansible modules python file.

by default you cannot. IMO you also should not, because that's contrary
to expectations that users have of modules. Modules can only access
things that users explicitly pass on to them.

(The only exception are certain internal arguments automatically passed
on, see
https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html#internal-arguments
for details.)

One way to work around this is to have an accompanying action plugin
that passes on more data to the module. But if you do that, you really
have to make sure to avoid accidentally sending something to the remote
that the user did not want to have sent, like credentials or personal
data that must not leave the current host. (For a module author it is
next to impossible to decide which such data is sensitive and which
not, so it's best to leave that decision to the user of the module.)

Cheers,
Felix



On Wed, 3 Aug 2022 00:33:00 -0700 (PDT)
Naveen Kandakur <naveen....@gmail.com> wrote:

> <https://stackoverflow.com/posts/73217822/timeline>

Naveen Kandakur

unread,
Aug 3, 2022, 7:04:48 AM8/3/22
to Ansible Development
Thanks for the info Felix,
I wanted to do some decision making in my module based on values configured in the ansible.cfg
for example, if the python_interpreter variable value set to python2 in ansible.cfg then i want to throw an appropriate error message by checking the python_interpreter value in my module

is it possible to do ?

Brian Coca

unread,
Aug 3, 2022, 9:14:42 AM8/3/22
to Naveen Kandakur, Ansible Development
It is possible, just not via the variable, do `import sys;
sys.version_info < (3, 0)`, since that will reflect the python the
script is running under.

----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages