Optimize PXE config templating

This commit is contained in:
Khue Doan 2021-03-03 01:25:56 +07:00
parent e6341de6e7
commit acb3dd03af
9 changed files with 104 additions and 82 deletions

View File

@ -1,5 +1,6 @@
iso_url: "https://mirrors.nhanhoa.com/centos/8.3.2011/isos/x86_64/CentOS-8.3.2011-x86_64-dvd1.iso"
disk: "sda"
dns_server: "8.8.8.8"
network_interface: "eno1"
ssh_public_key: "{{lookup('file', '~/.ssh/id_rsa.pub') }}"

View File

@ -1,5 +1,6 @@
- name: Install OS on empty nodes
hosts: controller
hosts: all
gather_facts: no
vars_prompt:
- name: username
prompt: Enter username for metal nodes

View File

@ -1,72 +1,12 @@
- name: Create build directory
file:
path: "{{ item }}"
state: directory
loop:
- "{{ role_path }}/build"
- "{{ role_path }}/build/images"
- "{{ role_path }}/build/mnt"
- name: Render configs and start PXE server
include_tasks:
file: server.yml
run_once: yes
when:
- "'controller' in group_names"
# - name: Download ISO
# get_url:
# url: "{{ iso_url }}"
# dest: "{{ role_path }}/build/images/centos8.iso"
- name: Mount the ISO
mount:
src: "{{ role_path }}/build/images/centos8.iso"
path: "{{ role_path }}/build/mnt"
fstype: iso9660
opts: loop,ro
state: mounted
become: yes
- name: Copy configs
copy:
src: "{{ role_path }}/templates/"
dest: "{{ role_path }}/build"
- name: Render DHCP config
template:
src: dhcp/dhcpd.conf.j2
dest: "{{ role_path }}/build/dhcp/dhcpd.conf"
- name: Render GRUB config
template:
src: tftp/tftpboot/grub.cfg.j2
dest: "{{ role_path }}/build/tftp/tftpboot/grub.cfg"
- name: Render shared kickstart config
template:
src: http/kickstart/centos8.ks.j2
dest: "{{ role_path }}/build/http/kickstart/centos8.ks"
- name: Render seperate network kickstart config for each node
template:
src: http/kickstart/network/network.ks.j2
dest: "{{ role_path }}/build/http/kickstart/network/{{ hostvars[item]['mac'] }}.ks"
loop: "{{ groups['metal'] }}"
- name: Start PXE server
docker_compose:
project_src: "{{ role_path }}/build"
build: yes
delegate_to: localhost
- name: Unconditionally shut down the machine with all defaults
community.general.shutdown:
delay: 0
- name: Wait for the machine to shutdown
wait_for:
timeout: 30
delegate_to: localhost
- name: Send magic Wake-on-LAN packet
community.general.wakeonlan:
mac: "{{ hostvars[inventory_hostname]['mac'] }}"
delegate_to: localhost
- name: Wait for the servers to comes up
wait_for_connection:
timeout: 600
- name: Wakes the nodes up and install OS on them
include_tasks:
file: wake.yml
when:
- "'metal' in group_names"

View File

@ -0,0 +1,62 @@
- name: Gather network facts
ansible.builtin.setup:
gather_subset:
- network
- name: Create build directory
file:
path: "{{ item }}"
state: directory
loop:
- "{{ role_path }}/build"
- "{{ role_path }}/build/images"
- "{{ role_path }}/build/mnt"
# - name: Download ISO
# get_url:
# url: "{{ iso_url }}"
# dest: "{{ role_path }}/build/images/centos8.iso"
# delegate_to: localhost
- name: Mount the ISO
mount:
src: "{{ role_path }}/build/images/centos8.iso"
path: "{{ role_path }}/build/mnt"
fstype: iso9660
opts: loop,ro
state: mounted
become: yes
- name: Copy configs
copy:
src: "{{ role_path }}/templates/"
dest: "{{ role_path }}/build"
- name: Render DHCP config
template:
src: dhcp/dhcpd.conf.j2
dest: "{{ role_path }}/build/dhcp/dhcpd.conf"
- name: Render GRUB config
template:
src: tftp/tftpboot/grub.cfg.j2
dest: "{{ role_path }}/build/tftp/tftpboot/grub.cfg"
- name: Render shared kickstart config
template:
src: http/kickstart/centos8.ks.j2
dest: "{{ role_path }}/build/http/kickstart/centos8.ks"
- name: Render seperate network kickstart config for each node
template:
src: http/kickstart/network/network.ks.j2
dest: "{{ role_path }}/build/http/kickstart/network/{{ hostvars[item]['mac'] }}.ks"
loop: "{{ groups['metal'] }}"
- name: Start PXE server
docker_compose:
project_src: "{{ role_path }}/build"
state: present
restarted: yes
build: yes
recreate: always

