Hi,
When you register the debug task you are not only handed back the
printed string but a dictionary containing information about the
task execution.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables
What goes wrong is that in your code you are trying to subtract a dictionary (date_earlier) from a string.
Take a look at the below example and I think things will become clearer:
user@dev:~/code/snippets$ cat register.yml
#!/usr/bin/ansible-playbook
---
- hosts: localhost
gather_facts: false
vars:
test: thisisastring
tasks:
- debug:
msg: "{{ test[:10] }}"
register: registered_var
- debug:
var: registered_var
user@dev:~/code/snippets$ ./register.yml
PLAY [localhost]
******************************************************************************
TASK [debug]
**********************************************************************************
ok: [localhost] => {
"msg": "thisisastr"
}
TASK [debug]
**********************************************************************************
ok: [localhost] => {
"registered_var": {
"changed": false,
"failed": false,
"msg": "thisisastr"
}
}
PLAY RECAP
************************************************************************************
localhost : ok=2 changed=0
unreachable=0 failed=0 skipped=0 rescued=0
ignored=0
If you to create a new variable you should instead use the ansible.builtin.set_fact module.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html
//Oskar
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e4de2516-c9a2-4e30-a85a-bc9498bec14fn%40googlegroups.com.
HI,
When you set your fact it is being casted into a string and then you are trying to subtract a string from a string, which isn't possible.
You need to convert the variable to an integer in order to
perform mathematical operations on it:
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/872e0945-a7ec-43d4-b42a-cd4449648fa8n%40googlegroups.com.