Using check mode

Although you might be confident in the code you have written, it still pays to test it before running it for real in a production environment. In such cases, it is a good idea to be able to run your code, but with a safety net in place. This is what check mode is for. Follow these steps:

  1. First of all, we need to create an easy playbook to test this feature. Let's create a playbook called check-mode.yaml that contains the following content:
---
- hosts: localhost
tasks:
- name: Touch a file
file:
path: /tmp/myfile
state: touch
  1. Now, we can run the playbook in the check mode by specifying the --check option in the invocation:
$ ansible-playbook check-mode.yaml --check

This will output everything as if it was really performing the operation, as follows:

PLAY [localhost] **********************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [localhost]

TASK [Touch a file] *******************************************************************************
ok: [localhost]

PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

However, if you look in /tmp, you won't find myfile.

Ansible check mode is usually called a dry run. The idea is that the run won't change the state of the machine and will only highlight the differences between the current status and the status declared in the playbook.

Not all modules support check mode, but all major modules do, and more and more modules are being added at every release. In particular, note that the command and shell modules do not support it because it is impossible for the module to tell what commands will result in a change, and what won't. Therefore, these modules will always return changed when they're run outside of check mode because they assume a change has been made. 

A similar feature to check mode is the --diff flag. What this flag allows us to do is track what exactly changed during an Ansible execution. So, let's say we run the same playbook with the following command:

$ ansible-playbook check-mode.yaml --diff

This will return something like the following:

PLAY [localhost] **********************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [localhost]

TASK [Touch a file] *******************************************************************************
--- before
+++ after
@@ -1,6 +1,6 @@
{
- "atime": 1560693571.3594637,
- "mtime": 1560693571.3594637,
+ "atime": 1560693571.3620908,
+ "mtime": 1560693571.3620908,
"path": "/tmp/myfile",
- "state": "absent"
+ "state": "touch"
}

changed: [localhost]

PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

As you can see, the output says changed, which means that something was changed (more specifically, the file was created), and in the output, we can see a diff-like output that tells us that the state moved from absent to touch, which means the file was created. mtime and atime also changed, but this is probably due to how files are created and checked.

Now that you have learned how to use check mode, let's learn how to solve host connection issues.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.231