View File

@ -0,0 +1,18 @@
- name: Unconditionally shut down the machine with all defaults
community.general.shutdown:
delay: 0
ignore_unreachable: yes
- name: Wait for the machine to shutdown
wait_for:
timeout: 30
delegate_to: localhost
- name: Send magic Wake-on-LAN packet
community.general.wakeonlan:
mac: "{{ hostvars[inventory_hostname]['mac'] }}"
delegate_to: localhost
- name: Wait for the servers to comes up
wait_for_connection:
timeout: 600

View File

@ -5,13 +5,13 @@ option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
option architecture-type code 93 = unsigned integer 16;
subnet {{ subnet }} netmask {{ netmask }} {
option routers {{ gateway }};
range {{ range_start }} {{ range_end }};
subnet {{ ansible_default_ipv4.network }} netmask {{ ansible_default_ipv4.netmask }} {
option routers {{ ansible_default_ipv4.gateway }};
range {{ ansible_default_ipv4.gateway | ansible.netcommon.ipmath(1) }} {{ ansible_default_ipv4.broadcast | ansible.netcommon.ipmath(-1) }};
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
next-server {{ pxe_server }};
next-server {{ ansible_default_ipv4.address }};
if option architecture-type = 00:07 {
filename "grubx64.efi";

View File

@ -1,6 +1,6 @@
%pre --interpreter=/bin/sh
mac=$(ip --brief link show dev {{ network_interface }} | tr -s ' ' | cut -d ' ' -f 3 | sed 's/:/-/g')
curl "http://{{ pxe_server }}/kickstart/network/$mac.ks" > /tmp/network.ks
curl "http://{{ ansible_default_ipv4.address }}/kickstart/network/$mac.ks" > /tmp/network.ks
%end
#version=RHEL8
@ -17,9 +17,9 @@ lang en_US.UTF-8
# Network information
%include /tmp/network.ks
repo --name="AppStream" --baseurl=http://{{ pxe_server }}/CentOS/AppStream
repo --name="AppStream" --baseurl=http://{{ ansible_default_ipv4.address }}/CentOS/AppStream
# Use network installation
url --url="http://{{ pxe_server }}/CentOS/"
url --url="http://{{ ansible_default_ipv4.address }}/CentOS/"
# Disable Setup Agent on first boot
firstboot --disable
# Do not configure the X Window System

View File

@ -1,2 +1,2 @@
network --bootproto=static --device={{ network_interface }} --ip={{ hostvars[item]['ansible_host'] }} --gateway={{ gateway }} --nameserver={{ dns }} --netmask={{ netmask }} --ipv6=auto --activate
network --hostname={{ hostvars[item]['inventory_hostname'] }}
network --bootproto=static --device={{ network_interface }} --ip={{ hostvars[item]['ansible_host'] }} --gateway={{ ansible_default_ipv4.gateway }} --nameserver={{ dns_server }} --netmask={{ ansible_default_ipv4.netmask }} --ipv6=auto --activate
network --hostname={{ hostvars[item]['inventory_hostname'] }}

View File

@ -1,5 +1,5 @@
set timeout=5
menuentry 'CentOS' {
linuxefi vmlinuz ip=dhcp inst.repo=http://{{ pxe_server }}/CentOS ks=http://{{ pxe_server }}/kickstart/centos8.ks
linuxefi vmlinuz ip=dhcp inst.repo=http://{{ ansible_default_ipv4.address }}/CentOS ks=http://{{ ansible_default_ipv4.address }}/kickstart/centos8.ks
initrdefi initrd.img
}