How to use vars to work with to_datetime

1,520 views
Skip to first unread message

Andy Magana

unread,
May 1, 2019, 10:32:29 AM5/1/19
to Ansible Project
Hello everyone,


My registered vars are not working with my math equation using to_datetime filter it will work with the string "2019-04-01" but not with my var.

PLAY [localhost] *********************************************************************************************************************************************

TASK
[Gathering Facts] ***************************************************************************************************************************************
ok
: [localhost]

TASK
[get the file] ******************************************************************************************************************************************
changed
: [localhost]

TASK
[debug] *************************************************************************************************************************************************
ok
: [localhost] => {
   
"today": {
       
"changed": true,
       
"cmd": "du -h --time /etc/ansible/ansible.cfg | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'",
       
"delta": "0:00:00.011270",
       
"end": "2019-05-01 09:27:11.662919",
       
"failed": false,
       
"rc": 0,
       
"start": "2019-05-01 09:27:11.651649",
       
"stderr": "",
       
"stderr_lines": [],
       
"stdout": "2019-04-30",
       
"stdout_lines": [
           
"2019-04-30"
       
]
   
}
}

TASK
[get current date] **************************************************************************************************************************************
changed
: [localhost]

TASK
[debug] *************************************************************************************************************************************************
ok
: [localhost] => {
   
"current": {
       
"changed": true,
       
"cmd": "date +%F",
       
"delta": "0:00:00.010083",
       
"end": "2019-05-01 09:27:12.031688",
       
"failed": false,
       
"rc": 0,
       
"start": "2019-05-01 09:27:12.021605",
       
"stderr": "",
       
"stderr_lines": [],
       
"stdout": "2019-05-01",
       
"stdout_lines": [
           
"2019-05-01"
       
]
   
}
}

TASK
[debug] *************************************************************************************************************************************************
fatal
: [localhost]: FAILED! => {"msg": "the field 'args' has an invalid value ({u'msg': u\"{{ ( 'current' | to_datetime('%Y-%m-%d') - 'today' | to_datetime('%Y-%m-%d') ).days }}\"}), and could not be converted to an dict.The error was: time data 'current' does not match format '%Y-%m-%d'\n\nThe error appears to have been in '/etc/ansible/wipstat.yml': line 20, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - debug:\n    ^ here\n"}

PLAY RECAP
***************************************************************************************************************************************************
localhost                  
: ok=5    changed=2    unreachable=0    failed=1  



d...@linder.org

unread,
May 1, 2019, 10:51:06 PM5/1/19
to Ansible Project
It's hard without the actual playbook, but I think you need to use 'current.stdout' inserted of just 'current' in that last task.

Andy Magana

unread,
May 2, 2019, 7:30:10 AM5/2/19
to Ansible Project
So I have resolved my issue thanks to others advice what worked is to
1. stat the file get its mtime and
2. register and
3. assign it to a variable and the same for the current date and
4. put both variables  and include to convert them to an INT into a jinja subtraction arithmetic and divide all inside the jinja {{ }}
5. register the sum total and include the INT
6. add another debug and msg to output SUM as a whole number instead of a float.





[root@ansible ansible]# ansible-playbook mystat.yml

PLAY
[localhost] *****************************************************************************************************************

TASK
[Gathering Facts] ***********************************************************************************************************
ok
: [localhost]

TASK
[stat] **********************************************************************************************************************
ok
: [localhost]

TASK
[debug] *********************************************************************************************************************
ok
: [localhost] => {
   
"file_age.stat.mtime": "1556662665.1"
}

TASK
[debug] *********************************************************************************************************************
ok
: [localhost] => {
   
"ansible_date_time.epoch": "1556746971"
}

TASK
[age of file in days] *******************************************************************************************************
ok
: [localhost] => {
   
"msg": "0.975763888889"
}

TASK
[debug] *********************************************************************************************************************
ok
: [localhost] => {
   
"msg": {
       
"changed": false,
       
"failed": false,
       
"msg": "0.975763888889"
   
}
}

TASK
[debug] *********************************************************************************************************************
ok
: [localhost] => {
   
"msg": " Your file is 0 days old"
}

PLAY RECAP
***********************************************************************************************************************
localhost                  
: ok=7    changed=0    unreachable=0    failed=0  

[root@ansible ansible]#

AND my playbook looks like this above is the results.


---
- hosts: localhost
  user
: ansible
  become
: true
  tasks
:
 
- stat:
      path
: /etc/ansible/ansible.cfg
   
register: file_age

 
- debug:
     
var: file_age.stat.mtime

 
- debug:
     
var: ansible_date_time.epoch

 
- name: age of file in days
    debug
:
      msg
: "{{ (ansible_date_time.epoch|int - file_age.stat.mtime|int)/(86400|round|int) }}"
   
register: msg

 
- debug:
     
var: msg

 
- debug:
      msg
: " Your file is {{ msg|int }} days old"
~                                                                            


Dan Linder

unread,
May 19, 2019, 9:26:09 AM5/19/19
to Ansible Project
You're using the "debug:" module to set the "msg" variable so the date value isn't in the "{{ msg }}" variable, rather it's in the "{{ msg.msg }}" variable.

Try these lines in stead:
  - name: FIXED - age of file in days stored in msg
    set_fact:

      msg: "{{ (ansible_date_time.epoch|int - file_age.stat.mtime|int)/(86400|round|int) }}"

  - name: FIXED - Show msg
    debug:
      var: msg

  - name: FIXED - Show msg as integer

    debug:
      msg: " Your file is {{ msg|int }} days old"


That then stores the computation done in the "set_fact:" module in the "msg" variable as expected.
Reply all
Reply to author
Forward
0 new messages