Search for string in stdout for failed_when

11 views
Skip to first unread message

JP-OKC

unread,
Aug 27, 2019, 3:10:20 PM8/27/19
to Ansible Project
how can I pull a string from the stdout and use it for failed_when?    I run a command on a switch and then I want to match a string w/in that output to determine if it failed or not.

Here's what the playbook looks like:

---
- name: Check the ssh keypair on a Nexus 9k
  hosts: all
  gather_facts: false
  connection: network_cli
  tasks:
  - block:
    - name: show keypair
      nxos_command:
        commands: "show username someuser keypair"
      ignore_unreachable: true
      register: print_output
      failed_when: ("could not retrieve rsa key information" in print_output.stdout)
  - debug: var=print_output





Here's what the debug output looks like.    I want to match on the 3rd line of the stdout_lines.

ok: [switchB] => {
    "print_output": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "failed_when_result": false,
        "stdout": [
            "**************************************\n\ncould not retrieve rsa key information\n**************************************\n\ncould not retrieve dsa key information\n**************************************\n\ncould not retrieve ecdsa key information\n**************************************"
        ],
        "stdout_lines": [
            [
                "**************************************",
                "",
                "could not retrieve rsa key information",
                "**************************************",
                "",
                "could not retrieve dsa key information",
                "**************************************",
                "",
                "could not retrieve ecdsa key information",
                "**************************************"
            ]
        ]
    }
}

Kai Stian Olstad

unread,
Aug 27, 2019, 3:22:37 PM8/27/19
to ansible...@googlegroups.com
As you can see here the [ indicate a list so first element is 0.
The nxos_command takes several commands, the first command is in list element 0, the second one is list element 1 and so on....

so you need to use element 0 like this

failed_when: ("could not retrieve rsa key information" in print_output.stdout.0)


--
Kai Stian Olstad

JP-OKC

unread,
Aug 27, 2019, 3:54:34 PM8/27/19
to Ansible Project
Thanks Kai,   That worked.
Reply all
Reply to author
Forward
0 new messages