AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible run_once: Execute Task on Single Host (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: database-automation

How to use Ansible run_once to execute tasks on a single host. Database migrations, token generation, delegation patterns, and common pitfalls explained.

Introduction Ansible is a powerful automation tool that makes managing IT infrastructure easier. One of its lesser-known but highly useful directives is “run_once.” While using it at the play level might seem unremarkable, its true potentials reveals itself when employed within a task. Let’s explore the wonders of run_once and how it can simplify complex automation scenarios.

Play Level At the play level, using run_once is equivalent to changing the host selection. Instead of specifying hosts as "foo” you’d use “foo[0]”. This makes it convenient for directing tasks to a specific host but doesn’t necessarily elicit much excitement.

However, the real enchantment occurs when you use run_once within a task. When you do this, tasks with run_once are executed on a single host, typically the first host in the runlist. This simple feature allows for intricate choreography. For instance, you can send a command to a cluster only once, even if there are multiple equally available hosts in the cluster group. Furthermore, with the ability to continue executing playbooks even when some hosts are unavailable, you can ensure your command reaches the first available node in the cluster. It’s very useful in its utility.

Task Level What happens when you combine run_once with the “register” directive? You might wonder if the variable for hosts where the command wasn’t run remains undefined or contains some “skipped” message, as can occur with other combinations of directives like “when” and “register.”

In this case, Ansible behaves in the best way imaginable. The registered variable is created for all hosts in the runlist, even if the task was executed on a single host. When you combine it with delegation, it becomes even more elegant.

One of the best applications of this feature is in gathering “pseudo-facts.” For example:

Previously, you might have added this to the play without concerns. While it’s slightly inefficient, it’s concise and straightforward. Now, it becomes a real trick that dramatically reduces the number of calls to the delegated host.

Pseudo-facts Imagine a playbook with multiple hosts, and you want to set a fact using run_once:

What do you think will happen to “foo” for all hosts in the play? It’s not just magic; it’s sheer brilliance. All hosts will have “foo: 42”. The primary difference from a version lacking run_once is the reduction in output. This feature is especially valuable when working with loops and other complex automation constructs, making it a fantastic way to streamline your Ansible playbooks.

Conclusion In conclusion, the “run_once” directive in Ansible might seem simple at first, but it holds incredible power. Whether you want to send a command to a single host in a group, efficiently gather “pseudo-facts,” or streamline your playbook output, run_once is a wonderful tool that every Ansible user should be aware of. It Playbooknstrates how the simplest features can bring about the most powerful automation results.

Basic run_once

run_once with delegate_to

Common Use Cases

Database migration

Cluster initialization

Generate shared secret

Download artifact once

run_once Behavior

| Aspect | Behavior | |--------|----------| | Which host? | First in ansible_play_hosts | | Registered vars? | Available on ALL hosts | | With serial? | Runs once per batch | | With delegate_to? | Runs on delegated host | | With when? | Condition evaluated on first host |

run_once with serial

FAQ

Does run_once skip the task on other hosts?

The task executes only on the first host, but registered variables are available on all hosts in the play.

run_once vs delegate_to?

run_once controls how many times. delegate_to controls where. Combine them: run_once: true + delegate_to: specific-host.

How do I run on a specific host, not the first?

Or use delegate_to with run_once.

Basic run_once

run_once + delegate_to

Common Use Cases

Database Migrations

Generate Shared Token

Download Once, Distribute

Send Notification Once

run_once with serial

Registered Variables

run_once vs when: inventory_hostname == ...

FAQ

Which host does run_once pick?

The first host in the current play's host list (after inventory sorting and limits).

Does run_once work with handlers?

Yes, but the handler fires on every host that notified it. Use run_once: true` on the handler too if needed.

Can I use run_once in roles?

Yes — it works in any task context (plays, roles, includes, blocks).

Related ArticlesAnsible Template GuideAnsible Become GuideAnsible set_fact GuideAnsible Loops Guide

Category: database-automation

Browse all Ansible tutorials · AnsiblePilot Home