Skip to content

Commit 959080c

Browse files
committed
Adding extra code to the DHCP task
1 parent 950b209 commit 959080c

File tree

5 files changed

+124
-63
lines changed

5 files changed

+124
-63
lines changed

roles/maas/tasks/config_dhcpd.yml

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
11
---
2-
- name: Configure MAAS DHCP Service
3-
when: inventory_hostname in groups['maas_region_rack_server'] and maas_installed.failed == false and logged_into_maas.rc == 0
4-
block:
5-
- name: Get a list of all hosts from the inventory
6-
set_fact:
7-
all_list: "{{ groups['all'] }}"
8-
register: all_nodes_listed
9-
10-
- name: Debugging subnet variable
11-
debug:
12-
var: "{{ item }}"
13-
14-
- name: Creating IP Range
15-
command: "maas admin ipranges create type={{ subnet.ip_range_type }} start_ip={{ subnet.start_ip }} end_ip={{ subnet.end_ip }}"
16-
ignore_errors: true
17-
18-
- name: Selecting the correct fabric to configure
19-
command: maas admin subnet read {{ subnet.subnet }}|grep 'fabric-'|cut -d '"' -f 4
20-
register: fabric_selected
21-
22-
- name: Getting subnet ID
23-
command: maas admin subnet read {{ subnet.subnet }}|grep '"id"'|cut -d ':' -f 2|cut -d ',' -f 1|head -1
24-
register: subnet_id
25-
26-
- name: Enabling DHCP with HA on the selected subnet
27-
command: "maas admin vlan update {{ fabric_selected.stdout }} 0 dhcp_on=True primary_rack={{ hostvars[groups['maas_region_rack_server'].0] }} secondary_rack={{ hostvars[groups['maas_rack_server'].0] }}"
28-
29-
- name: Creating temporary snippets directory
30-
file:
31-
path: /tmp/snippets
32-
state: directory
33-
mode: '0755'
34-
register: snippets_directory
35-
36-
- name: Copying each subnet classes snippet
37-
template:
38-
src: dhcpd.classes.conf.j2
39-
dest: "/tmp/snippets/{{ subnet }}_classes_snippet"
40-
when: subnet.classes is defined and snippets_directory.failed == false
41-
register: dhcp_classes_config
42-
43-
# - name: Creating DHCP snippets for the classes
44-
# command: maas admin dhcpsnippets create name=class_{{ item.key }} value="class ""{{ item.key }}"" {
45-
# "{{ item.value }}";
46-
# }" description='DHCP config for class "{{ item.key }}"" subnet="{{ subnet_id.stdout }}"'
47-
# ignore_errors: true
48-
# loop: "{{ subnet.classes | dict2items }}"
49-
50-
# - name: Creating DHCP snippets for the pools
51-
52-
# - name: Creating DHCP snippets for all hosts
53-
# command: "maas admin dhcpsnippets create name="{{ item }}" value="host {{ item }} {
54-
# hardware ethernet {{ hostvars[item]['mac'] }};
55-
# fixed-address {{ hostvars[item]['ip'] }};
56-
# }" description="DHCP config for host {{ item }}" subnet="{{ subnet_id.stdout }}""
57-
# with_items: "{{ all_list }}"
58-
# ignore_errors: true
59-
# when: hostvars[item]['mac'] is defined
2+
- name: Creating IP Range
3+
command: "maas admin ipranges create type={{ subnet_data.ip_range_type }} start_ip={{ subnet_data.start_ip }} end_ip={{ subnet_data.end_ip }}"
4+
ignore_errors: true
5+
tags: skip
6+
7+
- name: Selecting the correct fabric to configure
8+
command: bash -c "maas admin subnet read {{ subnet_data.cidr }}|grep 'fabric-'|cut -d '\"' -f 4"
9+
register: fabric_selected
10+
11+
- name: Getting subnet ID
12+
command: bash -c "maas admin subnet read {{ subnet_data.cidr }}|grep '\"id\"'|cut -d ':' -f 2|cut -d ',' -f 1|head -1"
13+
register: subnet_id
14+
15+
- name: Enabling DHCP with HA on the selected subnet
16+
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] }}"
17+
18+
- name: Creating temporary snippets directory
19+
file:
20+
path: /tmp/snippets
21+
state: directory
22+
mode: '0755'
23+
register: snippets_directory
24+
25+
- name: Copying each subnet classes snippet
26+
template:
27+
src: dhcpd.classes.conf.j2
28+
dest: "/tmp/snippets/{{ subnet_name }}_classes_snippet"
29+
when: subnet_data.classes is defined and snippets_directory.failed == false
30+
register: dhcp_classes_config
31+
32+
- name: Copying each subnet pools snippet
33+
template:
34+
src: dhcpd.pools.conf.j2
35+
dest: "/tmp/snippets/{{ subnet_name }}_pools_snippet"
36+
when: subnet_data.pools is defined and snippets_directory.failed == false
37+
register: dhcp_pools_config
38+
39+
- name: Copying each subnet hosts snippet
40+
template:
41+
src: dhcpd.hosts.conf.j2
42+
dest: "/tmp/snippets/{{ subnet_name }}_hosts_snippet"
43+
when: snippets_directory.failed == false
44+
register: dhcp_hosts_config
45+
46+
- name: Slurp classes file content
47+
slurp:
48+
src: "/tmp/snippets/{{ subnet_name }}_classes_snippet"
49+
register: classes_file
50+
51+
- name: Decode and show file contents
52+
set_fact:
53+
classes_content: "{{ classes_file.content | b64decode }}"
54+
55+
- name: Debugging classes_content
56+
debug:
57+
var: classes_content
58+
59+
- name: Adding classes snippets into MAAS
60+
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 }}'"
61+
when: dhcp_classes_config.failed == false
62+
ignore_errors: true

roles/maas/tasks/main.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
apt:
1010
name: snapd
1111
state: present
12+
tags: skip
13+
1214

1315
- name: Ensure system is up-to-date
1416
apt:
1517
update_cache: yes
18+
tags: skip
19+
1620

1721
# Install and configure the MAAS DB
1822
- import_tasks: install_maasdb.yml
23+
tags: skip
1924

2025
# Install MAAS
2126
- name: Install MAAS with Snap
@@ -25,28 +30,38 @@
2530
channel: "{{ maas_version }}/{{ maas_snap_channel }}"
2631
state: present
2732
register: maas_installed
33+
tags: skip
2834

2935
# Initialize MAAS
3036
- import_tasks: initialize_region_rack.yml
37+
tags: skip
38+
3139
- import_tasks: initialize_secondary_rack.yml
40+
tags: skip
3241

3342
# Logging into the MAAS API to use CLI
3443

3544
- name: Get API key
3645
command: maas apikey --username={{ maas_admin_username }}
3746
when: inventory_hostname in groups['maas_region_rack_server']
3847
register: maas_api_key
48+
tags: skip
3949

4050
- name: Logging into MAAS API
4151
command: "maas login {{ maas_admin_username }} http://{{ hostvars[groups['maas_region_rack_server'].0]['ip'] }}:5240/MAAS/api/2.0/ {{ maas_api_key.stdout }}"
4252
when: inventory_hostname in groups['maas_region_rack_server']
4353
register: logged_into_maas
54+
tags: skip
4455

4556
# Configure DHCP Service
46-
- include_tasks: config_dhcpd.yml
47-
loop: "{{ dhcp_subnets }}"
48-
# loop_control:
49-
# loop_var: subnet
57+
- name: dhcp_configuration
58+
include_tasks: config_dhcpd.yml
59+
loop: "{{ dhcp_maas_subnets|dict2items }}"
60+
loop_control:
61+
loop_var: subnet
62+
vars:
63+
subnet_name: "{{ subnet.key }}"
64+
subnet_data: "{{ subnet.value }}"
5065

5166
# Add Machines into MAAS
5267
- import_tasks: add_machines.yml

roles/maas/templates/dhcpd.classes.conf.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% for subnet, subnet_item in dhcp_subnets.items() %}
1+
{% for subnet, subnet_item in dhcp_maas_subnets.items() %}
22
{% if subnet_item.classes is defined -%}
33
{% for class_name, class_string in subnet_item.classes.items() -%}
44
class "{{ class_name }}" {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% for subnet, subnet_item in dhcp_maas_subnets.items() %}
2+
{% for host in groups['all'] | sort | unique -%}
3+
{% if hostvars[host][subnet_item.macvar] is defined -%}
4+
{% if hostvars[host][subnet_item.ipvar] | ansible.utils.ipaddr(subnet_item.cidr) -%}
5+
host {{ host.split('.')[0] }}-{{ subnet }} {
6+
{% if hostvars[host]['domain_name_servers'] is defined -%}
7+
option domain-name-servers {{ hostvars[host]['domain_name_servers']|join(', ') }};
8+
{% endif -%}
9+
hardware ethernet {{ hostvars[host][subnet_item.macvar] }};
10+
fixed-address {{ hostvars[host][subnet_item.ipvar] }};
11+
{% if hostvars[host]['dhcp_option_hostname'] is defined and hostvars[host]['dhcp_option_hostname'] == true %}
12+
option host-name "{{ host.split('.')[0] }}";
13+
{% endif -%}
14+
}
15+
{% endif -%}
16+
{% endif -%}
17+
{% endfor -%}
18+
{% endfor %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% for subnet, subnet_item in dhcp_maas_subnets.items() %}
2+
{% if subnet_item.pools is defined -%}
3+
{% for pool, pool_value in subnet_item.pools.items() -%}
4+
pool {
5+
{% if pool == "unknown_clients" -%}
6+
allow unknown-clients;
7+
{% else -%}
8+
allow members of "{{ pool }}";
9+
{% endif -%}
10+
{% if pool_value.range is string -%}
11+
range {{ pool_value.range }};
12+
{% else -%}
13+
range {{ pool_value.range|join(';\n range ') }};
14+
{% endif -%}
15+
{% if pool_value.next_server is defined -%}
16+
next-server {{ pool_value.next_server }};
17+
{% endif -%}
18+
{% if pool_value.filename is defined -%}
19+
filename "{{ pool_value.filename }}";
20+
{% endif -%}
21+
}
22+
23+
{% endfor -%}
24+
{%- endif -%}
25+
{% endfor %}

0 commit comments

Comments
 (0)