Site icon UnixArena

Using Date and Timestamp Variable in Ansible Playbook

ansible timestamp

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!!!

Exit mobile version