top of page
Writer's pictureSiddhesh Kadam

Automating Dynamic Time Date Facts with Ansible


Ansible Time Date Facts

Automating dynamic time and date facts with Ansible can be achieved using the ansible_date_time fact module. This module gathers various date and time-related information from the managed hosts. You can use this information in your playbooks for various purposes such as logging, conditional tasks, or configuration management.


Here's an example playbook demonstrating how to use ansible_date_time to gather dynamic time and date facts:

[root@siddhesh ~]# cat date.yml
---
- name: Gather Time and Date Facts
  hosts: localhost
  tasks:
    - name: Display current date and time
      debug:
        msg: "Current date and time: {{ ansible_date_time.date }} {{ ansible_date_time.time }}"
    - name: Display current year
      debug:
        msg: "Current year: {{ ansible_date_time.year }}"
    - name: Display current month
      debug:
        msg: "Current month: {{ ansible_date_time.month }}"
    - name: Display current day
      debug:
        msg: "Current day: {{ ansible_date_time.day }}"
    - name: Display current hour
      debug:
        msg: "Current hour: {{ ansible_date_time.hour }}"
    - name: Display current minute
      debug:
        msg: "Current minute: {{ ansible_date_time.minute }}"
    - name: Display current second
      debug:
        msg: "Current second: {{ ansible_date_time.second }}"
[root@siddhesh ~]#

In this playbook:

We're using the ansible_date_time fact module to gather information about the current date and time.

Various tasks are used to display different aspects of the date and time, such as the year, month, day, hour, minute, and second.

You can run this playbook with the following command:

[root@siddhesh ~]# ansible-playbook date.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [Gather Time and Date Facts] **************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************
ok: [localhost]
TASK [Display current date and time] ***********************************************************************************************************************
ok: [localhost] => {
    "msg": "Current date and time: 2024-04-09 16:06:09"
}
TASK [Display current year] ********************************************************************************************************************************
ok: [localhost] => {
    "msg": "Current year: 2024"
}
TASK [Display current month] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "Current month: 04"
}
TASK [Display current day] *********************************************************************************************************************************
ok: [localhost] => {
    "msg": "Current day: 09"
}
TASK [Display current hour] ********************************************************************************************************************************
ok: [localhost] => {
    "msg": "Current hour: 16"
}
TASK [Display current minute] ******************************************************************************************************************************
ok: [localhost] => {
    "msg": "Current minute: 06"
}
TASK [Display current second] ******************************************************************************************************************************
ok: [localhost] => {
    "msg": "Current second: 09"
}
PLAY RECAP *************************************************************************************************************************************************
localhost                  : ok=8    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@siddhesh ~]#

The playbook will only run on localhost, and you'll see the time and date facts gathered from localhost. You can further use these dynamic time and date facts within your playbooks for conditional statements, template rendering, or any other tasks where this information might be useful.


Yorumlar

5 üzerinden 0 yıldız
Henüz hiç puanlama yok

Puanlama ekleyin
bottom of page