diff --git a/roles/molecule_multipass/README.md b/roles/molecule_multipass/README.md new file mode 100755 index 0000000..736b41e --- /dev/null +++ b/roles/molecule_multipass/README.md @@ -0,0 +1,30 @@ +Molecule Multipass driver +========================= + +Multipass driver for Molecule. + +Requirements +------------ + +This role requires the following tools to be installed: +- [multipass](https://multipass.run/) +- [molecule](https://ansible.readthedocs.io/projects/molecule/) + +How to use? +----------- + +This section is being written. Coming soon... + +License +------- + +GNU General Public License v3.0 or later + +Author Information +------------------ + +Created by Kenneth KOFFI, you can reach him on the following platforms: +- medium: https://theko2fi.medium.com +- github: https://github.com/theko2fi +- linkedin: https://www.linkedin.com/in/kenneth-koffi-6b1218178 + diff --git a/roles/molecule_multipass/defaults/main.yml b/roles/molecule_multipass/defaults/main.yml new file mode 100755 index 0000000..8fccc82 --- /dev/null +++ b/roles/molecule_multipass/defaults/main.yml @@ -0,0 +1,3 @@ +--- +# defaults file for theko2fi.multipass.molecule +dest: "{{ ('molecule/' + (scenario_name | default('default'))) | realpath }}" \ No newline at end of file diff --git a/roles/molecule_multipass/handlers/main.yml b/roles/molecule_multipass/handlers/main.yml new file mode 100755 index 0000000..8b738d7 --- /dev/null +++ b/roles/molecule_multipass/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for theko2fi.multipass.molecule diff --git a/roles/molecule_multipass/meta/main.yml b/roles/molecule_multipass/meta/main.yml new file mode 100755 index 0000000..c572acc --- /dev/null +++ b/roles/molecule_multipass/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/roles/molecule_multipass/tasks/main.yml b/roles/molecule_multipass/tasks/main.yml new file mode 100755 index 0000000..7939f4e --- /dev/null +++ b/roles/molecule_multipass/tasks/main.yml @@ -0,0 +1,27 @@ +--- +# tasks file for theko2fi.multipass.molecule + +- name: Check if destination folder exists + ansible.builtin.file: + path: "{{ dest }}" + state: directory + mode: "0700" +- name: Check if destination folder is empty + ansible.builtin.find: + paths: "{{ dest }}" + register: dest_content +- name: Fail if destination folder is not empty + when: dest_content.matched > 0 + ansible.builtin.fail: + msg: Refused to expand templates as destination folder '{{ dest }}' as it already has content in it. +- name: Expand templates + vars: + dest_file: "{{ dest }}/{{ item | basename | regex_replace('\\.j2$', '') }}" + ansible.builtin.template: + src: "{{ item }}" + dest: "{{ dest_file }}" + mode: "0644" + with_fileglob: + - templates/scenario/*.j2 + loop_control: + label: "{{ dest_file | relpath }}" \ No newline at end of file diff --git a/roles/molecule_multipass/templates/scenario/converge.yml.j2 b/roles/molecule_multipass/templates/scenario/converge.yml.j2 new file mode 100755 index 0000000..a118a3b --- /dev/null +++ b/roles/molecule_multipass/templates/scenario/converge.yml.j2 @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: all + gather_facts: true + tasks: + - name: Replace this task with one that validates your content + ansible.builtin.debug: + msg: "This is the effective test" diff --git a/roles/molecule_multipass/templates/scenario/create.yml.j2 b/roles/molecule_multipass/templates/scenario/create.yml.j2 new file mode 100755 index 0000000..7a8d98d --- /dev/null +++ b/roles/molecule_multipass/templates/scenario/create.yml.j2 @@ -0,0 +1,44 @@ +--- +{% raw -%} +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + molecule_labels: + owner: molecule + tags: + - always + tasks: + - name: Set async_dir for HOME env + ansible.builtin.set_fact: + ansible_async_dir: "{{ lookup('env', 'HOME') }}/.ansible_async/" + when: (lookup('env', 'HOME')) + + - name: Create molecule instance(s) + theko2fi.multipass.multipass_vm: + name: "{{ item.name }}" + cloud_init: "{{ item.cloud_init | default(omit) }}" + cpus: "{{ item.cpu | default(omit) }}" + disk: "{{ item.disk | default(omit) }}" + image: "{{ item.image | default(omit) }}" + memory: "{{ item.memory | default(omit) }}" + recreate: "{{ item.recreate | default(omit) }}" + state: 'started' + register: server + with_items: "{{ molecule_yml.platforms }}" + loop_control: + label: "{{ item.name }}" + no_log: false + async: 7200 + poll: 0 + + - name: Wait for instance(s) creation to complete + ansible.builtin.async_status: + jid: "{{ item.ansible_job_id }}" + register: multipass_jobs + until: multipass_jobs.finished + retries: 300 + with_items: "{{ server.results }}" +{%- endraw %} \ No newline at end of file diff --git a/roles/molecule_multipass/templates/scenario/destroy.yml.j2 b/roles/molecule_multipass/templates/scenario/destroy.yml.j2 new file mode 100755 index 0000000..f640f1c --- /dev/null +++ b/roles/molecule_multipass/templates/scenario/destroy.yml.j2 @@ -0,0 +1,39 @@ +--- +{% raw -%} +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + tags: + - always + tasks: + - name: Set async_dir for HOME env + ansible.builtin.set_fact: + ansible_async_dir: "{{ lookup('env', 'HOME') }}/.ansible_async/" + when: (lookup('env', 'HOME')) + + - name: Destroy molecule instance(s) + theko2fi.multipass.multipass_vm: + name: "{{ item.name }}" + state: absent + purge: true + register: server + loop: "{{ molecule_yml.platforms }}" + loop_control: + label: "{{ item.name }}" + no_log: false + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + ansible.builtin.async_status: + jid: "{{ item.ansible_job_id }}" + register: multipass_jobs + until: multipass_jobs.finished + retries: 300 + loop: "{{ server.results }}" + loop_control: + label: "{{ item.item.name }}" + +{%- endraw %} \ No newline at end of file diff --git a/roles/molecule_multipass/templates/scenario/molecule.yml.j2 b/roles/molecule_multipass/templates/scenario/molecule.yml.j2 new file mode 100755 index 0000000..3b81f12 --- /dev/null +++ b/roles/molecule_multipass/templates/scenario/molecule.yml.j2 @@ -0,0 +1,15 @@ +--- +dependency: + name: galaxy +driver: + options: + managed: False + login_cmd_template: 'multipass exec {instance} -- bash' + ansible_connection_options: + ansible_connection: theko2fi.multipass.multipass +platforms: + - name: instance-molecule +provisioner: + name: ansible +verifier: + name: ansible diff --git a/roles/molecule_multipass/templates/scenario/requirements.yml.j2 b/roles/molecule_multipass/templates/scenario/requirements.yml.j2 new file mode 100755 index 0000000..00418f9 --- /dev/null +++ b/roles/molecule_multipass/templates/scenario/requirements.yml.j2 @@ -0,0 +1,2 @@ +collections: + - theko2fi.multipass diff --git a/roles/molecule_multipass/tests/inventory b/roles/molecule_multipass/tests/inventory new file mode 100755 index 0000000..878877b --- /dev/null +++ b/roles/molecule_multipass/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/roles/molecule_multipass/tests/test.yml b/roles/molecule_multipass/tests/test.yml new file mode 100755 index 0000000..1d0e10a --- /dev/null +++ b/roles/molecule_multipass/tests/test.yml @@ -0,0 +1,8 @@ +--- +- name: Create a new molecule scenario + hosts: localhost + connection: local + remote_user: root + gather_facts: false + roles: + - theko2fi.multipass.molecule_multipass diff --git a/roles/molecule_multipass/vars/main.yml b/roles/molecule_multipass/vars/main.yml new file mode 100755 index 0000000..6c0f643 --- /dev/null +++ b/roles/molecule_multipass/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for theko2fi.multipass.molecule