Interview Prep/Ansible/Ad-hoc Commands, Modules and Idempotency

How does the `stat` module work with `register` to control flow?

IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer

stat returns facts about a path without changing it. The key is stat.exists, plus isdir, mode, checksum and so on.

- ansible.builtin.stat:
    path: /etc/app/license.key
  register: lic

- ansible.builtin.fail:
    msg: "License missing"
  when: not lic.stat.exists

Prefer module-native idempotency (creates, state parameters) when it exists, and use stat for decisions that modules cannot make for you. Remember stat follows no symlinks unless follow: true.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency