# Conditinals based on remote facts

**URL:** <https://forum.ansible.com/t/conditinals-based-on-remote-facts/17708>\
**Category:** Ansible Project\
**Created:** [March 10, 2015, 12:02pm UTC](https://forum.ansible.com/t/conditinals-based-on-remote-facts/17708 "2015-03-10T12:02:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [March 10, 2015, 12:02pm UTC](https://forum.ansible.com/t/conditinals-based-on-remote-facts/17708/1 "2015-03-10T12:02:33Z")

</div>

Hi

I would need to run tasks based on a remote fact - by executing a script to extract the actual variable. Currently I’m doing it in the following way:

> - name: Check Java Version  
> **shell: $JAVA\_HOME/bin/java -version 2\>&1 | grep ‘java version’ | awk ‘{ print substr($3, 2, length($3)-2); }’**  
> **register: actual\_java\_version**
> 
> - name: Do Something based on Java version  
> when: **actual\_java\_version.stdout**!= …

Every time I run this script it says:

> PLAY [vm] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [vm]
> 
> TASK: [java-noinstall | Check Java Version] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> **changed: [vm]**

Which is not the desired behaviour. Ansible should not consider that as a ‘change’.  
Additionally it stores the whole context into that ‘actual\_java\_version’ variable (the script itself, the timestamp, wheter it was ok, etc…) and if I need the actual result I need to address it like ‘actual\_java\_version.stdout’.  
So all-in-all this is not a clean solution from my point of view.  
What could I do?

The most clean way would be to collect that information in the GATHERING FACTS phase as it is done for e.g. $JAVA\_HOME:

> ansible\_env.JAVA\_HOME

…but for that I would need to create a new module just for that custom fact ([http://serverascode.com/2015/01/27/ansible-custom-facts.html](http://serverascode.com/2015/01/27/ansible-custom-facts.html)).

set\_fact does not help me, since it just creates a new variable out of it with extended scope (to “survive between plays”).

Is there a quick solution like considering my TASK always OK?  
Is there a clean solution without creating a new module for my custom fact?

Regards:  
Bence

---

<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:** [March 10, 2015, 2:17pm UTC](https://forum.ansible.com/t/conditinals-based-on-remote-facts/17708/2 "2015-03-10T14:17:30Z")

</div>

shell/command module actions are considered changed by default as  
ansible has no way to know what transpired, there are several ways to  
tell ansible, in your case i recommend adding the changed\_when:  
directive.

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [March 10, 2015, 3:19pm UTC](https://forum.ansible.com/t/conditinals-based-on-remote-facts/17708/3 "2015-03-10T15:19:03Z")

</div>

I created my custom facts file

/etc/ansible/facts.dcustom\_facts.fact

> #!/bin/bash  
> JAVA\_VERSION=`"$JAVA_HOME/bin/java" -version 2>&1 | awk '/java version/{ print substr($3, 2, length($3)-2); }'`
> 
> cat \<\<EOF  
> {  
> “java\_version” : “$JAVA\_VERSION”  
> }  
> EOF

If I run it on the control machine, everything is ok:  
{  
“java\_version” : “1.7.0\_45”  
}

But if I run it on the remote machine:

> ansible -m setup vm | grep ‘java\_version’

then it is not populated. Only the ordinary ansible\_facts are present, my java\_version is not there.  
It’s the same if I declares the fact\_path:

> ansible -m setup vm -a ‘fact\_path=/etc/ansible/facts.d’ | grep ‘java\_version’

I’m using ansible version 1.8.2

Any tips?

1. március 10., kedd 13:02:33 UTC+1 időpontban Bence Takács a következőt írta:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [March 10, 2015, 3:23pm UTC](https://forum.ansible.com/t/conditinals-based-on-remote-facts/17708/4 "2015-03-10T15:23:07Z")

</div>

THanks, it do work using that:

> - name: Check Java Version  
> shell: $JAVA\_HOME/bin/java -version 2\>&1 | grep ‘java version’ | awk ‘{ print substr($3, 2, length($3)-2); }’  
> register: actual\_java\_version  
> changed\_when: FALSE

result:

> PLAY [vm] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [vm]
> 
> TASK: [java-noinstall | Check Java Version] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [vm]

1. március 10., kedd 15:17:34 UTC+1 időpontban Brian Coca a következőt írta:
