From 6e5cd32a276d20fd28082be55bd0e0856650f622 Mon Sep 17 00:00:00 2001 From: smirta Date: Thu, 6 Jun 2024 20:57:11 +0200 Subject: [PATCH] feat(libvirt): install libvirt, enable and start service, configure storage pool. add ansible galaxy and gh actions specifics --- .github/dependabot.yml | 8 +++++ .github/workflows/release.yaml | 12 +++++++ .github/workflows/semantic-release.yaml | 12 +++++++ .github/workflows/test.yaml | 10 ++++++ README.md | 9 +++++ galaxy.yml | 20 +++++++++++ meta/runtime.yml | 1 + roles/cloudinit_image/README.md | 38 -------------------- roles/cloudinit_image/defaults/main.yml | 9 ----- roles/cloudinit_image/meta/main.yml | 20 ----------- roles/cloudinit_image/tasks/main.yml | 35 ------------------- roles/cloudinit_image/tests/inventory | 2 -- roles/cloudinit_image/tests/test.yml | 5 --- roles/libvirt/README.md | 46 +++++++++++-------------- roles/libvirt/defaults/main.yml | 8 +++-- roles/libvirt/tasks/main.yml | 13 ++++--- 16 files changed, 107 insertions(+), 141 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/release.yaml create mode 100644 .github/workflows/semantic-release.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 galaxy.yml create mode 100644 meta/runtime.yml delete mode 100644 roles/cloudinit_image/README.md delete mode 100644 roles/cloudinit_image/defaults/main.yml delete mode 100644 roles/cloudinit_image/meta/main.yml delete mode 100644 roles/cloudinit_image/tasks/main.yml delete mode 100644 roles/cloudinit_image/tests/inventory delete mode 100644 roles/cloudinit_image/tests/test.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..aa770e4 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + commit-message: + prefix: "chore(ci): " diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..16f3c57 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,12 @@ +name: Release + +on: + release: + types: + - published + +jobs: + call-workflow: + uses: radiorabe/actions/.github/workflows/release-ansible-collection.yaml@v0.20.7 + secrets: + GALAXY_API_KEY: ${{ secrets.GALAXY_API_KEY }} diff --git a/.github/workflows/semantic-release.yaml b/.github/workflows/semantic-release.yaml new file mode 100644 index 0000000..c4e4c6c --- /dev/null +++ b/.github/workflows/semantic-release.yaml @@ -0,0 +1,12 @@ +name: Semantic Release + +on: + push: + branches: + - main + +jobs: + call-workflow: + uses: radiorabe/actions/.github/workflows/semantic-release.yaml@v0.20.7 + secrets: + RABE_ITREAKTION_GITHUB_TOKEN: ${{ secrets.RABE_ITREAKTION_GITHUB_TOKEN }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..7d90c20 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,10 @@ +name: Lint and Test + +on: + pull_request: + branches: + - main + +jobs: + call-workflow: + uses: radiorabe/actions/.github/workflows/test-ansible-collection.yaml@v0.20.7 diff --git a/README.md b/README.md index 21eb564..ea32375 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ # ansible-collection-foreman-libvirt + Ansible collection for making a host ready to serve as libvirt compute resource in Foreman. + +## Roles + +* [`libvirt`](https://github.com/radiorabe/ansible-collection-foreman-libvirt/tree/main/roles/libvirt) + +## License + +This collection is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 of the License. diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..84b24f5 --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,20 @@ +namespace: radiorabe +name: foreman_libvirt +version: 0.0.0 +readme: README.md +authors: + - Simon Nussbaum <@smirta> +description: Ansible collection for making a host ready to serve as libvirt compute resource in Foreman. +license_file: LICENSE +tags: + - radiorabe + - kvm + - libvirt + - qemu + - foreman + - cloudinit +dependencies: {} +repository: https://github.com/radiorabe/ansible-collection-foreman-libvirt +documentation: https://github.com/radiorabe/ansible-collection-foreman-libvirt/blob/main/README.md +homepage: hhttps://github.com/radiorabe/ansible-collection-foreman-libvirt +issues: https://github.com/radiorabe/ansible-collection-foreman-libvirt/issues diff --git a/meta/runtime.yml b/meta/runtime.yml new file mode 100644 index 0000000..15e28cd --- /dev/null +++ b/meta/runtime.yml @@ -0,0 +1 @@ +requires_ansible: ">=2.11,<2.17" diff --git a/roles/cloudinit_image/README.md b/roles/cloudinit_image/README.md deleted file mode 100644 index 225dd44..0000000 --- a/roles/cloudinit_image/README.md +++ /dev/null @@ -1,38 +0,0 @@ -Role Name -========= - -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. - -Role Variables --------------- - -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. - -Dependencies ------------- - -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -Example Playbook ----------------- - -Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - - hosts: servers - roles: - - { role: username.rolename, x: 42 } - -License -------- - -BSD - -Author Information ------------------- - -An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/cloudinit_image/defaults/main.yml b/roles/cloudinit_image/defaults/main.yml deleted file mode 100644 index 9885768..0000000 --- a/roles/cloudinit_image/defaults/main.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -# defaults file for kvm_provision -cloudinit_image_base_image_name: AlmaLinux-9-GenericCloud-latest.x86_64.qcow2 -cloudinit_image_base_image_url: https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/{{ cloudinit_image_base_image_name }} -cloudinit_image_base_image_sha: bff0885c804c01fff8aac4b70c9ca4f04e8c119f9ee102043838f33e06f58390 -cloudinit_image_libvirt_pool_dir: /var/lib/libvirt/images -cloudinit_image_local_files_path: "{{ role_path }}/files" -cloudinit_image_local_download: false -cloudinit_image_preparation_host: "{{ 'localhost' if cloudinit_image_local_download else inventory_hostname|default(inventory_hostname) }}" diff --git a/roles/cloudinit_image/meta/main.yml b/roles/cloudinit_image/meta/main.yml deleted file mode 100644 index 138fbf4..0000000 --- a/roles/cloudinit_image/meta/main.yml +++ /dev/null @@ -1,20 +0,0 @@ -galaxy_info: - author: RaBe IT-Reaktion - description: Download and prepare generic cloud image - issue_tracker_url: https://github.com/radiorabe/ansible-collection-foreman-libvirt/issues - license: AGPL-3.0-only - min_ansible_version: '2.9' - platforms: - - name: EL - versions: - - all - - name: Fedora - version: - - all - galaxy_tags: - - radiorabe - - kvm - - qemu - - libvirt - - cloud-init -dependencies: [] diff --git a/roles/cloudinit_image/tasks/main.yml b/roles/cloudinit_image/tasks/main.yml deleted file mode 100644 index 4fe5125..0000000 --- a/roles/cloudinit_image/tasks/main.yml +++ /dev/null @@ -1,35 +0,0 @@ ---- -# tasks file for kvm_provision -- name: Ensure requirements in place - delegate_to: "{{ cloudinit_image_preparation_host }}" - package: - name: - - libguestfs-tools - state: present - become: true - -- name: Download base image - delegate_to: "{{ cloudinit_image_preparation_host }}" - get_url: - url: "{{ cloudinit_image_base_image_url }}" - dest: "{{ cloudinit_image_local_files_path }}" - checksum: "sha256:{{ cloudinit_image_base_image_sha }}" - -- name: Configure the image - delegate_to: "{{ cloudinit_image_preparation_host }}" - command: | - virt-customize -a {{ cloudinit_image_local_files_path }}/{{ cloudinit_image_base_image_name }} \ - --run-command "dnf install -y subscription-manager" - -- name: Copy base image to libvirt storage pool directory {{ cloudinit_image_libvirt_pool_dir }} - copy: - dest: "{{ cloudinit_image_libvirt_pool_dir }}/" - src: "{{ cloudinit_image_local_files_path }}/{{ cloudinit_image_base_image_name }}" - force: no - mode: 0660 - -- name: Ensure temporary file is deleted - delegate_to: "{{ cloudinit_image_preparation_host }}" - file: - path: "/tmp/{{ cloudinit_image_base_image_name }}" - state: absent diff --git a/roles/cloudinit_image/tests/inventory b/roles/cloudinit_image/tests/inventory deleted file mode 100644 index 878877b..0000000 --- a/roles/cloudinit_image/tests/inventory +++ /dev/null @@ -1,2 +0,0 @@ -localhost - diff --git a/roles/cloudinit_image/tests/test.yml b/roles/cloudinit_image/tests/test.yml deleted file mode 100644 index 5e48cde..0000000 --- a/roles/cloudinit_image/tests/test.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -- hosts: localhost - remote_user: root - roles: - - cloudinit_image diff --git a/roles/libvirt/README.md b/roles/libvirt/README.md index 225dd44..029cbd4 100644 --- a/roles/libvirt/README.md +++ b/roles/libvirt/README.md @@ -1,38 +1,34 @@ -Role Name -========= +# Ansible Role - radiorabe.foreman-libvirt.libvirt -A brief description of the role goes here. +Install libvirtd, enable and start libvirtd and create default storage pool using [`ansible.builtin.package`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_module.html), [`ansible.builtin.systemd_service`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_service_module.html) and [`ansible.builtin.template`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html). -Requirements ------------- +## Requirements -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. +Destination host needs to be able to download and install libvirt package from repository. -Role Variables --------------- +## Role Variables -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. +| Variable | Default | Description | +| -------- | ------- | ----------- | +| `libvirt_storage_pool_path` | `/var/lib/libvirt/images/` | libvirt directory storage pool path. | +| `libvirt_storage_pool_name` | `default` | libvirt directory storage pool name. | +| `libvirt_libvirt_package_name` | `libvirt` | libvirt package name from repository. | +| `libvirt_libvirt_service_name` | `libvirtd` | libvirt service name. | -Dependencies ------------- +## Dependencies -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. +None -Example Playbook ----------------- +## Example Playbook Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - hosts: servers - roles: - - { role: username.rolename, x: 42 } +```yaml +- hosts: all + roles: + - libvirt +``` -License -------- +## License -BSD - -Author Information ------------------- - -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +This role is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 of the License. diff --git a/roles/libvirt/defaults/main.yml b/roles/libvirt/defaults/main.yml index 9961134..ff4cb53 100644 --- a/roles/libvirt/defaults/main.yml +++ b/roles/libvirt/defaults/main.yml @@ -1,4 +1,6 @@ --- -# defaults file for kvm_provision -storage_pool_path: '/var/lib/libvirt/images/' -storage_pool_name: 'default' +# defaults file for libvirt +libvirt_storage_pool_path: /var/lib/libvirt/images/ +libvirt_storage_pool_name: default +libvirt_libvirt_package_name: libvirt +libvirt_libvirt_service_name: libvirtd \ No newline at end of file diff --git a/roles/libvirt/tasks/main.yml b/roles/libvirt/tasks/main.yml index 703e221..2cd7299 100644 --- a/roles/libvirt/tasks/main.yml +++ b/roles/libvirt/tasks/main.yml @@ -3,7 +3,7 @@ - name: Ensure requirements are in place package: name: - - libvirt + - "{{ libvirt_libvirt_package_name }}" state: present become: true @@ -11,12 +11,17 @@ ansible.builtin.systemd_service: state: started enabled: true - name: libvirtd + name: "{{ libvirt_libvirt_service_name }}" -- name: Write storage pool definition for {{ storage_pool_name }} +- name: Write storage pool definition for {{ libvirt_storage_pool_name }} ansible.builtin.template: src: templates/storage-pool.xml.j2 dest: /tmp/storage-pool.xml -- name: Create storage pool {{ storage_pool_name }} +- name: Create storage pool {{ libvirt_storage_pool_name }} command: virsh pool-create /tmp/storage-pool.xml + +- name: Remove storage pool definition /tmp/storage-pool.xml + file: + path: /tmp/storage-pool.xml + state: absent