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

**URL:** <https://forum.ansible.com/t/how-to-read-the-ansible-cfg-file-variables-in-ansible-custom-modules/36937>\
**Category:** Ansible Developer\
**Created:** [August 3, 2022, 7:33am UTC](https://forum.ansible.com/t/how-to-read-the-ansible-cfg-file-variables-in-ansible-custom-modules/36937 "2022-08-03T07:33:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naveen\_Kandakur](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Naveen\_Kandakur](https://forum.ansible.com/u/Naveen_Kandakur)\
**Post date:** [August 3, 2022, 7:33am UTC](https://forum.ansible.com/t/how-to-read-the-ansible-cfg-file-variables-in-ansible-custom-modules/36937/1 "2022-08-03T07:33:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 3, 2022, 8:03am UTC](https://forum.ansible.com/t/how-to-read-the-ansible-cfg-file-variables-in-ansible-custom-modules/36937/2 "2022-08-03T08:03:51Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Naveen\_Kandakur](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Naveen\_Kandakur](https://forum.ansible.com/u/Naveen_Kandakur)\
**Post date:** [August 3, 2022, 11:04am UTC](https://forum.ansible.com/t/how-to-read-the-ansible-cfg-file-variables-in-ansible-custom-modules/36937/3 "2022-08-03T11:04:48Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 3, 2022, 1:13pm UTC](https://forum.ansible.com/t/how-to-read-the-ansible-cfg-file-variables-in-ansible-custom-modules/36937/4 "2022-08-03T13:13:25Z")

</div>

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.
