|
| 1 | +--- |
| 2 | +- name: Include secrets |
| 3 | + ansible.builtin.include_vars: "{{ item }}" |
| 4 | + no_log: true |
| 5 | + with_first_found: |
| 6 | + - "{{ secrets_path | mandatory }}/maas.yml" |
| 7 | + tags: |
| 8 | + - always |
| 9 | + |
| 10 | +- name: Ensure MAAS CLI is logged in |
| 11 | + ansible.builtin.command: "maas login {{ maas_profile }} {{ maas_api_url }} {{ maas_api_key }}" |
| 12 | + register: maas_login |
| 13 | + changed_when: maas_login.rc == 0 |
| 14 | + failed_when: maas_login.rc != 0 and maas_login.stderr != "Profile already exists" |
| 15 | + |
| 16 | +- name: Get existing DNS resources |
| 17 | + ansible.builtin.command: "maas {{ maas_profile }} dnsresources read" |
| 18 | + register: existing_resources |
| 19 | + changed_when: false |
| 20 | + |
| 21 | +- name: Initialize DNS records list |
| 22 | + ansible.builtin.set_fact: |
| 23 | + dns_records: [] |
| 24 | + |
| 25 | +- name: Define target hosts for DNS records |
| 26 | + ansible.builtin.set_fact: |
| 27 | + target_hosts: "{{ groups | dict2items | rejectattr('key', 'equalto', 'maas') | map(attribute='value') | flatten | unique }}" |
| 28 | + when: groups.keys() | length > 1 |
| 29 | + |
| 30 | +- name: Build DNS records for primary interfaces |
| 31 | + ansible.builtin.set_fact: |
| 32 | + dns_records: "{{ dns_records + [{'name': host.split('.')[0], 'ip': hostvars[host]['ip'], 'type': 'A', 'domain': dns_domains.ceph}] }}" |
| 33 | + loop: "{{ target_hosts }}" |
| 34 | + loop_control: |
| 35 | + loop_var: host |
| 36 | + |
| 37 | +- name: Build DNS records for IPMI interfaces |
| 38 | + ansible.builtin.set_fact: |
| 39 | + dns_records: "{{ dns_records + [{'name': host.split('.')[0], 'ip': hostvars[host]['ipmi'], 'type': 'A', 'domain': dns_domains.ipmi}] }}" |
| 40 | + loop: "{{ target_hosts }}" |
| 41 | + loop_control: |
| 42 | + loop_var: host |
| 43 | + when: "'ipmi' in hostvars[host]" |
| 44 | + |
| 45 | +- name: Parse desired FQDNs |
| 46 | + ansible.builtin.set_fact: |
| 47 | + desired_fqdns: "{{ dns_records | map(attribute='name') | zip(dns_records | map(attribute='domain')) | map('join', '.') | list }}" |
| 48 | + |
| 49 | +- name: Debug desired FQDNs |
| 50 | + ansible.builtin.debug: |
| 51 | + msg: "Desired FQDNs: {{ desired_fqdns }}" |
| 52 | + |
| 53 | +- name: Remove unwanted DNS records |
| 54 | + ansible.builtin.command: "maas {{ maas_profile }} dnsresource delete {{ item.id }}" |
| 55 | + loop: "{{ existing_resources.stdout | from_json }}" |
| 56 | + when: > |
| 57 | + item.fqdn not in desired_fqdns and |
| 58 | + item.ip_addresses | map(attribute='ip') | first not in (dns_records | map(attribute='ip') | list) |
| 59 | + register: dns_deletion |
| 60 | + failed_when: dns_deletion.rc != 0 and "does not exist" not in dns_deletion.stderr |
| 61 | + |
| 62 | +- name: Get updated DNS resources after deletions |
| 63 | + ansible.builtin.command: "maas {{ maas_profile }} dnsresources read" |
| 64 | + register: updated_resources |
| 65 | + changed_when: false |
| 66 | + |
| 67 | +- name: Get existing DNS domains |
| 68 | + ansible.builtin.command: "maas {{ maas_profile }} domains read" |
| 69 | + register: existing_domains |
| 70 | + changed_when: false |
| 71 | + |
| 72 | +- name: Parse existing domains |
| 73 | + ansible.builtin.set_fact: |
| 74 | + current_domains: "{{ existing_domains.stdout | from_json | map(attribute='name') | list }}" |
| 75 | + |
| 76 | +- name: Remove unwanted domains |
| 77 | + ansible.builtin.command: "maas {{ maas_profile }} domain delete {{ item.id }}" |
| 78 | + loop: "{{ existing_domains.stdout | from_json }}" |
| 79 | + when: > |
| 80 | + item.name not in allowed_domains and |
| 81 | + item.name not in dns_domains.values() and |
| 82 | + item.resource_record_count == 0 |
| 83 | + register: domain_deletion |
| 84 | + failed_when: domain_deletion.rc != 0 and "does not exist" not in domain_deletion.stderr and "protected foreign keys" not in domain_deletion.stderr |
| 85 | + |
| 86 | +- name: Ensure new DNS domains exist |
| 87 | + ansible.builtin.command: "maas {{ maas_profile }} domains create name={{ item.value }}" |
| 88 | + loop: "{{ dns_domains | dict2items }}" |
| 89 | + when: item.value not in current_domains |
| 90 | + register: domain_creation |
| 91 | + failed_when: domain_creation.rc != 0 and "already exists" not in domain_creation.stderr |
| 92 | + |
| 93 | +- name: Ensure DNS records exist |
| 94 | + ansible.builtin.command: > |
| 95 | + maas {{ maas_profile }} dnsresources create |
| 96 | + fqdn={{ item.name }}.{{ item.domain }} |
| 97 | + ip_addresses={{ item.ip }} |
| 98 | + loop: "{{ dns_records }}" |
| 99 | + when: > |
| 100 | + (item.name + '.' + item.domain) not in |
| 101 | + (updated_resources.stdout | from_json | map(attribute='fqdn') | list) |
| 102 | + register: dns_creation |
| 103 | + failed_when: dns_creation.rc != 0 and "already exists" not in dns_creation.stderr |
| 104 | + |
| 105 | +- name: Logout from MAAS |
| 106 | + ansible.builtin.command: "maas logout {{ maas_profile }}" |
| 107 | + when: maas_login is success |
| 108 | + changed_when: true |
0 commit comments