# register shell output, set as custom fact and use it as a condition of the playbook running

**URL:** <https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734>\
**Category:** Ansible Project\
**Created:** [March 12, 2015, 8:23pm UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734 "2015-03-12T20:23:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Short](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Short](https://forum.ansible.com/u/Chris_Short)\
**Post date:** [March 12, 2015, 8:23pm UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/1 "2015-03-12T20:23:53Z")

</div>

I am trying to register the output of a shell command, set it as a fact, and then run the playbook only when there is not a match. Example:

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [March 12, 2015, 9:08pm UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/2 "2015-03-12T21:08:52Z")

</div>

You're trying to compare a structure to a string. You probably meant  
to use "git\_installed\_version" in your when conditional. Also, you  
either need to quote the "{{ git\_version }}" in the conditional so  
that the string is quoted, or just leave it off entirely since it's  
using 2 variables, no need to bring templating into it. You'll  
probably want something like this:

- name: Dump git\_installed\_version to file  
&nbsp;&nbsp;debug: msg="success"  
&nbsp;&nbsp;when: git\_installed\_version == git\_version  
&nbsp;&nbsp;tags: dump

---

<div class="post-metadata">

**Author:** ![Chris\_Short](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Short](https://forum.ansible.com/u/Chris_Short)\
**Post date:** [March 13, 2015, 1:36am UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/3 "2015-03-13T01:36:21Z")

</div>

I’m running around in circles I feel. Below is my flattened playbook. Ideally the variable is the version of software I want installed. Everything else is to determine if the version on a system matches the variable or not. The issue is that no matter how I skin this cat the version never matches (it should as the version installed on the system I’m testing against does match the variable). Thoughts?

---

<div class="post-metadata">

**Author:** ![Chris\_Short](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Short](https://forum.ansible.com/u/Chris_Short)\
**Post date:** [March 13, 2015, 12:43pm UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/4 "2015-03-13T12:43:55Z")

</div>

When running with the -v flag I was getting this:

TASK: [Set git\_installed\_version fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [cld-lab0-cshort01] =\> {“ansible\_facts”: {“git\_installed\_version”: “git\_version\_output.stdout\_lines”}}

So clearly something wasn’t right. I tried this:

- name: Set git\_installed\_version fact  
set\_fact: git\_installed\_version={{ git\_version\_output.stdout\_lines }}  
tags: fact1

And then the output seemed to be close to desired:

TASK: [Set git\_installed\_version fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [cld-lab0-cshort01] =\> {“ansible\_facts”: {“git\_installed\_version”: “[‘2.3.2’]”}}

My problem is the JSON’y format of that output. Is there a way to just get the output without the formatting?

---

<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 13, 2015, 1:22pm UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/5 "2015-03-13T13:22:52Z")

</div>

stdout\_lines returns a list/array, that is why you are getting  
['2.3.2'], this is what Micheal was referring to when he mentioned you  
are comparing a string to a structure.

Use stdout.

---

<div class="post-metadata">

**Author:** ![Chris\_Short](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Short](https://forum.ansible.com/u/Chris_Short)\
**Post date:** [March 13, 2015, 1:37pm UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/6 "2015-03-13T13:37:06Z")

</div>

First facepalm of the day recorded at 9:35 AM. Thank you, Brian.

---

<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 16, 2015, 9:37am UTC](https://forum.ansible.com/t/register-shell-output-set-as-custom-fact-and-use-it-as-a-condition-of-the-playbook-running/17734/7 "2015-03-16T09:37:54Z")

</div>

Let me highlight the solution for the posterity 🙂

If you want to register a variable based on a shell script:

> - name: Register git version that is installed on system  
> shell: git --version | sed ‘s/git\ version\ //g’  
> register: git\_version\_output  
> tags: register

You expect that the variable will store the output of the script. But this is not the case  
This wil store a dict/struct that stores many things:  
the script itself, the timestamp of run, the exit value, etc

So if you are interested on simply the output itself, you need to address it like:

> - name: Dump git\_installed\_version to file  
> debug: msg=“success”  
> when: **git\_version\_output.stdout** == {{ git\_version }}  
> tags: dump

Regards:  
Bence

1. március 13., péntek 14:37:06 UTC+1 időpontban Chris Short a következőt írta:
