What does a module return, and how do you use it?
IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer
Every module returns JSON with common keys: changed, failed, msg, rc, stdout, stderr, plus module-specific data. Capture it with register and use it in when, changed_when or later tasks.
- ansible.builtin.command: systemctl is-active nginx
register: nginx_state
failed_when: false
changed_when: false
- ansible.builtin.debug:
msg: "nginx is {{ nginx_state.stdout }} (rc={{ nginx_state.rc }})"
Run with -v to print the returned dictionary, and check the "Return Values" section of ansible-doc for module-specific keys.
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