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.
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