Ansible Pilot

Mastering Time in Ansible: An Introduction to the now() Function

Effortlessly Schedule Tasks and Manage Timings with Ansible’s now() Function

Effortlessly schedule tasks and manage timings with Ansible’s now() function. Learn to master time in Ansible.
March 7, 2024
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons


In the realm of IT automation, timing and scheduling tasks are crucial for managing systems efficiently. Ansible, a powerful automation tool, facilitates this through various functions, including the now() function, which is an essential tool for fetching the current time within playbooks. This function, introduced in Ansible 2.8, utilizes the Jinja2 templating engine to retrieve a Python datetime object or a string representation of the current time, providing flexibility and precision in timing operations. This article will explore the now() function in detail, highlighting its usage and practical applications within Ansible automation tasks.

Overview of the now() Function

The now() function is a Jinja2 extension that Ansible playbooks can use to obtain the current time. This function is incredibly versatile, supporting arguments to adjust the time zone to UTC and to format the datetime string according to specific needs.

Arguments Supported by now()

Practical Examples

Let’s delve into some practical examples of how the now() function can be used in Ansible playbooks.

Example 1: Fetching Current Time in UTC

To get the current time in UTC and format it into a readable string, you can use the now() function as follows:

- name: Get current time in UTC
    msg: "Current time (UTC): {{ now(utc=true, fmt='%Y-%m-%d %H:%M:%S') }}"

This example demonstrates fetching the current UTC time and formatting it in the YYYY-MM-DD HH:MM:SS format, which is commonly used for logging and time-stamping tasks.

Example 2: Displaying Host Uptime

Ansible’s now() function can also be used to calculate and display a host’s uptime in a human-readable format, such as days, hours, minutes, and seconds. Assuming facts have been gathered beforehand, you can use the now() function in combination with the host’s uptime information:

- name: Show the uptime in days/hours/minutes/seconds
    msg: "Uptime {{ now().replace(microsecond=0) - now().fromtimestamp(now(fmt='%s') | int - ansible_uptime_seconds) }}"

This playbook snippet calculates the uptime by subtracting the host’s uptime in seconds from the current time, resulting in a timedelta object that represents the host’s uptime duration.


The now() function in Ansible is a powerful tool for managing time-dependent automation tasks. By allowing precise control over timing and formatting, it enables Ansible users to schedule tasks, generate time-stamped logs, and perform time-based calculations within their automation workflows. Whether you need to display the current time in UTC, format dates and times for reporting, or calculate uptime durations, the now() function offers a straightforward and efficient solution within Ansible’s automation toolkit.

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


Learn the Ansible automation technology with some real-life examples in my

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

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases