# custom module, how to locate ansible magic vars

**URL:** <https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683>\
**Category:** Ansible Project\
**Created:** [September 28, 2018, 11:35pm UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683 "2018-09-28T23:35:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adolphson](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Adolphson](https://forum.ansible.com/u/Adolphson)\
**Post date:** [September 28, 2018, 11:35pm UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683/1 "2018-09-28T23:35:01Z")

</div>

I am trying to write my first custom module and cannot find how to locate an ansible var that is not part of the task parameters sent. For example, let’s say I want to find the ansible\_ssh\_host or inventory\_hostname and they are not in the environment vars. I searched for import ansible.constants but could not find any examples or if this is the right module to call for this.

Any help would be much appreciated.

---

<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:** [September 29, 2018, 1:12am UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683/2 "2018-09-29T01:12:39Z")

</div>

You cannot, you only have access to the options passed into the module itself.

---

<div class="post-metadata">

**Author:** ![Adolphson](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Adolphson](https://forum.ansible.com/u/Adolphson)\
**Post date:** [September 29, 2018, 1:20pm UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683/3 "2018-09-29T13:20:21Z")

</div>

I am curious how for example the copy module works. There are no options to pass the user, password or server but somehow it must work. I suspect it uses ssh private keys that are set in ansible.cfg file. Is this how it works and if so how does the copy module access these values? I know I can pass whatever I want into the environment, but was hoping there was an ansible module which could access the values in ansible.cfg. I found the ansible.config.manager module and was wondering if it is used to parse the ansible.cfg file.

---

<div class="post-metadata">

**Author:** ![Adolphson](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Adolphson](https://forum.ansible.com/u/Adolphson)\
**Post date:** [September 29, 2018, 3:56pm UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683/4 "2018-09-29T15:56:35Z")

</div>

I was able to parse the ansible.cfg file using “ansible.module\_utils.six.moves import configparser” and pull the values I am looking for. Now I just need a way to pull the remote\_addr variable so I would not need to pass in the server: option from the task.

configparser examples = [https://wiki.python.org/moin/ConfigParserExamples](https://wiki.python.org/moin/ConfigParserExamples)

---

<div class="post-metadata">

**Author:** ![James\_Tanner](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@James\_Tanner](https://forum.ansible.com/u/James_Tanner)\
**Post date:** [September 29, 2018, 4:32pm UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683/5 "2018-09-29T16:32:39Z")

</div>

There’s another class of plugins that you can think of as “sitting on top of modules”:

[https://docs.ansible.com/ansible/devel/plugins/action.html](https://docs.ansible.com/ansible/devel/plugins/action.html)

Action plugins can do a lot of complicated things such as accessing vars and internal play contexts.

---

<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:** [October 1, 2018, 7:41pm UTC](https://forum.ansible.com/t/custom-module-how-to-locate-ansible-magic-vars/28683/6 "2018-10-01T19:41:17Z")

</div>

@texas copy does not read the connection information, that is what the  
connection plugin does, it is not module specific.
