Introduction

In IT infrastructure management, automation is a key component that empowers administrators to streamline repetitive tasks and ensure consistent operations. Ansible, a popular open-source automation tool, offers extensive capabilities for orchestrating tasks across a wide range of systems. One intriguing aspect of Ansible is its ability to execute tasks at specific times, enabling administrators to schedule actions with precision. This article will explore a practical example of how Ansible can execute tasks at a predetermined time, specifically at 10:55.

The Power of Scheduled Execution

Imagine a scenario where a system administrator needs to perform a task on a fleet of servers every day at exactly 10:55. This task might involve updating configurations, performing backups, or any other action necessary to maintain system health and security. Manually executing such tasks can be time-consuming, error-prone, and disruptive, especially in a large-scale environment.

Ansible addresses this challenge by allowing administrators to define and schedule tasks for execution at specific times. This reduces the required manual effort and ensures consistency and accuracy in task execution.

Join 50+ hours of courses in our exclusive community

Understanding the Playbook

To Playbooknstrate scheduled execution with Ansible, we will walk through an Ansible playbook that accomplishes the following:

  1. Calculates the time remaining until 10:55.
  2. Displays the current datetime, target datetime (10:55), and time remaining in seconds.
  3. Pauses the playbook execution until the specified time (10:55) is reached.
  4. Displays a message indicating the completion of the wait period.

Here’s the Ansible playbook for achieving this:

---
- name: Execute at 10:55
  hosts: all
  vars:
   var_wait_time: "10:55:00"
   var_wait_datetime: "{{ ansible_date_time.year }}-{{ ansible_date_time.month }}-{{ ansible_date_time.day }} {{ var_wait_time }}"
   var_current_datetime: "{{ ansible_date_time.date }} {{ ansible_date_time.time }}"
  tasks:
    - name: Calculate the seconds to pause
      ansible.builtin.set_fact:
       var_wait_seconds: "{{ ((var_wait_datetime | to_datetime) - (var_current_datetime | to_datetime)).total_seconds() }}"
      delegate_to: localhost
    - name: Show the datetimes and seconds to pause
      ansible.builtin.debug:
       msg: "{{ var_wait_datetime }} :: {{ var_current_datetime }} :: {{ var_wait_seconds }}"
      delegate_to: localhost
    - name: Pause for {{ var_wait_seconds }} seconds
      ansible.builtin.wait_for:
        timeout: "{{ var_wait_seconds | int }}"
      when: var_wait_seconds | int > 0
    - name: Display message
      ansible.builtin.debug:
        msg: "Waited {{ var_wait_seconds }} seconds"

This playbook showcases using Ansible’s datetime manipulation functions, variable calculations, and the wait_for module to achieve the desired scheduled execution.

Execution

ansible-playbook -i inventory time.yml

Output

PLAY [Execute at 10:55] *****************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [localhost]

TASK [Calculate the seconds to pause] ***************************************************
ok: [localhost]

TASK [Show the datetimes and seconds to pause] ******************************************
ok: [localhost] => {
    "msg": "2023-08-21 10:55:00 :: 2023-08-21 10:53:29 :: 91.0"
}

TASK [Pause for 91.0 seconds] ***********************************************************
ok: [localhost]

TASK [Display message] ******************************************************************
ok: [localhost] => {
    "msg": "Waited 91.0 seconds"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Conclusion

Automating tasks through Ansible’s scheduled execution capabilities gives administrators a powerful tool for maintaining systems efficiently and consistently. Administrators can adapt and extend this functionality by using the provided playbook as a template to suit their specific requirements. Whether performing routine maintenance, deploying updates, or any other task, Ansible’s automation capabilities help streamline operations and free up valuable time for IT teams to focus on more strategic activities.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza