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

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.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency