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