Learning Ansible
The run_once
parameter can be set on any task. It will ensure that a task is run only once, even when multiple hosts are in scope.
For example, if you are running a playbook on multiple nodes, but you need to run one specific task just once on a host, you can use run_once
. This can be used to select a single host to run a task on, for example to run a task on one node of a cluster.
- name: Install a cluster
hosts: cluster_a
become: yes
tasks:
- name: Check something on one node
ansible.builtin.assert:
that:
- ansible_distribution == "Ubuntu"
run_once: true
- name: Do something to all nodes
ansible.builtin.debug:
msg: "I'm doing something to all nodes."
Use-cases for run_once
:
localhost
.Write a playbook that:
localhost
once.