Ansible Conditional Based on Stdout of Result

Ansible conditional based on stdout of result?

Try checking to see it if equals a blank string or not?

- hosts: myhosts
tasks:
- name: echo hello
command: echo hello
register: result
- command: somecommand {{ result.stdout }}
when: result.stdout != ""
- command: someothercommand
when: result.stdout == ""

Why is Ansible not failing this string in stdout conditional?

When you write:

when: '"new_ios_md5" | string in md5_result.stdout'

You are looking for the literal string "new_ios_md5" inside the variable md5_result.stdout. Since you actually want to refer to the new new_ios_md5 variable, you ened to remove the quotes around it:

when: 'new_ios_md5 | string in md5_result.stdout'

Get exactly data from stdout or stdout_lines with exact words of output

You may have a look into debug – Print statements during execution, Using Variables and Return Values.

---
- hosts: localhost
become: true
gather_facts: false

vars:
RESULT:
STDOUT_LINES:
- "# name admin@shrrah.esquimail.com"
- "zimbraIsDelegatedAdminAccount: FALSE"
- ""
- "# name prueba5@prueba5.com"
- ""
- "# name prueba7@prueba7.com"
- "zimbraIsDelegatedAdminAccount: TRUE"
- ""
- "# name prueba9@prueba9.com"

tasks:

- name: Show STDOUT_LINES
debug:
msg: "{{ RESULT.STDOUT_LINES }}"

resulting into an output only of

TASK [Show STDOUT_LINES] *****************
ok: [localhost] =>
msg:
- '# name admin@shrrah.esquimail.com'
- 'zimbraIsDelegatedAdminAccount: FALSE'
- ''
- '# name prueba5@prueba5.com'
- ''
- '# name prueba7@prueba7.com'
- 'zimbraIsDelegatedAdminAccount: TRUE'
- ''
- '# name prueba9@prueba9.com'

and if Ansible Callback plugin is configured to YAML instead of JSON.

To get lines containing certain strings only you may Loop over the list based on a Condition

  - name: Show lines with TRUE only
debug:
msg: "{{ item }}"
when: "'TRUE' in item"
loop: "{{ RESULT.STDOUT_LINES }}"

resulting into an output of

TASK [Show lines with TRUE only] *******************************
ok: [localhost] => (item=zimbraIsDelegatedAdminAccount: TRUE) =>
msg: 'zimbraIsDelegatedAdminAccount: TRUE'

Further Documenation

  • Index of all Callback Plugins

If you like to have the line before included, you could use an approach like

  - name: Show lines with TRUE and line before
debug:
msg: "{{ RESULT.STDOUT_LINES[ansible_loop.index0 - 1] }}\n{{ item }}"
when: "'TRUE' in item"
loop: "{{ RESULT.STDOUT_LINES }}"
loop_control:
extended: true
label: "{{ ansible_loop.index0 }}"

resulting into an output of

TASK [Show lines with TRUE and line before] *************************************************************************************************************************************
ok: [localhost] => (item=6) =>
msg: |-
# name prueba7@prueba7.com
zimbraIsDelegatedAdminAccount: TRUE

Further Documentation

  • Extended loop variables

Since you are using the shell module, you could use also an approach like

- name: DELEGATED ADMIN ACCOUNTS - check, get and send to the file domain.list
shell:
cmd: /opt/zimbra/bin/zmprov -l gaaa -v zimbraIsDelegatedAdminAccount | grep -B 1 TRUE

and gather only result lines which are true an the line before.

Further Q&A

  • grep a file, but show several surrounding lines?

Regarding

... send it to the file.txt

you may have a look into

  • Ansible - Save registered variable to file
  • Ansible: Save registered variables to file
  • ...

Ansible stdout lines result without brackets and quotes

Short answer

Use stdout instead of stdout_lines and use yaml callback instead of default. Then

    - debug:
msg: "{{ sys_info.stdout }}"

gives what you want

  msg: |-
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

Details


Given the file

shell> cat info.txt
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

and the playbook

shell> cat test.yml
- hosts: localhost
tasks:
- name: Tested unit information
command: cat info.txt
register: sys_info
- debug:
msg: "{{ sys_info.stdout_lines }}"


What you see depends on the DEFAULT_STDOUT_CALLBACK. The default callback format is JSON

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => {
"msg": [
"Serial Number: Test12345",
"Technician Name: Team",
"Custom Serial Number: Test12345",
"System Part Number: xxxx",
"Rev: A10",
"Last Rev: A10",
"Paka Number: Auto",
"Ip Address: 1.1.1.1"
]
}

If you want to create plain text from the list use Jinja, e.g.

    - debug:
msg: |-
{% for line in sys_info.stdout_lines %}
{{ line }}
{% endfor %}

, or simply join the lines

    - debug:
msg: "{{ sys_info.stdout_lines|join('\n') }}"

Both options give the same result

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => {
"msg": "Serial Number: Test12345\nTechnician Name: Team\nCustom Serial Number: Test12345\nSystem Part Number: xxxx\nRev: A10\nLast Rev: A10\nPaka Number: Auto\nIp Address: 1.1.1.1\n"
}

Use callback yaml to get what you want

shell> ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] =>
msg: |-
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1



Related Topics



Leave a reply



Submit