From 9bd2c8c17a22c84da9a6ba6407c5b928d77fb469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= Date: Tue, 7 Dec 2021 15:41:02 +0000 Subject: roles/vmm: Serve autoinstall configurations via httpd --- roles/vmm/defaults/main.yml | 12 ++++++++++ roles/vmm/meta/main.yml | 3 +++ roles/vmm/tasks/autoinstall-configuration.yml | 27 +++++++++++++++++++++ roles/vmm/tasks/disk.yml | 2 +- roles/vmm/tasks/facts.yml | 8 +++---- roles/vmm/tasks/main.yml | 34 ++++++--------------------- roles/vmm/tasks/network.yml | 27 +++++++++++++++++++++ roles/vmm/templates/autoinstall.conf.j2 | 12 ++++++++++ roles/vmm/templates/hostname.j2 | 1 - roles/vmm/templates/httpd.conf.j2 | 4 ++++ roles/vmm/templates/vm.conf.j2 | 2 +- 11 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 roles/vmm/meta/main.yml create mode 100644 roles/vmm/tasks/autoinstall-configuration.yml create mode 100644 roles/vmm/tasks/network.yml create mode 100644 roles/vmm/templates/autoinstall.conf.j2 delete mode 100644 roles/vmm/templates/hostname.j2 create mode 100644 roles/vmm/templates/httpd.conf.j2 (limited to 'roles/vmm') diff --git a/roles/vmm/defaults/main.yml b/roles/vmm/defaults/main.yml index 1dcb012..8d26a49 100644 --- a/roles/vmm/defaults/main.yml +++ b/roles/vmm/defaults/main.yml @@ -4,6 +4,12 @@ vmm_disk_dir: /data/vmm/disk.d vmm_disk_format: qcow2 vmm_configuration_file: /etc/vm.conf +vmm_default_password: a +vmm_default_timezone: Europe/Stockholm +vmm_default_fallback_http: true +vmm_default_ssh_key: a + + vmm_network_forwarded_ips: ["", 6] vmm_network_switch: @@ -20,3 +26,9 @@ vmm_iso: version: 6.8 url: https://cdn.openbsd.org/pub/OpenBSD/7.0/amd64/install70.img checksum: sha256:6bc7f945c2709247d449892c33c0f1b9a31590528572c1e988fef4a7637210e6 + +# vmm_vms: +# - name: +# image: +# memory: +# size: diff --git a/roles/vmm/meta/main.yml b/roles/vmm/meta/main.yml new file mode 100644 index 0000000..6b163d1 --- /dev/null +++ b/roles/vmm/meta/main.yml @@ -0,0 +1,3 @@ +dependencies: + - role: httpd_pre + tags: dependency diff --git a/roles/vmm/tasks/autoinstall-configuration.yml b/roles/vmm/tasks/autoinstall-configuration.yml new file mode 100644 index 0000000..e05b11c --- /dev/null +++ b/roles/vmm/tasks/autoinstall-configuration.yml @@ -0,0 +1,27 @@ +- name: include httpd role variables + include_vars: "{{ inventory_dir }}/roles/httpd/defaults/main.yml" + +- name: create autoinstall directory + file: + path: /var/www/htdocs/autoinstall + owner: www + group: www + mode: 0700 + state: directory + +- name: generate autoinstall files + template: + src: autoinstall.conf.j2 + dest: "/var/www/htdocs/autoinstall/{{ item.lladdr }}-install.conf" + owner: www + group: www + mode: 0640 + loop: "{{ vmm_vms }}" + +- name: generate httpd configuration + template: + src: httpd.conf.j2 + dest: "{{ httpd_configuration_dir }}/autoinstall.conf" + owner: 0 + group: 0 + mode: 0640 diff --git a/roles/vmm/tasks/disk.yml b/roles/vmm/tasks/disk.yml index 24dd491..423d855 100644 --- a/roles/vmm/tasks/disk.yml +++ b/roles/vmm/tasks/disk.yml @@ -2,7 +2,7 @@ command: cmd: vmctl create -s "{{ item.size }}" "{{ item.name }}.{{ vmm_disk_format }}" chdir: "{{ vmm_disk_dir }}" - loop: "{{ __vms }}" + loop: "{{ vmm_vms }}" register: result changed_when: result.rc == 0 failed_when: diff --git a/roles/vmm/tasks/facts.yml b/roles/vmm/tasks/facts.yml index 7cf4f0c..c7b453b 100644 --- a/roles/vmm/tasks/facts.yml +++ b/roles/vmm/tasks/facts.yml @@ -1,14 +1,14 @@ - name: generate lladdr variable for virtual machines set_fact: - tmp_vms: > - {{ tmp_vms | default([]) + [ item | combine({ + vmm_vms_tmp: > + {{ vmm_vms_tmp | default([]) + [ item | combine({ 'lladdr': item.name | hash('sha1') | truncate(12, True, '') | ansible.netcommon.hwaddr('unix') }) ] }} - loop: "{{ __vms }}" + loop: "{{ vmm_vms }}" - name: save variables set_fact: - __vms: "{{ tmp_vms }}" + vmm_vms: "{{ vmm_vms_tmp }}" diff --git a/roles/vmm/tasks/main.yml b/roles/vmm/tasks/main.yml index 3c5a462..c596664 100644 --- a/roles/vmm/tasks/main.yml +++ b/roles/vmm/tasks/main.yml @@ -13,6 +13,10 @@ - name: include facts generation include_tasks: facts.yml +- name: include autoinstall generation + include_tasks: autoinstall-configuration.yml + tags: task_autoinstall_configuration + - name: include iso management include_tasks: iso.yml tags: task_iso @@ -21,33 +25,9 @@ include_tasks: disk.yml tags: task_disk -- name: start ip forwarding - command: sysctl net.inet.ip{{ item }}.forwarding=1 - loop: "{{ vmm_network_forwarded_ips }}" - -- name: enable ip forwarding - lineinfile: - path: /etc/sysctl.conf - regexp: "^net.inet.ip{{ item }}.forwarding=" - line: "net.inet.ip{{ item }}.forwarding=1" - owner: 0 - group: 0 - mode: 0640 - create: true - loop: "{{ vmm_network_forwarded_ips }}" - -- name: create network switch - lineinfile: - path: "/etc/hostname.{{ vmm_network_switch.interface }}" - regexp: &network_line "add {{ ansible_default_ipv4.interface }}" - line: *network_line - owner: 0 - group: 0 - mode: 0640 - create: true - -- name: start network switch - command: "sh /etc/netstart {{ vmm_network_switch.interface }}" +- name: include network management + include_tasks: network.yml + tags: task_network - name: generate vmm configuration template: diff --git a/roles/vmm/tasks/network.yml b/roles/vmm/tasks/network.yml new file mode 100644 index 0000000..b8fc7e1 --- /dev/null +++ b/roles/vmm/tasks/network.yml @@ -0,0 +1,27 @@ +- name: start ip forwarding + command: sysctl net.inet.ip{{ item }}.forwarding=1 + loop: "{{ vmm_network_forwarded_ips }}" + +- name: enable ip forwarding + lineinfile: + path: /etc/sysctl.conf + regexp: "^net.inet.ip{{ item }}.forwarding=" + line: "net.inet.ip{{ item }}.forwarding=1" + owner: 0 + group: 0 + mode: 0640 + create: true + loop: "{{ vmm_network_forwarded_ips }}" + +- name: create network switch + lineinfile: + path: "/etc/hostname.{{ vmm_network_switch.interface }}" + regexp: &network_line "add {{ ansible_default_ipv4.interface }}" + line: *network_line + owner: 0 + group: 0 + mode: 0640 + create: true + +- name: start network switch + command: "sh /etc/netstart {{ vmm_network_switch.interface }}" diff --git a/roles/vmm/templates/autoinstall.conf.j2 b/roles/vmm/templates/autoinstall.conf.j2 new file mode 100644 index 0000000..de64c42 --- /dev/null +++ b/roles/vmm/templates/autoinstall.conf.j2 @@ -0,0 +1,12 @@ +System hostname = {{ item.name }} +Password for root = {{ vmm_default_password }} +Network interfaces = run0 +IPv4 address for run0 = dhcp +Public ssh key for user = {{ vmm_default_ssh_key }} +Which disk is the root disk = sd0 +What timezone are you in = {{ vmm_default_timezone }} +Unable to connect using https. Use http instead = {{ "yes" if vmm_default_fallback_http else "no" }} +Location of sets = http +HTTP Server = cdn.openbsd.org +Server = {{ ansible_default_ipv4.gateway }} +Set name(s) = -all bsd* base* comp* man* diff --git a/roles/vmm/templates/hostname.j2 b/roles/vmm/templates/hostname.j2 deleted file mode 100644 index 68b989a..0000000 --- a/roles/vmm/templates/hostname.j2 +++ /dev/null @@ -1 +0,0 @@ -add {{ vmm.switch.interface_host }} diff --git a/roles/vmm/templates/httpd.conf.j2 b/roles/vmm/templates/httpd.conf.j2 new file mode 100644 index 0000000..ed9feb6 --- /dev/null +++ b/roles/vmm/templates/httpd.conf.j2 @@ -0,0 +1,4 @@ +server "autoinstall" { + listen on * port 80 + root "/htdocs/autoinstall" +} diff --git a/roles/vmm/templates/vm.conf.j2 b/roles/vmm/templates/vm.conf.j2 index d45c398..8653616 100644 --- a/roles/vmm/templates/vm.conf.j2 +++ b/roles/vmm/templates/vm.conf.j2 @@ -6,7 +6,7 @@ switch "{{ vmm_network_switch.name }}" { } # virtual machines -{% for vm in __vms %} +{% for vm in vmm_vms %} vm "{{ vm.name }}" { {% if vm.enabled is defined and not vm.enabled %} disable -- cgit v1.2.3