synchronized-start.yaml
Overview
synchronized-start.yaml is an automation playbook designed to coordinate the timing of operations by synchronizing the start time across multiple hosts or processes. It uses timestamp comparison and conditional waiting to ensure that subsequent tasks execute simultaneously or after a predetermined synchronized timestamp (SYNCHRONIZED_START). This is important in scenarios requiring coordinated execution, such as load testing, batch processing, or distributed orchestration.
Playbook Tasks and Their Functionality
The file consists of three main tasks executed in sequence, each running only once (due to the run_once: true directive) to avoid redundant operations on multiple hosts:
1. Get current timestamp
- name: Get current timestamp
ansible.builtin.shell: date +%s
register: current_timestamp
run_once: true
Purpose:
Executes a shell command to retrieve the current Unix timestamp (seconds since the Unix epoch) on the control node or the first host.Key Points:
Uses the
date +%scommand to get an integer timestamp.The result is stored in the variable
current_timestamp, which can be referenced in subsequent tasks.run_once: trueensures this command is executed a single time and not on all hosts.
Usage Example:
This timestamp serves as a reference point for calculating how long to wait until the synchronized start time.
2. Print timestamps
- name: Print timestamps
debug:
msg: "Current timestamp: {{ current_timestamp.stdout }}, Synchronized start: {{ SYNCHRONIZED_START }}, need to wait {{ SYNCHRONIZED_START - current_timestamp.stdout | int }} seconds"
run_once: true
Purpose:
Outputs diagnostic information that includes:The current timestamp obtained above.
The target synchronized start time (
SYNCHRONIZED_START), which must be provided externally (e.g., as an extra variable or inventory variable).The computed wait time (difference between
SYNCHRONIZED_STARTandcurrent_timestamp).
Parameters:
Uses Jinja2 templating to interpolate values into the debug message.
The subtraction operation converts the timestamps to integers before calculation.
Context:
Useful for verifying that the synchronization logic is working as expected before the actual wait.
3. Wait for synchronized start
- name: Wait for synchronized start
run_once: true
ansible.builtin.pause:
seconds: "{{ SYNCHRONIZED_START - current_timestamp.stdout | int }}"
when:
- current_timestamp.stdout | int < SYNCHRONIZED_START
Purpose:
Pauses execution until the synchronized start time is reached, effectively delaying further playbook tasks or roles.Implementation Details:
Uses Ansible's
pausemodule to halt execution for a calculated number of seconds.The pause duration is the difference between the target synchronized start time and the current timestamp.
The
whenconditional ensures that the pause only happens if the current time is earlier than the target start time to avoid negative pauses.
Practical Use:
Ensures that all hosts or processes begin their subsequent operations at the same global time.
Important Implementation Details
The playbook relies on the external provision of the
SYNCHRONIZED_STARTvariable. This should be an integer representing a Unix timestamp in the future.The
run_once: truedirective is critical to avoid redundant timestamp fetching and waiting logic on multiple hosts.The calculation
SYNCHRONIZED_START - current_timestamp.stdoutuses Jinja2 filters to ensure integer arithmetic.If the current time is already past the synchronized start time, no waiting occurs.
Interaction With Other System Components
This playbook is typically integrated as a prelude or synchronization step within a larger automation workflow or orchestration pipeline.
It depends on upstream components or user input to define the
SYNCHRONIZED_STARTtimestamp.Downstream tasks triggered after this playbook will execute in a synchronized manner relative to the centralized start time.
It may be combined with distributed workload execution, where coordinated timing is crucial for correctness or performance measurement.
Visual Diagram
flowchart TD
A[Get current timestamp] --> B[Print timestamps]
B --> C{Is current time < synchronized start?}
C -- Yes --> D[Pause until synchronized start]
C -- No --> E[Proceed immediately]
Explanation:
This flowchart illustrates the sequential workflow:Retrieve the current timestamp.
Output timing information.
Conditionally wait if the current time is earlier than the synchronized start.
Otherwise, proceed without delay.
Note: For detailed understanding of playbook execution and Ansible modules used, refer to [Ansible Playbooks](/ansible-playbooks) and [Ansible Modules](/ansible-modules). For timestamp management concepts, see [Unix Timestamp](/unix-timestamp) and [Time Synchronization](/time-synchronization).