• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Getting error with ansible_date_time.epoch when subtracting dates

Resolved: Getting error with ansible_date_time.epoch when subtracting dates

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am using ansible_date_time.epoch to calculate days since a server was built. I get the build date information from a csv file and trying to do a calculation, but I am getting an error.
serverInventory.csv:
Hosts:
Tasks:
Output:

Answer:

In order to do computation on date, you need both elements to be datetime.
In your task, you are trying to subtract a string, from your CSV, to an epoch, which is of type AnsibleUnsafeText.
So you need:
  1. The current date, in a datetime format, which can be achieved with
    '%Y-%m-%d %H:%M:%S' | strftime | to_datetime
    
  2. The date from your CSV converted in a datetime, which can be achieved with
    lookup(
      'csvfile', inventory_hostname,
      file='serverInventory.csv', delimiter=',', col=5
    ) | to_datetime
    
  3. Then only you can subtract the two of them

So, we end up with a task like:
Which would, on your example CSV, yield:

If you have better answer, please add a comment about this, thank you!

ansible epoch
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How can I write CSS selector(s) that apply to table rows for all td elements on that row after a td with a certain class?

01/04/2023

Resolved: How do I use SetWindowText with Unicode in Win32 using PowerShell?

01/04/2023

Resolved: Shopware 400 Status Error “This value is too long. It should have 255 character or less.” When I Try Updating Database Table

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.