diff options
| -rw-r--r-- | README.md | 21 | ||||
| -rw-r--r-- | collections/requirements.yml | 1 | ||||
| -rw-r--r-- | docs/sample-environment.yml | 4 | ||||
| -rw-r--r-- | roles/guest-configure/tasks/main.yml | 3 | ||||
| -rw-r--r-- | roles/guest-configure/tasks/user.yml | 22 | ||||
| -rw-r--r-- | roles/guest-configure/vars/main.yml | 7 | ||||
| -rw-r--r-- | roles/libvirt/tasks/vm-install.yml | 13 | ||||
| -rw-r--r-- | roles/libvirt/vars/main.yml | 20 |
8 files changed, 40 insertions, 51 deletions
@@ -10,32 +10,17 @@ The goal is a consistently deployed lab which can be defined with YAML for its c - ```ansible-playbook -i inventories/your-inventory.yml vm-create.yml``` - ```ansible-playbook -i inventories/your-inventory.yml vm-delete.yml``` -## Minimum needed environment file +## Absolute minimum environment file ```yaml all: hosts: test-el8: - os: "rhel8-unknown" - kickstart: "el8.ks" iso_path: "/path/to/ios/AlmaLinux-8.7-x86_64-dvd.iso" vars: - memory_mb: 1024 - cpus: 1 - disk_gb: 20 - disk_format: "raw" - timezone: "America/New_York" hypervisor_host: "hypervisor.fqdn" parent_dataset: "zfs-parent-dataset/zfs-child-dataset" - network: "bridge:vm-bridge" - root_password: "{{ lookup('password', '/dev/null length=32 chars=ascii_letters,digits') }}" - user: "admin" - ssh_key: "" - packages: - - qemu-guest-agent - services: - - qemu-guest-agent ``` -An example with multiple VMs is located in the ```docs``` directory. +This example has no automated install and no guest configuration. An example with multiple VMs and full options is located in the ```docs``` directory. ## Requirements - Ansible @@ -49,7 +34,7 @@ An example with multiple VMs is located in the ```docs``` directory. - Ansible will use sudo to communicate with KVM and ZFS - One ZFS dataset per VM is created - ZFS dataset for each VM will have no child datasets -- Kickstart files and compatible distros are required +- Kickstart files are required for any automated installations - The delete play will completely remove any VMs or datasets defined in your inventory ## Known Issues diff --git a/collections/requirements.yml b/collections/requirements.yml index 7588f8b..c5d6f67 100644 --- a/collections/requirements.yml +++ b/collections/requirements.yml @@ -1,3 +1,4 @@ collections: - name: community.general - name: community.libvirt + - name: ansible.posix diff --git a/docs/sample-environment.yml b/docs/sample-environment.yml index 3f2dcf6..bc6f928 100644 --- a/docs/sample-environment.yml +++ b/docs/sample-environment.yml @@ -1,7 +1,7 @@ all: hosts: test-el8: - os: "rhel8-unknown" + os: "rhel8-unknown" #A full list of OSes can be found with this #virt-install --osinfo list kickstart: "el8.ks" iso_path: "/path/to/ios/AlmaLinux-8.7-x86_64-dvd.iso" test-el9: @@ -22,7 +22,7 @@ all: hypervisor_host: "hypervisor.fqdn" parent_dataset: "zfs-parent-dataset/zfs-child-dataset" network: "bridge:vm-bridge" - root_password: "{{ lookup('password', '/dev/null length=32 chars=ascii_letters,digits') }}" #Random root password +# root_password: "" #Not defining this will cause a random root password to be generated user: "admin" #Regular user with sudo rights # SSH key for root and regular user ssh_key: | diff --git a/roles/guest-configure/tasks/main.yml b/roles/guest-configure/tasks/main.yml index 4e4b429..47a5f27 100644 --- a/roles/guest-configure/tasks/main.yml +++ b/roles/guest-configure/tasks/main.yml @@ -6,12 +6,15 @@ - name: Import user creation task ansible.builtin.import_tasks: user.yml + when: guest_configure_user|length > 0 - name: Import update task ansible.builtin.import_tasks: update.yml - name: Import packages task ansible.builtin.import_tasks: packages.yml + when: guest_configure_packages|length > 0 - name: Import services task ansible.builtin.import_tasks: services.yml + when: guest_configure_services|length > 0 diff --git a/roles/guest-configure/tasks/user.yml b/roles/guest-configure/tasks/user.yml index c0e418b..7039492 100644 --- a/roles/guest-configure/tasks/user.yml +++ b/roles/guest-configure/tasks/user.yml @@ -2,6 +2,13 @@ ansible.builtin.user: name: "{{ guest_configure_user }}" +- name: Add user SSH key (if defined) + ansible.posix.authorized_key: + user: "{{ guest_configure_user }}" + state: present + key: "{{ guest_configure_ssh_key }}" + when: guest_configure_ssh_key|length > 0 + - name: RedHat block block: - name: Add user to sudo group (RedHat) @@ -35,18 +42,3 @@ line: '%sudo ALL=(ALL) NOPASSWD: ALL' validate: 'visudo -cf %s' when: ansible_os_family == "Debian" - -- name: Create user .ssh folder - ansible.builtin.file: - path: "/home/{{ guest_configure_user }}/.ssh/" - state: directory - mode: "0700" - owner: "{{ guest_configure_user }}" - -- name: Copy root ssh authorized_keys key to new user - ansible.builtin.copy: - src: "/root/.ssh/authorized_keys" - dest: "/home/{{ guest_configure_user }}/.ssh/authorized_keys" - remote_src: yes - mode: "0600" - owner: "{{ guest_configure_user }}" diff --git a/roles/guest-configure/vars/main.yml b/roles/guest-configure/vars/main.yml index 10881fb..70d90a4 100644 --- a/roles/guest-configure/vars/main.yml +++ b/roles/guest-configure/vars/main.yml @@ -1,6 +1,7 @@ ansible_ssh_common_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ansible_user: root -guest_configure_user: "{{ user }}" -guest_configure_packages: "{{ packages }}" -guest_configure_services: "{{ services }}" +guest_configure_user: "{{ user if user is defined }}" +guest_configure_ssh_key: "{{ ssh_key if ssh_key is defined }}" +guest_configure_packages: "{{ packages if packages is defined }}" +guest_configure_services: "{{ services if services is defined }}" diff --git a/roles/libvirt/tasks/vm-install.yml b/roles/libvirt/tasks/vm-install.yml index b1d9de8..c0611e5 100644 --- a/roles/libvirt/tasks/vm-install.yml +++ b/roles/libvirt/tasks/vm-install.yml @@ -1,12 +1,19 @@ -- name: Copy kickstart file to destination filesystem +- name: Copy kickstart file to destination filesystem (if defined) ansible.builtin.template: src: "kickstart/{{ libvirt_vm_kickstart_file }}" dest: "/{{ libvirt_vm_destination }}/{{ libvirt_vm_kickstart_file }}" + when: libvirt_vm_kickstart_file|length > 0 -- name: Create VM in destination filesystem +- name: Create VM from kickstart in destination filesystem ansible.builtin.command: 'virt-install --name {{ libvirt_vm_name }} --memory {{ libvirt_vm_memory }} --vcpus {{ libvirt_vm_vcpus }} --network {{ libvirt_vm_network }} --disk size={{ libvirt_vm_disk_size }},path=/{{ libvirt_vm_destination }}/{{ libvirt_vm_name }}.img,format={{ libvirt_vm_disk_format }} --location {{ libvirt_vm_location_path }}{{ libvirt_vm_location_arguments }} --os-variant {{ libvirt_vm_os }} --initrd-inject=/{{ libvirt_vm_destination }}/{{ libvirt_vm_kickstart_file }} --extra-args="inst.ks=file:/{{ libvirt_vm_kickstart_file }}"' + when: libvirt_vm_kickstart_file|length > 0 -- name: Remove kickstart file from destination filesystem +- name: Create VM without kickstart in destination filesystem + ansible.builtin.command: 'virt-install --name {{ libvirt_vm_name }} --memory {{ libvirt_vm_memory }} --vcpus {{ libvirt_vm_vcpus }} --network {{ libvirt_vm_network }} --disk size={{ libvirt_vm_disk_size }},path=/{{ libvirt_vm_destination }}/{{ libvirt_vm_name }}.img,format={{ libvirt_vm_disk_format }} --location {{ libvirt_vm_location_path }}{{ libvirt_vm_location_arguments }} --os-variant {{ libvirt_vm_os }}' + when: libvirt_vm_kickstart_file|length == 0 + +- name: Remove kickstart file from destination filesystem (if defined) ansible.builtin.file: path: "/{{ libvirt_vm_destination }}/{{ libvirt_vm_kickstart_file }}" state: absent + when: libvirt_vm_kickstart_file|length > 0 diff --git a/roles/libvirt/vars/main.yml b/roles/libvirt/vars/main.yml index 54fd9e7..1c26e68 100644 --- a/roles/libvirt/vars/main.yml +++ b/roles/libvirt/vars/main.yml @@ -1,15 +1,15 @@ libvirt_vm_name: "{{ inventory_hostname }}" -libvirt_vm_memory: "{{ memory_mb }}" -libvirt_vm_vcpus: "{{ cpus }}" -libvirt_vm_disk_size: "{{ disk_gb }}" -libvirt_vm_disk_format: "{{ disk_format }}" -libvirt_vm_os: "{{ os }}" -libvirt_vm_kickstart_file: "{{ kickstart }}" +libvirt_vm_memory: "{{ memory_mb if memory_mb is defined else '1024' }}" +libvirt_vm_vcpus: "{{ cpus if cpus is defined else '1' }}" +libvirt_vm_disk_size: "{{ disk_gb if disk_gb is defined else '20' }}" +libvirt_vm_disk_format: "{{ disk_format if disk_format is defined else 'qcow2' }}" +libvirt_vm_os: "{{ os if os is defined else 'rhel8-unknown' }}" +libvirt_vm_kickstart_file: "{{ kickstart if kickstart is defined }}" libvirt_vm_location_path: "{{ iso_path }}" libvirt_vm_destination: "{{ parent_dataset }}/{{ inventory_hostname }}" -libvirt_vm_network: "{{ network }}" +libvirt_vm_network: "{{ network if network is defined else 'default' }}" libvirt_kickstart_hostname: "{{ inventory_hostname }}" -libvirt_kickstart_timezone: "{{ timezone }}" -libvirt_kickstart_root_ssh_key: "{{ ssh_key }}" -libvirt_kickstart_root_password: "{{ root_password }}" +libvirt_kickstart_timezone: "{{ timezone if timezone is defined else 'Etc/GMT' }}" +libvirt_kickstart_root_ssh_key: "{{ ssh_key if ssh_key is defined }}" +libvirt_kickstart_root_password: "{{ root_password if root_password is defined else lookup('password', '/dev/null length=32 chars=ascii_letters,digits') }}" |
