• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

Using Date and Timestamp Variable in Ansible Playbook

May 29, 2019 By Cloud_Devops 3 Comments

Timestamp plays a crucial role while automating any job. Timestamp variable would be helpful to capture the current date and time (While gathering facts). In Ansible, we might need to create a file or directory based on the timestamp to make it unique. By default, the ansible engine collects timestamp of the remote systems in facts unless the gather_facts is disabled in playbook or ansible.cfg. This article will help you to capture the timestamp and display it Or create the files based on the timestamp to make it unique.

Ansible engine version: 2.7.10

 

Note: Timestamp might not change during the play since it will be recorded during the facts gathering.

 

Capture date YYYY.MM.DD format in Ansible:

1. Login to the ansible server.

 

2. Create the playbook with the following contents.

---
- hosts: localhost
  become: no
  gather_facts: yes

  tasks:
   - name: Display the current timestamp in YYYY-MM-DD
     debug:
      var=ansible_date_time.date

 

3. Execute the playbook. It will display the current timestamp. (time stamp will be captured during the gathering facts)

[root@ansible-server ~]# ansible-playbook timestamp.yaml

PLAY [localhost] ********************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [localhost]

TASK [Display the current timestamp in YYYY-MM-DD] *************************************************************************************************
ok: [localhost] => {
    "ansible_date_time.date": "2019-03-11"
}

PLAY RECAP *************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

[root@ansible-server ~]#

 

Available date & time format in Ansible:

1. Create the playbook with the following contents.

---
- hosts: localhost
  become: no
  gather_facts: yes

  tasks:
   - name: Display the availble timestamp format in Ansible
     debug:
      var=ansible_date_time

 

2. Execute the playbook and see the available date & time formats.

[root@ansible-server ~]# ansible-playbook timestamp.yaml

PLAY [localhost] ********************************************************************

TASK [Gathering Facts] ****************************************************************
ok: [localhost]

TASK [Display the availble timestamp format in Ansible] ***************************************************************************************
ok: [localhost] => {
    "ansible_date_time": {
        "date": "2019-03-11",
        "day": "11",
        "epoch": "1552295973",
        "hour": "09",
        "iso8601": "2019-03-11T09:19:33Z",
        "iso8601_basic": "20190311T091933496829",
        "iso8601_basic_short": "20190311T091933",
        "iso8601_micro": "2019-03-11T09:19:33.496910Z",
        "minute": "19",
        "month": "03",
        "second": "33",
        "time": "09:19:33",
        "tz": "UTC",
        "tz_offset": "+0000",
        "weekday": "Monday",
        "weekday_number": "1",
        "weeknumber": "10",
        "year": "2019"
    }
}

PLAY RECAP ***********************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
[root@ansible-server ~]# 

 

In the above output, we could see the assignment “variables: values“. If you want to capture date and time with seconds, you need to use variable “ansible_date_time.iso8601”

 

Create a file in Ansible with timestamp: 

1.  The following playbook will copy a file in another name with the timestamp variable.

---
- hosts: localhost
  become: no
  gather_facts: yes

  tasks:
   - name: Display the available timestamp format in Ansible
     shell: cp /etc/nsswitch.conf  /etc/nsswitch.conf.{{ ansible_date_time.iso8601 }}

   - name: Display the newly created file
     shell: ls -lrt /etc/nsswitch.conf.{{ ansible_date_time.iso8601 }}
     register: LISTFILE

   - debug: msg={{ LISTFILE.stdout }}

 

2. Run the playbook.

[root@ansible-server ~]# ansible-playbook timestamp.yaml

PLAY [localhost] ******************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [localhost]

TASK [Display the availble timestamp format in Ansible] ************************************************************************************************
changed: [localhost]

TASK [Display the newly created file] ************************************************************************************************
changed: [localhost]

TASK [debug] ***********************************************************************************
ok: [localhost] => {
    "msg": "-rw-r--r-- 1 root root 1746 Mar 11 10:02 /etc/nsswitch.conf.2019-03-11T10:02:26Z"
}

PLAY RECAP *************************************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0

[root@ansible-server ~]# 

In the above playbook results, we could see that file has been copied in another name with the timestamp variable.

 

Note: Timestamp variable is captured during the fact gathering. You can re-use the same variable, again and again, to refer the file/object even though, time might be different during the execution phase.

 

Re-using timestamp variable to refer the same object during the play 

1. The following example shows that file has been copied with the new name using a timestamp variable and cleaned at the end.

---
- hosts: localhost
  become: no
  gather_facts: yes

  tasks:
   - name: Display the available timestamp format in Ansible
     shell: cp /etc/nsswitch.conf  /etc/nsswitch.conf.{{ ansible_date_time.iso8601 }}

   - name: Display the newly created file
     shell: ls -lrt /etc/nsswitch.conf.{{ ansible_date_time.iso8601 }}
     register: LISTFILE

   - debug: msg={{ LISTFILE.stdout }}

   - name: Delete the newly copied file.
     file:
       path: /etc/nsswitch.conf.{{ ansible_date_time.iso8601 }}
       state: absent

 

2. Execute the playbook.

[root@ansible-server ~]# ansible-playbook example_create_file_delete_timestamp.yaml

PLAY [localhost] **************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [localhost]

TASK [Display the availble timestamp format in Ansible] ********************************************************************************************
changed: [localhost]

TASK [Display the newly created file] ********************************************************************************************
changed: [localhost]

TASK [debug] *******************************************************************************
ok: [localhost] => {
    "msg": "-rw-r--r-- 1 root root 1746 Mar 11 10:15 /etc/nsswitch.conf.2019-03-11T10:15:39Z"
}

TASK [Delete the newly copied file.] *******************************************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************************
localhost                  : ok=5    changed=3    unreachable=0    failed=0

[root@ansible-server ~]# date
Mon Mar 11 10:15:43 UTC 2019
[root@ansible-server ~]#

We can see that the file has been successfully deleted by using the same timestamp variable. (Delete task is showing as changed)

Hope this article is informative to you. Share it! Comment it !! Be sociable!!!

Filed Under: Ansible engine, Configuration Management, DevOps Tagged With: Ansible, ansible date/time, ansible tutorial

Reader Interactions

Comments

  1. G says

    July 16, 2019 at 4:47 pm

    As I found out recently “ansible_date_time” may not behave how you assume it does..

    https://github.com/ansible/ansible/issues/22561
    > ansible_date_time is NOT actually the date/time — it is the time cached from the facts.

    Reply
    • Lingeswaran R says

      July 16, 2019 at 7:31 pm

      Yes. Timestamp gathered from facts. That’s why we are able to perform the file cleanup at the end of the play.

      If you need the realtime timestamp, you could run the date command using the shell module.

      Thank you
      Lingesh

      Reply
      • John says

        December 6, 2020 at 1:31 am

        Actually there’s a better way to get the actual date and/or time, by using the Jinja2 filter:

        “`
        “{{ lookup(‘pipe’,’date +%Y-%m-%d %H:%M:%S’) }}”
        “`

        Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version