diff --git a/molecule/shared/substrate/lxd/create.yml b/molecule/shared/substrate/lxd/create.yml new file mode 100644 index 00000000..3a1f1d1c --- /dev/null +++ b/molecule/shared/substrate/lxd/create.yml @@ -0,0 +1,79 @@ +--- +- name: Manage LXD containers and set Pulumi ESC + hosts: localhost + gather_facts: false + connection: local + become: true + vars: + molecule_inventory: + molecule: + hosts: {} + molecule_yml: + platforms: + - name: instance + image_fingerprint: "noble" + architecture: x86_64 + user: ansible + + tasks: + - name: Check if LXD is installed + ansible.builtin.command: lxd --version + register: lxd_check + ignore_errors: true + + - name: Fail if LXD is not installed + ansible.builtin.fail: + msg: | + LXD is not installed on this system. + Please install LXD before running this playbook. + For installation instructions, visit: https://linuxcontainers.org/lxd/getting-started-cli/ + when: lxd_check.rc != 0 + + - name: Check if LXD is initialized + ansible.builtin.command: lxd waitready --timeout=5 + register: lxd_initialized + ignore_errors: true + + - name: Fail if LXD is not initialized + ansible.builtin.fail: + msg: "LXD is not initialized or not ready. Please run 'sudo lxd init' manually." + when: lxd_initialized.rc != 0 + + - name: Get ESC from Pulumi + ansible.builtin.command: esc open deeep-network/dev/services --format dotenv + changed_when: false + register: pulumi_esc + + - name: Ensure cloud-init directory exists + ansible.builtin.file: + path: /etc/cloud + state: directory + + - name: Write Pulumi ESC to cloud-init file + ansible.builtin.blockinfile: + path: /etc/cloud/cloud.cfg.d/01-pulumi-esc.cfg + block: | + # Pulumi ESC Configuration + {{ pulumi_esc.stdout }} + + - name: Check if LXD container exists + ansible.builtin.command: lxc list "{{ platform['name'] }}" --format=json + register: lxd_container_info + ignore_errors: true + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Debug container existence + ansible.builtin.debug: + var: lxd_container_info + + - name: Create LXD containers if not exists using lxc launch + ansible.builtin.command: > + lxc launch ubuntu:{{ platform['image_fingerprint'] }} {{ platform['name'] }} + args: + creates: /var/lib/lxd/lxd.db + when: lxd_container_info.results[0].stdout == '[]' + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform diff --git a/molecule/shared/substrate/lxd/destroy.yml b/molecule/shared/substrate/lxd/destroy.yml new file mode 100644 index 00000000..16069602 --- /dev/null +++ b/molecule/shared/substrate/lxd/destroy.yml @@ -0,0 +1,31 @@ +--- +- name: Destroy LXD containers using command module + hosts: all + gather_facts: false + become: true + vars: + molecule_yml: + platforms: + - name: instance + user: ansible + + tasks: + - name: Check if LXD container exists + ansible.builtin.command: lxc list "{{ platform['name'] }}" --format=json + register: lxd_container_info + ignore_errors: true + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Debug container existence + ansible.builtin.debug: + var: lxd_container_info + + - name: Destroy LXD containers if exists using lxc delete + ansible.builtin.command: > + lxc delete {{ platform['name'] }} --force + when: lxd_container_info.results[0].stdout != '[]' + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform