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.
More on Write-an-Ansible-playbook tasks
Write a playbook that installs and starts Nginx on Ubuntu web servers.BeginnerWrite an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.Advanced