Interview Prep/Linux/Patching, Automation & Golden Images

How do you make an Ansible task idempotent?

IntermediateProductionPatching, Automation & Golden Images
Answer

Use modules that describe desired state (package, template, lineinfile, systemd, user) instead of shell/command. When you must run a command, guard it with creates:/removes: or changed_when:/when:.

- name: Initialize app DB once
  ansible.builtin.command: /opt/app/bin/init-db
  args:
    creates: /var/lib/app/.initialized

Run twice; the second run must report changed=0.

All Linux questions Roadmap: Linux

More on Patching, Automation & Golden Images