Handling check mode

Detecting when check mode is active is very easy. The module object will have a check_mode attribute, which will be set to Boolean value true when check mode is active. In our module, we want to detect whether check mode is active before performing the copy. We can simply move the copy action into an if statement to avoid copying when check mode is active. No further changes to the module are necessary beyond this:

    if not module.check_mode: 
        shutil.copy(module.params['source'], 
                    module.params['dest']) 

Now, we can run our playbook and add the -C argument to our execution. This argument engages check mode. We'll also test to ensure that the playbook did not actually create and copy the files. Let's take a look at the following screenshot:

Although the module output looks as though it created and copied files, we can see that the files referenced did not exist before execution and still do not exist after execution.