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

Write a task that runs a command only if a file doesn't exist, without breaking idempotency.

BeginnerHands-onWrite-an-Ansible-playbook tasks
Answer
- name: Initialise the app database once
  ansible.builtin.command: /opt/app/bin/init-db
  args:
    creates: /var/lib/app/.db_initialised

Key trick → command and shell always report changed. creates: (or removes:) makes them skip when the marker exists, so the task is idempotent and reports correctly.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-an-Ansible-playbook tasks