# set\_fact question

**URL:** <https://forum.ansible.com/t/set-fact-question/13076>\
**Category:** Ansible Project\
**Created:** [July 5, 2013, 10:40pm UTC](https://forum.ansible.com/t/set-fact-question/13076 "2013-07-05T22:40:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [July 5, 2013, 10:40pm UTC](https://forum.ansible.com/t/set-fact-question/13076/1 "2013-07-05T22:40:53Z")

</div>

Hi I’m trying to use set\_fact but am struggling with extracting it’s contents later in the playbook:

- name: set ldap\_host  
action: set\_fact ldap\_host=“blah”

- name: check ldap\_host  
fail: msg=“something screwed up”  
only\_if: “‘{{ansible\_facts["my\_ldap\_host"]}}’ != ‘$ldap\_host’”

what am I doing wrong in only\_if section?

---

<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:** [July 5, 2013, 10:55pm UTC](https://forum.ansible.com/t/set-fact-question/13076/2 "2013-07-05T22:55:56Z")

</div>

Can I ask where you learned only\_if?

It’s old stuff that I hoped we purged all references to.

I don’t know where you have my\_ldap\_host set, but it would just look like this:

when: my\_ldap\_host == ldap\_host

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [July 6, 2013, 1:30am UTC](https://forum.ansible.com/t/set-fact-question/13076/3 "2013-07-06T01:30:30Z")

</div>

I can see it here in the module documentation:  
[http://www.ansibleworks.com/docs/modules.html/#debug](http://www.ansibleworks.com/docs/modules.html/#debug)

and fail  
[http://www.ansibleworks.com/docs/modules.html/#fail](http://www.ansibleworks.com/docs/modules.html/#fail)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 6, 2013, 2:27am UTC](https://forum.ansible.com/t/set-fact-question/13076/4 "2013-07-06T02:27:32Z")

</div>

Thanks I am going to file a ticket to remove those!

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [July 8, 2013, 6:10pm UTC](https://forum.ansible.com/t/set-fact-question/13076/5 "2013-07-08T18:10:58Z")

</div>

thanks, changing the code to the above seems to have worked. However I’ve got to ask: does it mean that ansible facts are kind of like “global variables” that are set depending on host processed? I thought access to facts was via complex structures like ansible\_\* is there a document outlining those fine details about “facts” ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 8, 2013, 6:22pm UTC](https://forum.ansible.com/t/set-fact-question/13076/6 "2013-07-08T18:22:53Z")

</div>

ansible facts have values that vary per host, so they are not global.

Certain variables like ansible\_ssh\_host mean something special when set in inventory, and this is useful as an inventory variable, but they are not facts. They are just variables set on each host.

Facts would refer to variable data derived from running the ‘setup’ module (which gets run automatically) or other modules that return an “ansible\_facts” dictionary, which are usually refered to as “custom facts modules”

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [July 9, 2013, 8:41am UTC](https://forum.ansible.com/t/set-fact-question/13076/7 "2013-07-09T08:41:59Z")

</div>

During our Ansible meetup I noted a list of items that are confusing.

One of the items on my list was the differences between facts or variables, their context, the internal data-structure and how they are being 'injected' when they are used/substituted.

Not just because it is often hard to foresee which value a variable/fact may have (given they live in the same namespace) also because it is sometimes unclear/ambiguous how they are being processed.

I think the documentation should make this more clear, and point out the pitfalls.

Things that should be mentioned:

&nbsp;&nbsp;- the different data-structures that 'host' these variables/facts  
&nbsp;&nbsp;- where they could come from  
&nbsp;&nbsp;- how certain variables (e.g. group variables) may be overriden by other  
&nbsp;&nbsp;&nbsp;&nbsp;groups the system is part of (and what the policy here is, last  
&nbsp;&nbsp;&nbsp;&nbsp;processed group wins ? how does this mechanism works with multiple  
&nbsp;&nbsp;&nbsp;&nbsp;inventory scripts ?)  
&nbsp;&nbsp;- play-specific variables are local to the play, other facts or variables  
&nbsp;&nbsp;&nbsp;&nbsp;are always global, they outlive the play and exist over playbooks in a  
&nbsp;&nbsp;&nbsp;&nbsp;single run ?

Only someone with an intimate knowledge of the internals can do this though.

Also some better way to inspect these different data-structures and/or a mechanism to understand where a value comes from could help here. The obvious goal here is to create awareness as this is not something we can easily fix in the design at this point.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 10, 2013, 1:16am UTC](https://forum.ansible.com/t/set-fact-question/13076/8 "2013-07-10T01:16:52Z")

</div>

Happy to try to make this more clear…

" - the different data-structures that ‘host’ these variables/facts

- where they could come from

Not sure I understand this part, can you elaborate?

- how certain variables (e.g. group variables) may be overriden by other  
groups the system is part of (and what the policy here is, last  
processed group wins ? how does this mechanism works with multiple  
inventory scripts ?)

I think this is what the precedence section of the “Advanced Playbook” page was written to answer:

[http://www.ansibleworks.com/docs/playbooks2.html#understanding-variable-precedence](http://www.ansibleworks.com/docs/playbooks2.html#understanding-variable-precedence)

It’s a fine line to balance between overwhelming someone, making someone know how to use it, and sharing how the code works (which someone should not be expected to understand), but I’m all for making this easier to understand, if we can also make it still remain concise and not too technical.

- play-specific variables are local to the play, other facts or variables  
are always global, they outlive the play and exist over playbooks in a  
single run ?"

I don’t think facts are global (they apply only to (or about) the host) so I think we’re having some sort of terminology disconnect. They live beyond a play, yes!
