Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible -- failed_when not working as expected

Trying to run a simple failed_when check::

  - name: JunOS Commands
    junos_command:
     commands:
     - show version
     provider:
      username: zgould
    register: results
    failed_when: '"[12.3R12.4]" in results.stdout'

! ! Debug Output::

"stdout": [
    "fpc0:\n--------------------------------------------------------------------------\nHostname: KC2-SWI-SalesCloset\nModel: ex2200-48p-4g\nJUNOS Base OS boot [12.3R12.4]\nJUNOS Base OS Software Suite [12.3R12.4]\nJUNOS Kernel Software Suite [12.3R12.4]\nJUNOS Crypto Software Suite [12.3R12.4]\nJUNOS Online Documentation [12.3R12.4]\nJUNOS Enterprise Software Suite [12.3R12.4]\nJUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R12.4]\nJUNOS Routing Software Suite [12.3R12.4]\nJUNOS Web Management [12.3R12.4]\nJUNOS FIPS mode utilities [12.3R12.4]"
],
"stdout_lines": [
    [
        "fpc0:",
        "--------------------------------------------------------------------------",
        "Hostname: KC2-SWI-SalesCloset",
        "Model: ex2200-48p-4g",
        "JUNOS Base OS boot [12.3R12.4]",
        "JUNOS Base OS Software Suite [12.3R12.4]",
        "JUNOS Kernel Software Suite [12.3R12.4]",
        "JUNOS Crypto Software Suite [12.3R12.4]",
        "JUNOS Online Documentation [12.3R12.4]",
        "JUNOS Enterprise Software Suite [12.3R12.4]",
        "JUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R12.4]",
        "JUNOS Routing Software Suite [12.3R12.4]",
        "JUNOS Web Management [12.3R12.4]",
        "JUNOS FIPS mode utilities [12.3R12.4]"
    ]
]

}

I think it would be simple enough, that if the Version appears in the std.out, then it should fail.

Is this now how it's supposed to work?

I even tried using the string "Peanuts" and it still passed...

like image 871
Zachary Gould Avatar asked Nov 30 '25 16:11

Zachary Gould


2 Answers

According to your debug output, stdout is actually an array.

You could change the statement to something like:

failed_when: '"[12.3R12.4]" in results.stdout[0]'

My guess is that since junos_command accepts several commands, it will place the output of each in an stdout array, so if you specify two commands the second output would be accessible as results.stdout[1]

like image 179
Victor Jerlin Avatar answered Dec 02 '25 06:12

Victor Jerlin


This answer properly identifies the cause of the error, but the underlying reason is the fact that you provide a single-element list to commands argument:

commands:
 - show version

For your use case, you can simply replace it with:

commands: show version

Which will fix the problem without the need to change the condition in failed_when.

like image 40
techraf Avatar answered Dec 02 '25 06:12

techraf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!