Ansible Pilot

Automating Dynamic Time Date Facts with Ansible

How to use the ansible.builtin.set_fact module and lookup plugin to set Ansible date.

February 26, 2024
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons


In the realm of automation and infrastructure management, Ansible stands out for its simplicity and power, enabling IT professionals to automate a wide array of tasks efficiently. One of the lesser-discussed yet powerful features of Ansible is its ability to set facts dynamically during playbook execution. This capability can be particularly useful when you need to generate values on the fly, such as capturing the current date and time.

A practical application of this is setting a fact to hold the current date and time, which can be used for various purposes such as timestamping files, creating backup directories with the date, or logging. Let’s delve into how you can leverage Ansible’s ansible.builtin.set_fact module along with the lookup plugin to achieve this.

Understanding ansible.builtin.set_fact and lookup

Before we dive into the example, it’s essential to understand the two key components we’ll be using: ansible.builtin.set_fact and lookup.

The Practical Example: Setting a Date Fact

Let’s look at a practical example where we set a fact named unix_date that captures the current date and time in a specific format:

- name: "Set a fact for the date"
    unix_date: "{{ lookup('pipe', 'date +%Y-%m-%d-%H%M') }}"

In this task:

Use Cases

Once unix_date fact is set, you can use it anywhere within your playbook. Here are a few examples of how you might use this fact:


Leveraging Ansible’s ansible.builtin.set_fact module with the lookup plugin to set dynamic date facts is a powerful technique that can enhance your automation strategies. It’s a testament to Ansible’s flexibility and capability to adapt to various operational needs. This method is not only efficient but also simplifies processes that require dynamic values, making your automation tasks more robust and reliable. Whether you’re a seasoned Ansible user or new to the platform, incorporating dynamic date facts into your playbooks can significantly streamline your automation workflows.

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