ansible stdout

Is there a way to extract stdout variables from Ansible?

Here is my script:

  • hosts: myhost
    sudo: yes
    tasks:

  • name: output bash
    command: rpm -q bash
    register: result

  • debug: msg=“{{ result.stderr }}”

Here is the output:

[root@ansi playbooks]$ ansible-playbook which_bash.yml

PLAY [myhost] ***************************************************

TASK [setup] *******************************************************************
ok: [myhost]

TASK [which bash RPM] **********************************************************
changed: [myhost]
[WARNING]: Consider using yum module rather than running rpm

TASK [debug] *******************************************************************
ok: [myhost] => {
“bash_version”: {
“changed”: true,
“cmd”: [
“rpm”,
“-q”,
“bash”
],
“delta”: “0:00:00.045905”,
“end”: “2016-06-11 18:22:07.782431”,
“rc”: 0,
“start”: “2016-06-11 18:22:07.736526”,
“stderr”: “”,
“stdout”: “bash-4.2.46-12.el7.x86_64”,
“stdout_lines”: [
“bash-4.2.46-12.el7.x86_64”
],
“warnings”: [
“Consider using yum module rather than running rpm”
]
}
}

PLAY RECAP *********************************************************************
myhost : ok=3 changed=1 unreachable=0 failed=0

The output works great. However I’m looking or a way to only extract the stdout from that output. I want nothing but stdout. How can I do that?

thanks in advance.

I’m not convinced your output matches your playbook.

Are you sure you’re running it correctly ?

Your output:

TASK [debug] ******************************************************************* ok: [myhost] => { "bash_version": { etc...

… implies you are using the debug module with a variable called bash_version, e.g.
`

  • name: output bash
    command: rpm -q bash
    register bash_version

  • debug: var=bash_version
    `

Which is not the same as the playbook you’ve quoted.

BTW, to get the stdout, you are nearly correct.

Use:

`

  • debug: msg=“{{ result.stdout }}”
    `