Introduction

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.

  • ansible.builtin.set_fact: This module is used to set new variables on a host-by-host basis during playbook execution. These variables will be available for the remainder of the playbook’s execution and can be used to influence the outcome dynamically.

  • lookup: The lookup plugin is used to retrieve data from outside sources within your Ansible playbooks. There are many types of lookup plugins, but in our example, we will use the pipe lookup to execute a shell command on the control node and use its output.

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"
  ansible.builtin.set_fact:
    unix_date: "{{ lookup('pipe', 'date +%Y-%m-%d-%H%M') }}"

In this task:

  • Task Name: It’s always a good practice to give your tasks descriptive names. Here, it clearly indicates that this task is setting a fact for the date.

  • ansible.builtin.set_fact Module: This module is used to define a new variable.

  • unix_date: This is the name of the variable (or fact) we are setting. The name can be anything you choose, but it should be descriptive of the data it holds.

  • lookup('pipe', 'date +%Y-%m-%d-%H%M'): This part of the task is where the magic happens. The lookup plugin with the pipe argument is used to execute the date command on the control node. The +%Y-%m-%d-%H%M format specifies that we want the output to be in the format of Year-Month-Day-HourMinute.

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:

  • Creating timestamped backup directories.
  • Naming log files with the execution date and time.
  • Tagging resources with the creation date in cloud environments.

Conclusion

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.

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