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

18,589 views
Skip to first unread message

Chris Short

unread,
Mar 12, 2015, 4:23:53 PM3/12/15
to ansible...@googlegroups.com
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:

---
- name: Register git version that is installed on system
  shell: git --version | sed 's/git\ version\ //g'
  register: git_version_output
  tags: register

- name: Set git_installed_version fact
  set_fact: git_installed_version=git_version_output.stdout_lines
  tags: fact

- name: Dump git_installed_version to file
  debug: msg="success"
  when: git_version_output == {{ git_version }}
  tags: dump

But, I'm getting the following error:

error while evaluating conditional: git_version_output == 2.3.2

I'm not quite sure if the syntax is right and I'm completely lost on how to make the playbook run/not run if the conditional is met/not met.

Michael Peters

unread,
Mar 12, 2015, 5:08:55 PM3/12/15
to ansible...@googlegroups.com
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
debug: msg="success"
when: git_installed_version == git_version
tags: dump
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/941d93cb-bb5a-4f08-a441-7a05d3e0055b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chris Short

unread,
Mar 12, 2015, 9:36:21 PM3/12/15
to ansible...@googlegroups.com
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?

---
- name: Install compiled from source git RPMs
  hosts: git_whs_dev
  vars:
    git_version: 2.3.2
  tasks:
    - name: Register git version that is installed on system
      shell: git --version | sed 's/git\ version\ //g'
      register: git_version_output
      tags: register

    - name: Set git_installed_version fact
      set_fact: git_installed_version=git_version_output.stdout_lines
      tags: fact1

    - name: Set git_version fact
      set_fact: desired_git_version="{{ git_version }}"
      tags: fact2

    - name: Message Success if Versions DO NOT Match
      debug: msg="not equal"
      when: git_installed_version != desired_git_version
      tags: dump

Chris Short

unread,
Mar 13, 2015, 8:43:55 AM3/13/15
to ansible...@googlegroups.com
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?

Brian Coca

unread,
Mar 13, 2015, 9:22:55 AM3/13/15
to ansible...@googlegroups.com
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.
--
Brian Coca

Chris Short

unread,
Mar 13, 2015, 9:37:06 AM3/13/15
to ansible...@googlegroups.com
First facepalm of the day recorded at 9:35 AM. Thank you, Brian.

Bence Takács

unread,
Mar 16, 2015, 5:37:54 AM3/16/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages