If possible I would like to combine the two tasks below into a single task. Rather than use the when conditional, is there some way to add the last two list items under subject_alt_name (second task) only when item.0.sans is defined? These two tasks are otherwise identical.
I did try some additional filters for those lines to default/omit them, but then I ended up with errors that the list could not be parsed. I also explored the possibility of an inline template, but I cannot tell if that is supported in this context. A templating solution seems ideal to me... I think that technique could be useful in many other scenarios.
The full playbook is pretty short, so I can post that if it would be helpful for additional context.
- name: generate endpoint CSR - no custom sans
when: item.0.sans is undefined
community.crypto.openssl_csr:
common_name: "{{item.0.name}}.{{item.1}}"
path: "{{endpoint_artifact_dir}}/{{item.1}}//CSRs/{{item.0.name}}.{{item.1}}.pem"
privatekey_path: "{{endpoint_artifact_dir}}/{{item.1}}/keys/{{item.0.name}}.{{item.1}}.pem"
subject_alt_name:
- "DNS:{{item.0.name}}.{{item.1}}"
- "DNS:{{item.0.name}}"
- "IP:127.0.0.1"
with_nested:
- "{{endpoints}}"
- "{{domains}}"
- name: generate endpoint CSR - with custom sans
when: item.0.sans is defined
community.crypto.openssl_csr:
common_name: "{{item.0.name}}.{{item.1}}"
path: "{{endpoint_artifact_dir}}/{{item.1}}/CSRs/{{item.0.name}}.{{item.1}}.pem"
privatekey_path: "{{endpoint_artifact_dir}}/{{item.1}}/keys/{{item.0.name}}.{{item.1}}.pem"
subject_alt_name:
- "DNS:{{item.0.name}}.{{item.1}}"
- "DNS:{{item.0.name}}"
- "IP:127.0.0.1"
- "{{ item.0.sans | join(',') | replace(',', '.' + item.1 + ', ') + '.' + item.1 }}"
- "{{ item.0.sans | join(',') }}"
with_nested:
- "{{endpoints}}"
- "{{domains}}"