Maybe if else

Here is my logic if if else Ansible.

- name: Check certs exist
  stat: path=/etc/letsencrypt/live/{{ rootDomain }}/fullchain.pem
  register: st

- include: ./_common/check-certs-renewable.yaml
  when: st.stat.exists

- include: ./_common/create-certs.yaml
  when: not st.stat.exists

This code boils down to the following:

There are IF certificates

renew certificates

ELSE

create certificates

End IF

Is this the right approach or is there a better approach to the IF ELSE construct in the inaccessible?

+4
source share
1 answer

What you have there should work, and this is one way to do it.

Alternatively, you can use the Jinja query to reduce it to 2 tasks, for example:

 - name: Check certs exist
   stat: path=/etc/letsencrypt/live/{{ rootDomain }}/fullchain.pem
   register: st

- include: "{{ './_common/check-certs-renewable.yaml' if st.stat.exists else './_common/create-certs.yaml' }}"

However, it is more a matter of personal preference than anything else, and your path is more readable, so I will just stick to this IMHO.

+5
source

Source: https://habr.com/ru/post/1668788/


All Articles