|
4 | 4 | tags: config_dhcp
|
5 | 5 | block:
|
6 | 6 | # This section enables DHCP on the subnets included into the secrets repo group_vars and creates an IP range for them
|
| 7 | + - name: Verifying available ipranges |
| 8 | + command: "maas {{ maas_admin_username }} ipranges read" |
| 9 | + register: ip_ranges_raw |
| 10 | + |
| 11 | + - name: Parse IP range JSON |
| 12 | + set_fact: |
| 13 | + ip_ranges: "{{ ip_ranges_raw.stdout | from_json }}" |
| 14 | + |
| 15 | + - name: Check if range exists |
| 16 | + set_fact: |
| 17 | + range_exists: >- |
| 18 | + {{ |
| 19 | + ip_ranges | selectattr('start_ip', 'equalto', '{{ subnet_data.start_ip }}') |
| 20 | + | selectattr('end_ip', 'equalto', '{{ subnet_data.end_ip }}') |
| 21 | + | list |
| 22 | + | length > 0 |
| 23 | + }} |
| 24 | +
|
| 25 | + - name: Debugging range_exists |
| 26 | + debug: |
| 27 | + var: range_exists |
| 28 | + |
7 | 29 | - name: Creating IP Range for {{ subnet_name }} subnet
|
8 |
| - command: "maas admin ipranges create type={{ subnet_data.ip_range_type }} start_ip={{ subnet_data.start_ip }} end_ip={{ subnet_data.end_ip }}" |
| 30 | + command: "maas {{ maas_admin_username }} ipranges create type={{ subnet_data.ip_range_type }} start_ip={{ subnet_data.start_ip }} end_ip={{ subnet_data.end_ip }}" |
9 | 31 | ignore_errors: true
|
10 |
| - tags: skip |
11 |
| - |
12 |
| - - name: Selecting the correct fabric to configure {{ subnet_name }} subnet |
13 |
| - command: bash -c "maas admin subnet read {{ subnet_data.cidr }}|grep 'fabric-'|cut -d '\"' -f 4" |
14 |
| - register: fabric_selected |
15 |
| - |
16 |
| - - name: Getting {{ subnet_name }} subnet ID |
17 |
| - command: bash -c "maas admin subnet read {{ subnet_data.cidr }}|grep '\"id\"'|cut -d ':' -f 2|cut -d ',' -f 1|head -1" |
18 |
| - register: subnet_id |
| 32 | + |
| 33 | + - name: Getting subnet information |
| 34 | + command: "maas {{ maas_admin_username }} subnet read {{ subnet_data.cidr }}" |
| 35 | + register: subnet_info |
| 36 | + |
| 37 | + - name: Defining subnet variables |
| 38 | + set_fact: |
| 39 | + fabric_name: "{{ subnet_info.stdout | from_json | json_query('vlan.fabric') }}" |
| 40 | + vlan_vid: "{{ subnet_info.stdout | from_json | json_query('vlan.vid') }}" |
| 41 | + vlan_id: "{{ subnet_info.stdout | from_json | json_query('vlan.id') }}" |
19 | 42 |
|
20 |
| - - name: Enabling DHCP with HA on {{ subnet_name }} subnet |
21 |
| - command: "maas admin vlan update {{ fabric_selected.stdout }} 0 dhcp_on=True primary_rack={{ groups['maas_region_rack_server'][0] }} secondary_rack={{ groups['maas_rack_server'][0] }}" |
22 |
| - tags: skip |
| 43 | + - name: Enabling DHCP on {{ subnet_name }} subnet |
| 44 | + command: "maas {{ maas_admin_username }} vlan update {{ fabric_name }} {{ vlan_vid }} dhcp_on=True primary_rack={{ groups['maas_region_rack_server'][0] }}" |
23 | 45 |
|
24 | 46 | # This task creates the directory where the snippets are going to be copied
|
25 | 47 |
|
|
33 | 55 | # This section verifies if the snippets already exist
|
34 | 56 |
|
35 | 57 | - name: Verifying if the global dhcp snippet exists
|
36 |
| - command: bash -c "maas admin dhcpsnippets read|grep 'global_dhcp'" |
| 58 | + command: bash -c "maas {{ maas_admin_username }} dhcpsnippets read|grep 'global_dhcp'" |
37 | 59 | ignore_errors: true
|
38 | 60 | register: global_exists
|
39 | 61 |
|
40 | 62 | - name: Verifying if the {{ subnet_name }} subnet classes snippet exists
|
41 |
| - command: bash -c "maas admin dhcpsnippets read |grep '{{ subnet_name }}_classes'" |
| 63 | + command: bash -c "maas {{ maas_admin_username }} dhcpsnippets read |grep '{{ subnet_name }}_classes'" |
42 | 64 | when: subnet_data.classes is defined
|
43 | 65 | ignore_errors: true
|
44 | 66 | register: classes_exists
|
45 | 67 |
|
46 | 68 | - name: Verifying if the {{ subnet_name }} subnet pools snippet exists
|
47 |
| - command: bash -c "maas admin dhcpsnippets read |grep '{{ subnet_name }}_pools'" |
| 69 | + command: bash -c "maas {{ maas_admin_username }} dhcpsnippets read |grep '{{ subnet_name }}_pools'" |
48 | 70 | when: subnet_data.pools is defined
|
49 | 71 | ignore_errors: true
|
50 | 72 | register: pools_exists
|
51 | 73 |
|
52 | 74 | - name: Verifying if the {{ subnet_name }} subnet hosts snippet exists
|
53 |
| - command: bash -c "maas admin dhcpsnippets read |grep '{{ subnet_name }}_hosts'" |
| 75 | + command: bash -c "maas {{ maas_admin_username }} dhcpsnippets read |grep '{{ subnet_name }}_hosts'" |
54 | 76 | ignore_errors: true
|
55 | 77 | register: hosts_exists
|
56 | 78 |
|
|
131 | 153 | # This section deletes the snippets if already exist
|
132 | 154 |
|
133 | 155 | - name: Delete global DHCP snippet if already exists
|
134 |
| - command: "maas admin dhcpsnippet delete global_dhcp" |
| 156 | + command: "maas {{ maas_admin_username }} dhcpsnippet delete global_dhcp" |
135 | 157 | when: dhcp_global_config.changed == true and global_exists.failed == false
|
136 | 158 |
|
137 | 159 | - name: Delete {{ subnet_name }} subnet classes snippet if already exists
|
138 |
| - command: "maas admin dhcpsnippet delete {{ subnet_name }}_classes" |
| 160 | + command: "maas {{ maas_admin_username }} dhcpsnippet delete {{ subnet_name }}_classes" |
139 | 161 | when: subnet_data.classes is defined and dhcp_classes_config.changed == true and classes_exists.failed == false
|
140 | 162 |
|
141 | 163 | - name: Delete {{ subnet_name }} subnet pools snippet if already exists
|
142 |
| - command: "maas admin dhcpsnippet delete {{ subnet_name }}_pools" |
| 164 | + command: "maas {{ maas_admin_username }} dhcpsnippet delete {{ subnet_name }}_pools" |
143 | 165 | when: subnet_data.pools is defined and dhcp_pools_config.changed == true and pools_exists.failed == false
|
144 | 166 |
|
145 | 167 | - name: Delete {{ subnet_name }} subnet hosts snippet if already exists
|
146 |
| - command: "maas admin dhcpsnippet delete {{ subnet_name }}_hosts" |
| 168 | + command: "maas {{ maas_admin_username }} dhcpsnippet delete {{ subnet_name }}_hosts" |
147 | 169 | when: dhcp_hosts_config.changed == true and hosts_exists.failed == false
|
148 | 170 |
|
149 | 171 | # This section adds snippets into MAAS
|
|
153 | 175 | when: dhcp_global_config.failed == false and dhcp_global_config.changed == true
|
154 | 176 |
|
155 | 177 | - name: Adding {{ subnet_name }} classes snippets into MAAS
|
156 |
| - command: "maas {{ maas_admin_username }} dhcpsnippets create name='{{ subnet_name }}_classes' value='{{ classes_content }}' description='This snippet configures the classes in {{ subnet_name }} subnet' subnet='{{ subnet_id.stdout }}'" |
| 178 | + command: "maas {{ maas_admin_username }} dhcpsnippets create name='{{ subnet_name }}_classes' value='{{ classes_content }}' description='This snippet configures the classes in {{ subnet_name }} subnet' subnet='{{ vlan_id }}'" |
157 | 179 | when: dhcp_classes_config.failed == false and dhcp_classes_config.changed == true
|
158 | 180 |
|
159 | 181 | - name: Adding {{ subnet_name }} pools snippets into MAAS
|
160 |
| - command: "maas {{ maas_admin_username }} dhcpsnippets create name='{{ subnet_name }}_pools' value='{{ pools_content }}' description='This snippet configures the pools in {{ subnet_name }} subnet' subnet='{{ subnet_id.stdout }}'" |
| 182 | + command: "maas {{ maas_admin_username }} dhcpsnippets create name='{{ subnet_name }}_pools' value='{{ pools_content }}' description='This snippet configures the pools in {{ subnet_name }} subnet' subnet='{{ vlan_id }}'" |
161 | 183 | when: dhcp_pools_config.failed == false and dhcp_pools_config.changed == true
|
162 | 184 |
|
163 | 185 | - name: Adding {{ subnet_name }} hosts snippets into MAAS
|
164 |
| - command: "maas {{ maas_admin_username }} dhcpsnippets create name='{{ subnet_name }}_hosts' value='{{ hosts_content }}' description='This snippet configures the hosts in {{ subnet_name }} subnet' subnet='{{ subnet_id.stdout }}'" |
| 186 | + command: "maas {{ maas_admin_username }} dhcpsnippets create name='{{ subnet_name }}_hosts' value='{{ hosts_content }}' description='This snippet configures the hosts in {{ subnet_name }} subnet' subnet='{{ vlan_id }}'" |
165 | 187 | when: dhcp_hosts_config.failed == false and dhcp_hosts_config.changed == true
|
0 commit comments