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.
More on Ad-hoc Commands, Modules and Idempotency
What is an ad-hoc command and what is its syntax?BeginnerWhat does `ansible all -m ping` actually test?BeginnerGive practical use cases for ad-hoc commands.IntermediateWhat is the difference between the `command` and `shell` modules?BeginnerCompare `raw`, `command`, `shell` and `script`.IntermediateWhy prefer a dedicated module over `shell`/`command`?Beginner