Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

role should be usable via include_role, vars should be possible via loop #145

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

michaelamattes
Copy link
Collaborator

At the moment the acme role can only use acme_domain in the vars, e.g.

---
- name: Lets Encrypt certificates
  hosts: localhost
  collections:
    - telekom_mms.acme
  roles:
    - telekom_mms.acme.acme
  vars:
    acme_domain:
      email_address: "{{ service_email }}"
      certificate_name: domain
      zone: www.domain.com
      subject_alt_name: ["domain.com"]

Due to the fact that we have more than 100 domains it would be nice if we can loop over the domains.
So the that the role also should be work over ansible.builtin.include_role
So that the code should like, e.g.

---
- name: Lets Encrypt certificates
  hosts: localhost
  tasks:
    - name: Create and upload Lets Encrypt certificates
      ansible.builtin.include_role:
        name: telekom_mms.acme.acme
      vars:
        acme_domain:
          email_address: "{{ service_email }}"
          certificate_name: "{{ certificate.key }}"
          zone: "{{ certificate.value.zone }}"
          subject_alt_name: "{{ certificate.value.subject_alt_name }}"
      loop: "{{ certificates | dict2items }}"
      loop_control:
        loop_var: certificate

@michaelamattes michaelamattes added the enhancement New feature or request label Sep 5, 2024
@michaelamattes michaelamattes requested a review from a team as a code owner September 5, 2024 14:39
@rndmh3ro rndmh3ro self-requested a review September 6, 2024 06:31
@@ -12,21 +12,26 @@
- name: Run key generation
ansible.builtin.include_tasks:
file: create-keys.yml
when: acme_domain | length > 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these checks redundant by the precondition, or are there cases left, where the precondition allows this to happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I changed this with 541861e

@@ -27,3 +27,10 @@
- challenge/dns-01/{{ acme_challenge_provider }}.yml
- challenge/http-01/{{ acme_challenge_provider }}.yml
- challenge-unknown.yml

- name: Check if a acme_domain is set
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be move up to be task nr 2

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this with 541861e

@@ -0,0 +1,11 @@
---
- name: Test role if acme_domain is not set
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think checking for errors from a role is a bit more complcated and this seems like a good approach:
https://stackoverflow.com/questions/55521078/how-to-deal-with-errors-coming-from-ansible-roles

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add this with 541861e

@avalor1
Copy link
Collaborator

avalor1 commented Sep 6, 2024

I'am not quite sure how the new approach would be looking in a var file. Could you please add an example for this?

@michaelamattes
Copy link
Collaborator Author

I'am not quite sure how the new approach would be looking in a var file. Could you please add an example for this?

I add an example ind the readme with 541861e

acme_account_email: ssl-admin@example.com
acme_staging_directory: https://localhost:14000/dir
acme_validate_certs: false
ignore_errors: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that way this will always succeed. We need to include some tasks at the end, that verify the response of the role.

ansible.builtin.assert:
that:
- acme_domain is defined
- acme_domain != []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: acme_domain is used as a hash, here the check is looking for an empty array. This is clone enough, that there is no missmatch in Ansible, but I think it would be cleaner to handle it as a {}.

Also applies to the defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants