Conditional problems with Ansible 2.5.0

I have the following steps in a playbook:

  • name: check if .net 4.7.1 is installed
    script: check_net_version.ps1 “4.7.1”
    register: net471result
    changed_when: false

  • name: Install .Net 4.7.1
    win_chocolatey:
    name: dotnet4.7.1
    register: net471_exit_code
    failed_when: net471_exit_code.rc != 0 and net471_exit_code.rc != 3010
    when: ‘“missing” in net471result.stdout’

  • name: reboot node if needed
    win_reboot:
    when: net471_exit_code.rc is defined and net471_exit_code.rc == 3010 and allow_reboot is defined and (allow_reboot | bool)

Check if software is missing (using a custom script because some weirdness), install if it is.
This has worked fine for us in Ansible 2.3.x which we’ve been on until now, but this fails in 2.5.0. Even if I add a check for making sure the “net471_exit_code” var exists altogether, it fails:

“msg”: “The conditional check ‘net471_exit_code.rc != 0 and net471_exit_code.rc != 3010’ failed. The error was: error while evaluating conditional (net471_exit_code.rc != 0 and net471_exit_code.rc != 3010): ‘dict object’ has no attribute ‘rc’”

It seems to me that there’s some bug in how Ansible evaluates object attributes (or dict keys), so that “net471_exit_code.rc is defined” is picked up as a “true” instead of a “false”.

Anyone able to repro?

I’ve also tried the “dict-format”:

net471_exit_code[‘rc’] is defined

But the same thing happens. I almost get the feeling that Ansible evaluates the entire “chain”, which is a problem with potentially undefined variable (that’s why we test the “ is defined” first, to make the evaluation halt).

If this doesn’t work anymore I’m a bit stumped as to how to build complex conditionals.

I think this is because win_chocolatey doesn't return rc anymore so maybe the bug lies there.
You could check by run win_chocolatey register and print the result.

I can reproduce this by replacing win_chocolatey with debug that does not return rc.
But if I use command module that do return rc it works.

Thanks Kai,

Good point - I may have been looking at this all wrong, and not able to repro just playing around with basic set_fact/conditionals playbooks. I’ll keep researching :slight_smile:

thanks again!

about win_chocolatey: It’s strange that it doesnt emit an “rc” anymore, it certainly doesn’t match the documentation:
http://docs.ansible.com/ansible/2.5/modules/win_chocolatey_module.html

oh, it only emits it on change. Not sure how I feel about that.