top of page

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
    - name: Display current date and time
        msg: "Current date and time: {{ }} {{ ansible_date_time.time }}"
    - name: Display current year
        msg: "Current year: {{ ansible_date_time.year }}"
    - name: Display current month
        msg: "Current month: {{ ansible_date_time.month }}"
    - name: Display current day
        msg: "Current day: {{ }}"
    - name: Display current hour
        msg: "Current hour: {{ ansible_date_time.hour }}"
    - name: Display current minute
        msg: "Current minute: {{ ansible_date_time.minute }}"
    - name: Display current second
        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.


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

Puanlama ekleyin
bottom of page