0

I play this playbook to get amount of days between two expiration certificates dates:

---
- hosts: localhost
  gather_facts: no
  connection: local

  tasks:

  - name: Save the old date
    shell:
       cmd: >-
           date --date="$(openssl x509 -noout -dates -in cert1.crt
           | grep 'notAfter' | cut -d= -f2)" --utc +"%d-%m-%Y"
    ignore_errors: true
    args:
       executable: /bin/bash
    register: x1

  - name: Save the new date
    shell:
       cmd: >-
           date --date="$(openssl x509 -noout -dates -in cert2.crt
           | grep 'notAfter' | cut -d= -f2)" --utc +"%d-%m-%Y"
    ignore_errors: true
    args:
       executable: /bin/bash
    register: x2

  - name: Show mount of days with dedug module
    debug:
      var: "{{ (( x2.stdout | to_datetime('%d-%m-%Y')) - ( x1.stdout  | to_datetime('%d-%m-%Y'))).days }}"

  - set_fact:
       diff: "{{ (( x2.stdout | to_datetime('%d-%m-%Y')) - ( x1.stdout | to_datetime('%d-%m-%Y'))).days }}"

  - name: Show diff variable
    debug:
      var: diff

The playbook run well and i can show the resultat with debug module (task name: Show mount of days with debug module):

ok: [localhost] => {
    "msg": "357"
}

Otherwise the task (Show diff variable) show me a null output:

ok: [localhost] => {}

I can't find what is wrong with set_fact , anyone have an idea please ?

1
0

When I have a look at your posted output and compare it to mine, there is something wrong:

ok: [localhost] => {
    "msg": "357"
}

That output comes from the debug message. But only with debug msg={{ value }} and not with debug var={{ value }}. When I do it with var= then I get (in my case) "32": "32" as a response.

But that's not the problem. The problem is:

- name: Show diff variable
  debug:
    var: diff

The name diff for the variable is somehow reserved. You need to set the diff as Jinja

- name: Show diff variable
  debug:
    var: "{{ diff }}"

And then I see

ok: [localhost] => {
    "32": "32"
}

When I rename the variable to diffa everything is normal. I can't find any documentation for it.

- name: Show mount of days with dedug module
  set_fact:
    diffa: "{{ (( x2 | to_datetime('%d-%m-%Y')) - ( x1  | to_datetime('%d-%m-%Y'))).days }}"

- name: Show mount of days with dedug module
  debug:
    var: diffa

output:

TASK [Show mount of days with dedug module] *********************************************
ok: [localhost] => {
    "diffa": "32"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.