# Using setup facts within modules

**URL:** <https://forum.ansible.com/t/using-setup-facts-within-modules/12044>\
**Category:** Ansible Project\
**Created:** [October 28, 2012, 8:49pm UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044 "2012-10-28T20:49:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [October 28, 2012, 8:49pm UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044/1 "2012-10-28T20:49:46Z")

</div>

Is there a mechanism to be able to get at the facts (from the setup  
module) within a module?

&nbsp;&nbsp;Nigel.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [October 28, 2012, 9:05pm UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044/2 "2012-10-28T21:05:17Z")

</div>

Nigel Metheringham wrote:

> Is there a mechanism to be able to get at the facts (from the setup  
> module) within a module?

Not unless you pass them as arguments to the module.

Daniel

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [October 29, 2012, 7:39am UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044/3 "2012-10-29T07:39:54Z")

</div>

> \> Is there a mechanism to be able to get at the facts (from the setup  
> \> module) within a module?
> 
> Not unless you pass them as arguments to the module.

There was a time when Ansible retained the facts from `setup' in a file  
on the managed node, but that went away several months ago.

Would it be an idea to implement something like

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: store

to mean run `setup' and leave facts in a predefined/configurable file on  
the node? If configurable, it would supposedly mean carrying an  
ansible.cfg parameter into the runner (no idea how to do that -- I think  
Michael said that would be difficult to do). If predefined, it could be  
called `setup.json' or something and remain in $ANSIBLE\_TMP on the node.

Regards,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 29, 2012, 11:18am UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044/4 "2012-10-29T11:18:01Z")

</div>

> > \> Is there a mechanism to be able to get at the facts (from the setup  
> > \> module) within a module?
> > 
> > Not unless you pass them as arguments to the module.
> 
> There was a time when Ansible retained the facts from `setup' in a file  
> on the managed node, but that went away several months ago.
> 
> Would it be an idea to implement something like
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: store
> 
> to mean run `setup' and leave facts in a predefined/configurable file on  
> the node? If configurable, it would supposedly mean carrying an  
> ansible.cfg parameter into the runner (no idea how to do that -- I think  
> Michael said that would be difficult to do). If predefined, it could be  
> called `setup.json' or something and remain in $ANSIBLE\_TMP on the node.

I really don't want to do this at this time, it seems sufficient to  
pass variables to the module to get explicitly what you need, and that  
also leaves  
a record of the configuration in the playbook, which I believe is important.

---

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [October 29, 2012, 11:46am UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044/5 "2012-10-29T11:46:32Z")

</div>

Michael DeHaan wrote:

> I really don't want to do this at this time, it seems sufficient to  
> pass variables to the module to get explicitly what you need, and that  
> also leaves  
> a record of the configuration in the playbook, which I believe is important.

The use case for this is that I am hacking around with the user module  
to make it work for other OS variants, and was planning on re-using the  
more detailed OS description info from the setup module to select the  
correct module, rather than either get the user to pass stuff in (hmm...  
wonder if I should let them override?), or re-implement the appropriate  
code.

Its only two variables right now - really only one as platform.system is  
pretty damn standard, but linux (and maybe other?) distribution is a  
bit more tweakable, and having this derived in 2 separate places is  
another way of having problems later.

Maybe it could be worth pushing that code into the overall module  
support utils, before there are modules that change implementation  
depending on the system they run on?

&nbsp;&nbsp;Nigel.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 29, 2012, 11:51am UTC](https://forum.ansible.com/t/using-setup-facts-within-modules/12044/6 "2012-10-29T11:51:41Z")

</div>

> The use case for this is that I am hacking around with the user module  
> to make it work for other OS variants, and was planning on re-using the  
> more detailed OS description info from the setup module to select the  
> correct module, rather than either get the user to pass stuff in (hmm...  
> wonder if I should let them override?), or re-implement the appropriate  
> code.

Yeah, this is not how we are going to do this.

What we're going to do, like the way we handled many modules wanting  
to do file ops, is to move the OS detect function into  
lib/ansible/module\_common.py

Then we just make each module more or less organized like the  
class-based system in the setup module, so it chooses  
the right implementation based on the OS detection result.

> Maybe it could be worth pushing that code into the overall module  
> support utils

Now you've got it 🙂
