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:
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
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?
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?
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.
First facepalm of the day recorded at 9:35 AM. Thank you, Brian.
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
- március 13., péntek 14:37:06 UTC+1 időpontban Chris Short a következőt írta: