From acd74d0fc1ad910e8414cf130a07f704fe360390 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Sachse?= <Joerg.Sachse@slub-dresden.de>
Date: Fri, 30 Oct 2020 15:23:31 +0100
Subject: [PATCH] feat: first working implementation of install ISO creation

---
 site.yml                  |  65 +++++++++++++++++----
 templates/isolinux.cfg.j2 | 117 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 172 insertions(+), 10 deletions(-)
 create mode 100644 templates/isolinux.cfg.j2

diff --git a/site.yml b/site.yml
index bb7b5f0..1398068 100644
--- a/site.yml
+++ b/site.yml
@@ -1,31 +1,76 @@
 ---
 - hosts: localhost
   connection: local
+  vars:
+    - ISO: "RHEL79"
+  vars_prompt:
+    - name: install_hostname
+      prompt: "What's the hostname of the system that you're trying to install? Use ONLY the hostname, NOT the FQDN!"
+      private: false
   tasks:
-    - name: check if listfile for networking exists
+    ### PREPARATION
+    - name: create build directory
+      file:
+        path: "{{ playbook_dir }}/build/"
+        state: directory
+    - name: mount boot ISO
+      mount:
+        fstype: "iso9660"
+        opts: "loop"
+        path: "{{ playbook_dir }}/iso/"
+        src: "{{ playbook_dir }}/rhel-server-7.9-x86_64-dvd.iso"
+        fstab: "/tmp/fstab"
+        state: mounted
+    - name: copy ISO contents
+      shell:
+        cmd: 'cp -avRf "{{ playbook_dir }}/iso" "{{ playbook_dir }}/build"'
+      ignore_errors: true
+    - name: check if listfile for networking exists...
       find:
         paths: "{{ playbook_dir }}/vars/"
         patterns: "network.yml"
       register: result_network
-    - debug:
-        msg: "{{ item.path }}"
-      loop: "{{ result_network.files }}"
-    - name: bail out if config file doesn't exist
+    - name: ... and bail out if config file doesn't exist
       fail:
         msg: "The required config file 'vars/network.yml' couldn't be found. Please refer to vars/network.yml.example for further documentation."
       when: "result_network.matched < 1"
     - name: include networking variables
       include_vars: "{{ item.path }}"
       loop: "{{ result_network.files }}"
-    - name: create build directory
-      file:
-        path: "{{ playbook_dir }}/build/"
-        state: directory
+
+    ### CREATE CONFIGS
     - name: write Kickstart files
       template:
         src: "kickstart.cfg.j2"
-        dest: "build/{{ item.hostname }}"
+        dest: "build/{{ item.hostname }}.cfg"
+      loop: "{{ hosts }}"
+    - name: template isolinux config
+      template:
+        src: "isolinux.cfg.j2"
+        dest: "{{ playbook_dir }}/build/isolinux/isolinux.cfg"
       loop: "{{ hosts }}"
+      when: "install_hostname in item.hostname"
+
+    ### CREATE ISO INSTALLATION IMAGE
+    - name: create ISO image
+      command:
+        cmd: 'xorrisofs -output {{ playbook_dir }}/build/{{ ISO }}.iso -eltorito-boot isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -volid "{{ ISO }}" -rational-rock {{ playbook_dir }}/build/'
+        chdir: "{{ playbook_dir }}/build"
+    - name: make ISO bootable
+      command: 'isohybrid -v {{ playbook_dir }}/build/{{ ISO }}.iso'
+
+    ### CLEANUP
+    - name: umount boot ISO
+      mount:
+        path: "{{ playbook_dir }}/iso"
+        state: unmounted
+    - name: clean up
+      file:
+        path: "{{ item }}"
+        state: absent
+      loop:
+        - "/tmp/fstab"
+        - "{{ playbook_dir }}/iso/"
 
 #- hosts: "*"
 #  pre_tasks:
diff --git a/templates/isolinux.cfg.j2 b/templates/isolinux.cfg.j2
new file mode 100644
index 0000000..fdbd290
--- /dev/null
+++ b/templates/isolinux.cfg.j2
@@ -0,0 +1,117 @@
+default vesamenu.c32
+timeout 600
+
+display boot.msg
+
+# Clear the screen when exiting the menu, instead of leaving the menu displayed.
+# For vesamenu, this means the graphical background is still displayed without
+# the menu itself for as long as the screen remains in graphics mode.
+menu clear
+menu background splash.png
+menu title Red Hat Enterprise Linux 7.9
+menu vshift 8
+menu rows 18
+menu margin 8
+#menu hidden
+menu helpmsgrow 15
+menu tabmsgrow 13
+
+# Border Area
+menu color border * #00000000 #00000000 none
+
+# Selected item
+menu color sel 0 #ffffffff #00000000 none
+
+# Title bar
+menu color title 0 #ff7ba3d0 #00000000 none
+
+# Press [Tab] message
+menu color tabmsg 0 #ff3a6496 #00000000 none
+
+# Unselected menu item
+menu color unsel 0 #84b8ffff #00000000 none
+
+# Selected hotkey
+menu color hotsel 0 #84b8ffff #00000000 none
+
+# Unselected hotkey
+menu color hotkey 0 #ffffffff #00000000 none
+
+# Help text
+menu color help 0 #ffffffff #00000000 none
+
+# A scrollbar of some type? Not sure.
+menu color scrollbar 0 #ffffffff #ff355594 none
+
+# Timeout msg
+menu color timeout 0 #ffffffff #00000000 none
+menu color timeout_msg 0 #ffffffff #00000000 none
+
+# Command prompt text
+menu color cmdmark 0 #84b8ffff #00000000 none
+menu color cmdline 0 #ffffffff #00000000 none
+
+# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
+
+menu tabmsg Press Tab for full configuration options on menu items.
+
+menu separator # insert an empty line
+menu separator # insert an empty line
+
+##### Begin INSERTED FOR ANSIBLE ROLE #####
+label netinstall
+  menu label Install Red Hat Enterprise Linux 7.9 ^Netinstall Server
+  menu default
+  kernel vmlinuz
+  append initrd=initrd.img inst.ks=cdrom:/{{ item.hostname }}.cfg inst.stage2=hd:LABEL=RHEL79 quiet
+#####  End  INSERTED FOR ANSIBLE ROLE #####
+
+menu separator # insert an empty line
+
+# utilities submenu
+menu begin ^Troubleshooting
+  menu title Troubleshooting
+
+label vesa
+  menu indent count 5
+  menu label Install Red Hat Enterprise Linux 7.9 in ^basic graphics mode
+  text help
+	Try this option out if you're having trouble installing
+	Red Hat Enterprise Linux 7.9.
+  endtext
+  kernel vmlinuz
+  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 xdriver=vesa nomodeset quiet
+
+label rescue
+  menu indent count 5
+  menu label ^Rescue a Red Hat Enterprise Linux system
+  text help
+	If the system will not boot, this lets you access files
+	and edit config files to try to get it booting again.
+  endtext
+  kernel vmlinuz
+  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rescue quiet
+
+label memtest
+  menu label Run a ^memory test
+  text help
+	If your system is having issues, a problem with your
+	system's memory may be the cause. Use this utility to
+	see if the memory is working correctly.
+  endtext
+  kernel memtest
+
+menu separator # insert an empty line
+
+label local
+  menu label Boot from ^local drive
+  localboot 0xffff
+
+menu separator # insert an empty line
+menu separator # insert an empty line
+
+label returntomain
+  menu label Return to ^main menu
+  menu exit
+
+menu end
-- 
GitLab