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_versionname: 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).
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