Skip to content

Commit 998b7df

Browse files
committed
Adding DHCP config task (incomplete)
1 parent 3dd6063 commit 998b7df

File tree

7 files changed

+128
-38
lines changed

7 files changed

+128
-38
lines changed

roles/maas/defaults/main.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,20 @@ maas_db_name: "maasdb"
55
maas_db_user: "maas"
66
maas_version: "3.5"
77
maas_snap_channel: "stable"
8+
9+
# MAAS DHCP variables
10+
dhcp_subnets:
11+
front:
12+
subnet: 10.0.8.0/24
13+
classes:
14+
virtual: "match if substring(hardware, 0, 4) = 01:52:54:00"
15+
lxc: "match if substring(hardware, 0, 4) = 01:52:54:ff"
16+
pools:
17+
virtual:
18+
range: 172.21.10.20 172.21.10.250
19+
unknown_clients:
20+
range:
21+
- 172.21.11.0 172.21.11.19
22+
- 172.21.13.170 172.21.13.250
23+
lxc:
24+
range: 172.21.14.1 172.21.14.200

roles/maas/tasks/add_machines.yml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@
22
- name: Add all machines from inventory to MAAS
33
vars:
44
arch: "amd64" # Change if needed
5-
when: inventory_hostname in groups['maas_region_rack_server'] and maas_installed.failed == false
5+
when: inventory_hostname in groups['maas_region_rack_server'] and maas_installed.failed == false and logged_into_maas.rc == 0
66
block:
7-
- name: Get a list of all hosts from the inventory
7+
- name: Get a list of testnodes from the inventory
88
set_fact:
9-
host_list: "{{ groups['testnodes'] }}"
10-
register: nodes_listed
11-
12-
- name: Get API key file
13-
command: maas apikey --username={{ maas_admin_username }}
14-
register: maas_api_key
15-
16-
- name: Logging into MAAS API
17-
command: "maas login {{ maas_admin_username }} http://{{ hostvars[groups['maas_region_rack_server'].0]['ip'] }}:5240/MAAS/api/2.0/ {{ maas_api_key.stdout }}"
18-
register: logged_into_maas
9+
testnodes_list: "{{ groups['testnodes'] }}"
10+
register: testnodes_listed
1911

2012
- name: Add machines to MAAS
2113
command: "maas {{ maas_admin_username }} machines create deployed=true hostname={{ item }} architecture={{ arch }} mac_addresses={{ hostvars[item]['mac'] }} power_type=manual"
22-
with_items: "{{ host_list }}"
14+
with_items: "{{ testnodes_list }}"
2315
ignore_errors: true
24-
when: hostvars[item]['mac'] is defined and logged_into_maas.rc == 0 and nodes_listed.changed == true
16+
when: hostvars[item]['mac'] is defined and testnodes_listed.changed == true

roles/maas/tasks/config_dhcpd.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
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

roles/maas/tasks/install_maas_region.yml renamed to roles/maas/tasks/initialize_region_rack.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
---
2-
- name: Install MAAS with Snap
3-
snap:
4-
name: maas
5-
classic: yes
6-
channel: "{{ maas_version }}/{{ maas_snap_channel }}"
7-
state: present
8-
register: maas_installed
9-
102
- name: Initialize MAAS Region + Rack Controller
113
when: inventory_hostname in groups['maas_region_rack_server'] and maas_installed.failed == false and maas_installed.changed == true
124
block:
@@ -29,10 +21,7 @@
2921
- name: Create MAAS admin user
3022
command: "sudo maas createadmin --username={{ maas_admin_username }} --password={{ maas_admin_password }} --email={{ maas_admin_email }}"
3123
ignore_errors: true
24+
register: admin_user_created
3225

3326
- name: Restart MAAS services
3427
command: "snap restart maas"
35-
36-
- name: Generate secret variable
37-
command: "cat /var/snap/maas/common/maas/secret"
38-
register: secret_var

roles/maas/tasks/install_maas_rack.yml renamed to roles/maas/tasks/initialize_secondary_rack.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
---
2-
- name: Install MAAS with Snap in Rack Server
3-
snap:
4-
name: maas
5-
classic: yes
6-
channel: "{{ maas_version }}/{{ maas_snap_channel }}"
7-
state: present
8-
when: inventory_hostname in groups['maas_rack_server']
9-
register: maas_rack_installed
2+
- name: Generate secret variable
3+
command: "cat /var/snap/maas/common/maas/secret"
4+
when: inventory_hostname in groups['maas_region_rack_server']
5+
register: secret_var
106

117
- name: Initialize MAAS Rack Controller
12-
when: inventory_hostname in groups['maas_rack_server'] and maas_rack_installed.failed == false and secret_var is defined and maas_rack_installed.changed == true
8+
when: inventory_hostname in groups['maas_rack_server'] and maas_installed.failed == false and secret_var is defined and maas_installed.changed == true
139
block:
1410
- name: Removing conflicting NTP
1511
apt:

roles/maas/tasks/main.yml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,36 @@
1717
# Install and configure the MAAS DB
1818
- import_tasks: install_maasdb.yml
1919

20-
# Install and initialize MAAS
21-
- import_tasks: install_maas_region.yml
22-
- import_tasks: install_maas_rack.yml
20+
# Install MAAS
21+
- name: Install MAAS with Snap
22+
snap:
23+
name: maas
24+
classic: yes
25+
channel: "{{ maas_version }}/{{ maas_snap_channel }}"
26+
state: present
27+
register: maas_installed
28+
29+
# Initialize MAAS
30+
- import_tasks: initialize_region_rack.yml
31+
- import_tasks: initialize_secondary_rack.yml
32+
33+
# Logging into the MAAS API to use CLI
34+
35+
- name: Get API key
36+
command: maas apikey --username={{ maas_admin_username }}
37+
when: inventory_hostname in groups['maas_region_rack_server']
38+
register: maas_api_key
39+
40+
- name: Logging into MAAS API
41+
command: "maas login {{ maas_admin_username }} http://{{ hostvars[groups['maas_region_rack_server'].0]['ip'] }}:5240/MAAS/api/2.0/ {{ maas_api_key.stdout }}"
42+
when: inventory_hostname in groups['maas_region_rack_server']
43+
register: logged_into_maas
44+
45+
# Configure DHCP Service
46+
- include_tasks: config_dhcpd.yml
47+
loop: "{{ dhcp_subnets }}"
48+
# loop_control:
49+
# loop_var: subnet
2350

2451
# Add Machines into MAAS
2552
- import_tasks: add_machines.yml
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% for subnet, subnet_item in dhcp_subnets.items() %}
2+
{% if subnet_item.classes is defined -%}
3+
{% for class_name, class_string in subnet_item.classes.items() -%}
4+
class "{{ class_name }}" {
5+
{{ class_string }};
6+
}
7+
8+
{% endfor -%}
9+
{%- endif -%}
10+
{% endfor %}

0 commit comments

Comments
 (0)