# ansible\_facts access within a custom module

**URL:** <https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245>\
**Category:** Ansible Project\
**Created:** [May 15, 2014, 10:32am UTC](https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245 "2014-05-15T10:32:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Huck](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Christian\_Huck](https://forum.ansible.com/u/Christian_Huck)\
**Post date:** [May 15, 2014, 10:32am UTC](https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245/1 "2014-05-15T10:32:05Z")

</div>

Hi,

how can I access the ansible\_facts within a custom module which was initialized with the AnsibleModule class ?

I either:

a) need to pass the facts as a module parameter

or

b) access the facts directly inside the module

Thanks in advance.

Christian

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 11:30am UTC](https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245/2 "2014-05-15T11:30:48Z")

</div>

You can access the fact functions by importing the “module\_utils” code for the facts functions and calling them directly from within your module.

This is in fact how the setup module itself is written.

Take a look at what it does and follow suit, and it should be pretty straightforward.

In the future, this is about developing on Ansible, so probably better suited for ansible-devel. I’m trying to get that list to have a bit nicer momentum 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![Christian\_Huck](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Christian\_Huck](https://forum.ansible.com/u/Christian_Huck)\
**Post date:** [May 15, 2014, 11:45am UTC](https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245/3 "2014-05-15T11:45:21Z")

</div>

Hi Michael,

I’ll give that a try.

Thank you !

---

<div class="post-metadata">

**Author:** ![Mona\_Gopal](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mona\_Gopal](https://forum.ansible.com/u/Mona_Gopal)\
**Post date:** [May 17, 2016, 11:19am UTC](https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245/4 "2016-05-17T11:19:02Z")

</div>

Hi,

import exceptions  
import subprocess  
from ansible.module\_utils.basic import \*

def main():  
module = AnsibleModule(argument\_spec = dict(data=dict(required=False, default=None),),supports\_check\_mode = True)  
dt = {“System\_Details”:{‘Kernel\_version’:ansible\_hostname}}

module.exit\_json(ansible\_facts=dt)  
main()

Above is my custom module, where i am trying to access the ansible facts.  
I am getting the below error:

fatal: [172.19.3.60]: FAILED! =\> {“changed”: false, “failed”: true, “module\_stderr”: “”, “module\_stdout”: “Traceback (most recent call last):\r\n File "/root/.ansible/tmp/ansible-tmp-1463490863.86-275822236207074/get\_system\_specific\_details", line 2014, in \r\n main()\r\n File "/root/.ansible/tmp/ansible-tmp-1463490863.86-275822236207074/get\_system\_specific\_details", line 2011, in main\r\n dt = {"System\_Details":{‘Kernel\_version’:ansible\_hostname}}\r\nNameError: global name ‘ansible\_hostname’ is not defined\r\n”, “msg”: “MODULE FAILURE”, “parsed”: false}

Can you please help me out??

Thanks in advance,  
Mona G

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 17, 2016, 2:14pm UTC](https://forum.ansible.com/t/ansible-facts-access-within-a-custom-module/15245/5 "2016-05-17T14:14:40Z")

</div>

Any required facts need to be passed in, a module normally executes on a remote machine and we pass only the options given to the module. We do not pass all the data that the ansible controller has, both for security and performance reasons.
