A database migration with `run_once: true` ran four times in a rolling deploy with `serial: 25%`
AdvancedTroubleshootingVariables, Facts and Precedence Problems
Answer
Likely causes → run_once means once per batch. With serial, each batch is a separate pass through the play, so the task runs once per batch.
Fix → Move one-time steps into their own play before the rolling play:
- name: Migrate once
hosts: app[0]
tasks:
- ansible.builtin.command: /opt/app/bin/migrate
- name: Rolling deploy
hosts: app
serial: "25%"
roles: [app]
Or guard the task with when: inventory_hostname == ansible_play_hosts_all[0].
Prevent → Make migrations idempotent (track a version in a migrations table), so that a repeated run is harmless anyway.
More on Variables, Facts and Precedence Problems
Variables in `group_vars/webservers.yml` are silently ignoredBeginnerYou set `nginx_port: 8443` in inventory `group_vars`, but the role still uses `80`Intermediate`set_fact: release_tag=v2` runs, but later tasks still see `v1`Intermediate`'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'` when templating `hostvars['db01']`Intermediate`'ansible_eth0' is undefined` on new cloud instancesBeginnerFacts like `ansible_hostname` became undefined after someone changed `ansible.cfg`Intermediate