|
| 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 | +# Define target hosts for DNS records, excluding the 'maas' group. |
| 26 | +# This can be overridden in secrets/maas.yml (e.g., target_hosts: ['host1.example.com', 'host2.example.com']) |
| 27 | +# or via extra vars (e.g., -e "target_hosts=['host1.example.com']"). |
| 28 | +- name: Define target hosts for DNS records |
| 29 | + ansible.builtin.set_fact: |
| 30 | + target_hosts: "{{ target_hosts | default(groups | dict2items | rejectattr('key', 'equalto', 'maas') | map(attribute='value') | flatten | unique | default([])) }}" |
| 31 | + when: groups.keys() | length > 1 |
| 32 | + |
| 33 | +- name: Build DNS records for all interfaces |
| 34 | + ansible.builtin.set_fact: |
| 35 | + dns_records: "{{ dns_records + [{'name': item[0].split('.')[0], 'ip': interface_ip, 'type': 'A', 'domain': item[1].value}] }}" |
| 36 | + loop: "{{ (target_hosts | default([])) | product(dns_domains | dict2items) | list }}" |
| 37 | + vars: |
| 38 | + interface_ip: "{{ hostvars[item[0]][item[1].key] if item[1].key != 'ceph' else hostvars[item[0]]['ip'] }}" |
| 39 | + when: |
| 40 | + - target_hosts is defined and target_hosts | length > 0 |
| 41 | + - "item[1].key in hostvars[item[0]] or (item[1].key == 'ceph' and 'ip' in hostvars[item[0]])" |
| 42 | + |
| 43 | +- name: Parse desired FQDNs |
| 44 | + ansible.builtin.set_fact: |
| 45 | + desired_fqdns: "{{ dns_records | map(attribute='name') | zip(dns_records | map(attribute='domain')) | map('join', '.') | list }}" |
| 46 | + when: dns_records | length > 0 |
| 47 | + |
| 48 | +- name: Remove unwanted DNS records |
| 49 | + ansible.builtin.command: "maas {{ maas_profile }} dnsresource delete {{ item.id }}" |
| 50 | + loop: "{{ existing_resources.stdout | from_json }}" |
| 51 | + when: > |
| 52 | + dns_records | length > 0 and |
| 53 | + item.fqdn not in desired_fqdns |
| 54 | + register: dns_deletion |
| 55 | + failed_when: dns_deletion.rc != 0 and "does not exist" not in dns_deletion.stderr |
| 56 | + notify: log_deletion_failure |
| 57 | + |
| 58 | +- name: Get updated DNS resources after deletions |
| 59 | + ansible.builtin.command: "maas {{ maas_profile }} dnsresources read" |
| 60 | + register: updated_resources |
| 61 | + changed_when: false |
| 62 | + |
| 63 | +- name: Get existing DNS domains |
| 64 | + ansible.builtin.command: "maas {{ maas_profile }} domains read" |
| 65 | + register: existing_domains |
| 66 | + changed_when: false |
| 67 | + |
| 68 | +- name: Parse existing domains |
| 69 | + ansible.builtin.set_fact: |
| 70 | + current_domains: "{{ existing_domains.stdout | from_json | map(attribute='name') | list }}" |
| 71 | + |
| 72 | +- name: Remove unwanted domains |
| 73 | + ansible.builtin.command: "maas {{ maas_profile }} domain delete {{ item.id }}" |
| 74 | + loop: "{{ existing_domains.stdout | from_json }}" |
| 75 | + when: > |
| 76 | + item.name not in default_domains and |
| 77 | + item.name not in dns_domains.values() |
| 78 | + register: domain_deletion |
| 79 | + failed_when: domain_deletion.rc != 0 and "does not exist" not in domain_deletion.stderr and "protected foreign keys" not in domain_deletion.stderr |
| 80 | + |
| 81 | +- name: Ensure new DNS domains exist |
| 82 | + ansible.builtin.command: "maas {{ maas_profile }} domains create name={{ item.value }}" |
| 83 | + loop: "{{ dns_domains | dict2items }}" |
| 84 | + when: item.value not in current_domains |
| 85 | + register: domain_creation |
| 86 | + failed_when: domain_creation.rc != 0 and "already exists" not in domain_creation.stderr |
| 87 | + |
| 88 | +- name: Ensure DNS records exist |
| 89 | + ansible.builtin.command: > |
| 90 | + maas {{ maas_profile }} dnsresources create |
| 91 | + fqdn={{ item.name }}.{{ item.domain }} |
| 92 | + ip_addresses={{ item.ip }} |
| 93 | + loop: "{{ dns_records }}" |
| 94 | + when: > |
| 95 | + dns_records | length > 0 and |
| 96 | + (item.name + '.' + item.domain) not in |
| 97 | + (updated_resources.stdout | from_json | map(attribute='fqdn') | list) |
| 98 | + register: dns_creation |
| 99 | + failed_when: dns_creation.rc != 0 and "already exists" not in dns_creation.stderr |
| 100 | + |
| 101 | +- name: Logout from MAAS |
| 102 | + ansible.builtin.command: "maas logout {{ maas_profile }}" |
| 103 | + when: maas_login is success |
| 104 | + changed_when: true |
0 commit comments