Reputation: 13
I'm trying to run this task:
---
- name: "{{ BANNER }}"
shell: "rpm -qf /etc/issue"
register: rpm
changed_when: False
ignore_errors: True
- shell: 'rpm -q -i "{{ rpm.stdout }}" | grep "Install Date:" | awk ''{ print $4 " " $5 " " $6 }'''
register: rpm
changed_when: False
ignore_errors: True
- shell: 'date -d "{{ rpm.stdout }}" +''%Y-%d-%m'''
register: date
changed_when: False
ignore_errors: True
- debug: var=date.stdout
- debug: var={{ (( date.stdout | to_datetime('%Y-%m-%d')) - ("2020-12-25" | to_datetime('%Y-%m-%d'))).days }}
Basically I need to pass the string contained in data.stdout to filter to_datetime for date subtraction but I receive this error:
TASK [RH7-008 : debug] **********************************************************************************************************************************************
ok: [192.168.56.1] => {
"date.stdout": "2019-14-03"
}
TASK [RH7-008 : debug] **********************************************************************************************************************************************
fatal: [192.168.56.1]: FAILED! => {"msg": "the field 'args' has an invalid value ([u'check_mode']), and could not be converted to an dict.The error was: time data '2019-14-03' does not match format '%Y-%m-%d'\n\nThe error appears to have been in '/root/ansible/roles/RH7-008/tasks/check_mode.yml': line 27, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug: var={{ (( date.stdout | to_datetime('%Y-%m-%d')) - (\"2020-12-25\" | to_datetime('%Y-%m-%d'))).days }}\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n\nexception type: <type 'exceptions.ValueError'>\nexception: time data '2019-14-03' does not match format '%Y-%m-%d'"}
to retry, use: --limit @/root/ansible/main.retry
It seem's that the format input contained in date.stdout
is wrong compared to the one that is specified in to_datetime('%Y-%m-%d')
. What am I missing? Maybe there's some strange character in date.stdout
?
Thanks in advance! Tommaso.
Upvotes: 0
Views: 853
Reputation: 311870
You are asking to_datetime
to parse dates of the form %Y-%m-%d
.
You are passing in data of the form 2019-14-03
.
There are not 14 months in the year.
You want the format argument to to_datetime
to match the format argument you're providing to the date
command:
to_datetime('%Y-%d-%m')
Making it (reformatted slightly for clarity, and with the hardcoded date swapped to match your format):
- debug:
var: >-
(
(date.stdout | to_datetime('%Y-%d-%m')) -
("2020-25-12" | to_datetime('%Y-%d-%m'))
).days
Alternatively, swap the argument to the date
command instead if you really wanted %Y-%m-%d
. Just make sure they match.
Upvotes: 1