issue with ansible dry-run mode (--check) on package installation tasks (yum and apt modules)

Hello guys.

I’m here to bring an issue with Ansible dry-run (–check) mode. I have been facing a curious situation where package installation made using Playbooks and run in a dry-run mode is not working as expected. Using register+debug module it’s possible to see that the package has been successfully installed, but when I try to use that installed package in later task, Ansible fails.

My Playbook:

The whole point of check mode is that it doesn’t make changes to the system. If you want things to change (e.g. packages to install), don’t run in check mode.

@flowerysong

The point is, a CI tool such as Travis, Tower… will report error on that task, but actually there is no error at all (normal running). In addition, package installing task is just 0,5% of the whole deployment project and thinking deeper, every task does changes on the systems. Many ansible other modules I have used do work in --check mode, except package manager which I am reporting here.

The yum task succeeded. It’s the subsequent fail that is failing. You can make any other module +fail react in the same way:

  • file: path=/var/tmp/i_do_not_exist_yet state=directory

  • stat:
    path: /var/tmp/i_do_not_exist_yet
    register: file

  • fail:
    msg: “File i_do_not_exist_yet doesn’t exist!”
    when: file.stat.exists == False

As flowerysong says, --check means, do not make any changes to the system. So you cannot run ansible in check-mode and then expect an assertion that a change was made to succeed. Similarly, you cannot expect that running a second task which depends on the first task to have changed the system in some way to succeed in check-mode because the change will not have been made to the system.

-Toshio

Ok guys, I got the point “–check doesn’t make any changes to the system” after making more tests with tasks coupling (Task2 relies on previous executed Task1), --check mode will never work for these cases. Therefore, i will change my test pipeline to discard dry-run mode which has been thought to do a “Functional test”.

Thank you very much for helping me.

j

An alternative to make check work involves some code changes.

You have "check_mode: no" to make a task run in check_mode.
You can also check if Ansible run in check mode to skip some task, "when: not ansible_check_mode"