Bootstrap FreeKB - Ansible - --check (dry run) command line flag
Ansible - --check (dry run) command line flag

Updated:   |  Ansible articles

There are two ways to determine if plays in a playbook will be successful, like a dry run, meaning that no changes will be made on the target systems.

The --check flag will check every tasks in each play in a playbook. The check_mode parameter will only check certain tasks.

Let's say you've a playbook named example.yml that is using lineinfile to append "Hello World" to /tmp/foo.txt, like this.

---
- hosts: all
  tasks:
  - name: append 'Hello World' to /tmp/foo.txt
    ansible.builtin.lineinfile:
      path: /tmp/foo.txt
      line: Hello World
    check_mode: yes
...

 

Here is how to use the --check flag with the ansible-playbook command.

ansible-playbook example.yml --check

 

Something like this should be returned.

TASK [append 'Hello World' to /tmp/foo.txt]
changed: [server1.example.com]

PLAY RECAP
server1.example.com : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

On the target system, "Hello World" would NOT have been appended to /tmp/foo.txt. Now let's invoke the ansible-playbook command without the --check flag.

ansible-playbook foo.yml

 

This should produce the same exact output, which is something important to be aware of. The --check flag will not change the output produced by the ansible-playbook command. However, now, "Hello World" should have been appended to /tmp/foo.txt on the target system. Just like the --check flag, check_mode will not change the output produced by the ansible-playbook command.

TASK [append 'Hello World' to /tmp/foo.txt]
changed: [server1.example.com]

PLAY RECAP
server1.example.com : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

Be aware that some modules, such as the uri module, do not support the --check flag. For modules that do not support check mode, the module can be skipped by including when not ansible_check_mode.

---
- hosts: all
  tasks:
  - name: determine if foo.txt exists
    ansible.builtin.uri:
      url: http://example.com/foo.txt
    when: not ansible_check_mode
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 05a998 in the box below so that we can be sure you are a human.