A recipe to bootstrap an Alpine Linux based LXD container with Ansible.

First, it creates the container (or reconfigures if it exists and its configuration differs); then, it checks whether Python (which is a prerequisite for Ansible) is installed, and if not, installs it.

---
- hosts: localhost
  gather_facts: no
  connection: local
  tasks:
  - name: Create the container
    lxd_container:
      name: Container
      state: started
      source:
        type: image
        mode: pull
        server: https://images.linuxcontainers.org
        protocol: lxd
        alias: alpine/edge/amd64
      devices: "{{ lxd_devices }}"
      config: "{{ lxd_config }}"
      profiles: ["default"]
      wait_for_ipv4_addresses: true

  - name: Check if Python is installed in the container
    raw: apk info -e python
    register: python_install_check
    changed_when: false
    failed_when: false
    delegate_to: container
  - name: Update package list
    raw: apk update
    when: python_install_check.rc == 1
    failed_when: false
    delegate_to: container
  - name: Install Python
    raw: apk add python
    when: python_install_check.rc == 1
    delegate_to: container

[containers]
container ansible_connection=lxd
---
lxd_devices: {}
lxd_config: {}
Bootstrap Alpine Linux LXD Container with Ansible
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *