Running a task only once

At times, a specific task in a role may need to be executed only once during a playbook execution, even though the role is applied to many hosts. This could be achieved with the run_once conditional:

name: initialize wordpress database
script: initialize_wp_database.sh 
run_once: true

Since we are using the run_once option, the preceding task would run on the first host in the inventory that the role is being applied to. All consequent hosts would skip this task.

Get Ansible Playbook Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.