Question:
I am usingansible_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:
Answer:
In order to do computation on date, you need both elements to bedatetime
.In your task, you are trying to subtract a
string
, from your CSV, to an epoch, which is of type AnsibleUnsafeText
.So you need:
- The current date, in a
datetime
format, which can be achieved with'%Y-%m-%d %H:%M:%S' | strftime | to_datetime
- The date from your CSV converted in a
datetime
, which can be achieved withlookup( 'csvfile', inventory_hostname, file='serverInventory.csv', delimiter=',', col=5 ) | to_datetime
- Then only you can subtract the two of them
So, we end up with a task like:
If you have better answer, please add a comment about this, thank you!