Interview Prep/Real-World Interview/Write-an-Ansible-playbook tasks

Register output of a command and use it in a later task with a condition.

IntermediateHands-onWrite-an-Ansible-playbook tasks
Answer
- name: Check current app version
  ansible.builtin.command: /opt/app/bin/app --version
  register: app_ver
  changed_when: false
  failed_when: false

- name: Upgrade if outdated
  ansible.builtin.include_tasks: upgrade.yml
  when: app_ver.rc != 0 or target_version not in app_ver.stdout

Key trick → changed_when: false marks read-only checks as unchanged. failed_when: false lets you handle a missing binary (rc != 0) yourself instead of failing the play.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-an-Ansible-playbook tasks