Ansible - ansible_date_time fact

ansible_date_time is a magic variable that returns date/time on the managed node (e.g. the target system). Or, the lookup plugin and date can be used. Or, the strftime (string format time) filter can be used.

In this example, the debug module is used output the current date.

- name: current date
  debug: 
    msg: "{{ ansible_date_time.date }}"

 

Something like this should be returned.

TASK [current date]
ok: [server1.example.com] => {
    "msg": "2020-12-11"
}

 

Or the time.

- name: current time
  debug: 
    msg: "{{ ansible_date_time.time }}"

 

Something like this should be returned.

TASK [current time]
ok: [server1.example.com] => {
    "msg": "01:54:08"
}

 

Usually, you are going to want to format the output, like this.

vars:
  date: "{{ ansible_date_time.year }}-{{ ansible_date_time.month }}-{{ ansible_date_time.day }}"
  time: "{{ ansible_date_time.hour }}:{{ ansible_date_time.minute }}:{{ ansible_date_time.second }}"

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 6aee6 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |