ansible if else construct

What you have there should work and is one way of doing it.

Alternatively, you could use a Jinja query to reduce it to 2 tasks, such that:

 - 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's more a matter of personal preference than anything else, and your way is more readable, so I would just stick with that IMHO.


What about the following

condition_arg: >-
  {%- if ansible_distribution == 'Ubuntu' -%}
  'param for ubuntu'
  {%- elif ansible_distribution == "Debian" -%}
  'params for debian'
  {%- else -%}
  'what else'
  {%- end %-}

kind of if-else statement.. ? They have been used in some projects with ansible. This form isn't available from the official docs, but from an active thread on stackexchange


The following can be used if you want to search if a register is empty. The msg Unavailable is printed if php -version info is not found.If it is found, it prints the output of the command.

enter image description here

- name: Check php
  shell: php -version
  register: result

- name: Print register
  debug:
    msg: '{{ (result.stdout) | ternary( (result.stdout),"Unavailable") }}'