Skip to content

Commit 776ba11

Browse files
committed
Changing some variables and adding an IP validation [in-progress]
1 parent 94f488e commit 776ba11

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

roles/maas/tasks/config_dhcpd.yml

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,44 @@
44
tags: config_dhcp
55
block:
66
# 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+
729
- 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 }}"
931
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') }}"
1942

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] }}"
2345

2446
# This task creates the directory where the snippets are going to be copied
2547

@@ -33,24 +55,24 @@
3355
# This section verifies if the snippets already exist
3456

3557
- 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'"
3759
ignore_errors: true
3860
register: global_exists
3961

4062
- 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'"
4264
when: subnet_data.classes is defined
4365
ignore_errors: true
4466
register: classes_exists
4567

4668
- 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'"
4870
when: subnet_data.pools is defined
4971
ignore_errors: true
5072
register: pools_exists
5173

5274
- 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'"
5476
ignore_errors: true
5577
register: hosts_exists
5678

@@ -131,19 +153,19 @@
131153
# This section deletes the snippets if already exist
132154

133155
- 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"
135157
when: dhcp_global_config.changed == true and global_exists.failed == false
136158

137159
- 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"
139161
when: subnet_data.classes is defined and dhcp_classes_config.changed == true and classes_exists.failed == false
140162

141163
- 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"
143165
when: subnet_data.pools is defined and dhcp_pools_config.changed == true and pools_exists.failed == false
144166

145167
- 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"
147169
when: dhcp_hosts_config.changed == true and hosts_exists.failed == false
148170

149171
# This section adds snippets into MAAS
@@ -153,13 +175,13 @@
153175
when: dhcp_global_config.failed == false and dhcp_global_config.changed == true
154176

155177
- 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 }}'"
157179
when: dhcp_classes_config.failed == false and dhcp_classes_config.changed == true
158180

159181
- 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 }}'"
161183
when: dhcp_pools_config.failed == false and dhcp_pools_config.changed == true
162184

163185
- 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 }}'"
165187
when: dhcp_hosts_config.failed == false and dhcp_hosts_config.changed == true

0 commit comments

Comments
 (0)