Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions molecule/shared/substrate/lxd/create.yml
Original file line number Diff line number Diff line change
@@ -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 }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add the following to ensure Pulumi ESC gets set in the cloud-init file.

    - name: Get ESC from Pulumi
      ansible.builtin.command: esc open deeep-network/dev/services --format dotenv
      changed_when: false
      register: pulumi_esc

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added without testing

- 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 == '[]'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code assumes the first result from lxd_container_info.results corresponds to the current platform in the loop, which may not be true when multiple platforms are defined. To properly match results with platforms, add index_var: platform_index to the loop_control block and reference lxd_container_info.results[platform_index].stdout instead.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed at next phase

loop: "{{ molecule_yml['platforms'] }}"
loop_control:
loop_var: platform
31 changes: 31 additions & 0 deletions molecule/shared/substrate/lxd/destroy.yml
Original file line number Diff line number Diff line change
@@ -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