Bootstrap FreeKB - Ansible - lookup pipe date (time)
Ansible - lookup pipe date (time)

Updated:   |  Ansible articles

The lookup plugin is always executed on the control node (that's your Ansible server), which is to say that the lookup plugin does not do something on your managed nodes (the target systems).

To use the pipe plugin, the Python script must be in your Ansible plugins lookup directory, such as /usr/lib/python2.7/site-packages/ansible/plugins/lookup/

ansible_date_time is a magic variable that returns date/time on the managed node (e.g. the target system). Or, the lookup plugin 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 time.

- name: "current datetime"
    msg: "{{ lookup('pipe','date') }}"


Something like this should be returned. The output is identical to the Linux date command.

TASK [current datetime]
ok: [] => {
    "msg": "Fri Dec 11 02:04:14 CST 2020"


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

- name: "current datetime"
    msg: "{{ lookup('pipe','date +%Y-%m-%d_%H:%M:%S') }}"


Something like this should be returned.

TASK [current datetime]
ok: [] => {
    "msg": "2020-12-11_02:07:37"


Or even better, create a date and time variables.

  date: "{{ lookup('pipe','date +%Y-%m-%d') }}"
  time: "{{ lookup('pipe','date +%H:%M:%S') }}"


Sometimes, you'll be working with an epoch formatted datetime. For example, when using the find module or stat module, the accessed datetime (atime), changed datetime (ctime), and modified datetime (mtime) of the file will be epoch formated, like this.

"mtime": 158338361.5600421


The strftime (string format time) filter can be used to convert epoch to human readable, like this.

- debug:
    msg: "{{ '%Y-%m-%d' | strftime(item.mtime) }}"
  with_items: "{{ out.files }}"



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

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