community.general.lxd_container (8.5.0) — module

Manage LXD instances

Authors: Hiroaki Nakamura (@hnakamur)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

Management of LXD containers and virtual machines.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for creating a Ubuntu container and install python
- hosts: localhost
  connection: local
  tasks:
    - name: Create a started container
      community.general.lxd_container:
        name: mycontainer
        ignore_volatile_options: true
        state: started
        source:
          type: image
          mode: pull
          server: https://images.linuxcontainers.org
          protocol: lxd # if you get a 404, try setting protocol: simplestreams
          alias: ubuntu/xenial/amd64
        profiles: ["default"]
        wait_for_ipv4_addresses: true
        timeout: 600

    - name: Check python is installed in container
      delegate_to: mycontainer
      ansible.builtin.raw: dpkg -s python
      register: python_install_check
      failed_when: python_install_check.rc not in [0, 1]
      changed_when: false

    - name: Install python in container
      delegate_to: mycontainer
      ansible.builtin.raw: apt-get install -y python
      when: python_install_check.rc == 1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for creating an Ubuntu 14.04 container using an image fingerprint.
# This requires changing 'server' and 'protocol' key values, replacing the
# 'alias' key with with 'fingerprint' and supplying an appropriate value that
# matches the container image you wish to use.
- hosts: localhost
  connection: local
  tasks:
    - name: Create a started container
      community.general.lxd_container:
        name: mycontainer
        ignore_volatile_options: true
        state: started
        source:
          type: image
          mode: pull
          # Provides current (and older) Ubuntu images with listed fingerprints
          server: https://cloud-images.ubuntu.com/releases
          # Protocol used by 'ubuntu' remote (as shown by 'lxc remote list')
          protocol: simplestreams
          # This provides an Ubuntu 14.04 LTS amd64 image from 20150814.
          fingerprint: e9a8bdfab6dc
        profiles: ["default"]
        wait_for_ipv4_addresses: true
        timeout: 600
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for creating container in project other than default
- hosts: localhost
  connection: local
  tasks:
    - name: Create a started container in project mytestproject
      community.general.lxd_container:
        name: mycontainer
        project: mytestproject
        ignore_volatile_options: true
        state: started
        source:
          protocol: simplestreams
          type: image
          mode: pull
          server: https://images.linuxcontainers.org
          alias: ubuntu/20.04/cloud
        profiles: ["default"]
        wait_for_ipv4_addresses: true
        timeout: 600
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for deleting a container
- hosts: localhost
  connection: local
  tasks:
    - name: Delete a container
      community.general.lxd_container:
        name: mycontainer
        state: absent
        type: container
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for restarting a container
- hosts: localhost
  connection: local
  tasks:
    - name: Restart a container
      community.general.lxd_container:
        name: mycontainer
        state: restarted
        type: container
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for restarting a container using https to connect to the LXD server
- hosts: localhost
  connection: local
  tasks:
    - name: Restart a container
      community.general.lxd_container:
        url: https://127.0.0.1:8443
        # These client_cert and client_key values are equal to the default values.
        #client_cert: "{{ lookup('env', 'HOME') }}/.config/lxc/client.crt"
        #client_key: "{{ lookup('env', 'HOME') }}/.config/lxc/client.key"
        trust_password: mypassword
        name: mycontainer
        state: restarted
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Note your container must be in the inventory for the below example.
#
# [containers]
# mycontainer ansible_connection=lxd
#
- hosts:
    - mycontainer
  tasks:
    - name: Copy /etc/hosts in the created container to localhost with name "mycontainer-hosts"
      ansible.builtin.fetch:
        src: /etc/hosts
        dest: /tmp/mycontainer-hosts
        flat: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for LXD cluster deployments. This example will create two new container on specific
# nodes - 'node01' and 'node02'. In 'target:', 'node01' and 'node02' are names of LXD cluster
# members that LXD cluster recognizes, not ansible inventory names, see: 'lxc cluster list'.
# LXD API calls can be made to any LXD member, in this example, we send API requests to
#'node01.example.com', which matches ansible inventory name.
- hosts: node01.example.com
  tasks:
    - name: Create LXD container
      community.general.lxd_container:
        name: new-container-1
        ignore_volatile_options: true
        state: started
        source:
          type: image
          mode: pull
          alias: ubuntu/xenial/amd64
        target: node01

    - name: Create container on another node
      community.general.lxd_container:
        name: new-container-2
        ignore_volatile_options: true
        state: started
        source:
          type: image
          mode: pull
          alias: ubuntu/xenial/amd64
        target: node02
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# An example for creating a virtual machine
- hosts: localhost
  connection: local
  tasks:
    - name: Create container on another node
      community.general.lxd_container:
        name: new-vm-1
        type: virtual-machine
        state: started
        ignore_volatile_options: true
        wait_for_ipv4_addresses: true
        profiles: ["default"]
        source:
          protocol: simplestreams
          type: image
          mode: pull
          server: https://images.linuxcontainers.org
          alias: debian/11
        timeout: 600

Inputs

    
url:
    default: unix:/var/lib/lxd/unix.socket
    description:
    - The unix domain socket path or the https URL for the LXD server.
    required: false
    type: str

name:
    description:
    - Name of an instance.
    required: true
    type: str

type:
    choices:
    - container
    - virtual-machine
    default: container
    description:
    - Instance type can be either V(virtual-machine) or V(container).
    required: false
    type: str
    version_added: 4.1.0
    version_added_collection: community.general

state:
    choices:
    - started
    - stopped
    - restarted
    - absent
    - frozen
    default: started
    description:
    - Define the state of an instance.
    required: false
    type: str

config:
    description:
    - 'The config for the instance (for example V({"limits.cpu": "2"})). See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).'
    - If the instance already exists and its "config" values in metadata obtained from
      the LXD API U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get)
      are different, then this module tries to apply the configurations U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_put).
    - The keys starting with C(volatile.) are ignored for this comparison when O(ignore_volatile_options=true).
    required: false
    type: dict

source:
    description:
    - 'The source for the instance (for example V({ "type": "image", "mode": "pull", "server":
      "https://images.linuxcontainers.org", "protocol": "lxd", "alias": "ubuntu/xenial/amd64"
      })).'
    - See U(https://documentation.ubuntu.com/lxd/en/latest/api/) for complete API documentation.
    - 'Note that C(protocol) accepts two choices: V(lxd) or V(simplestreams).'
    required: false
    type: dict

target:
    description:
    - For cluster deployments. Will attempt to create an instance on a target node. If
      the instance exists elsewhere in a cluster, then it will not be replaced or moved.
      The name should respond to same name of the node you see in C(lxc cluster list).
    required: false
    type: str
    version_added: 1.0.0
    version_added_collection: community.general

devices:
    description:
    - 'The devices for the instance (for example V({ "rootfs": { "path": "/dev/kvm", "type":
      "unix-char" }})). See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).'
    required: false
    type: dict

project:
    description:
    - Project of an instance. See U(https://documentation.ubuntu.com/lxd/en/latest/projects/).
    required: false
    type: str
    version_added: 4.8.0
    version_added_collection: community.general

timeout:
    default: 30
    description:
    - A timeout for changing the state of the instance.
    - This is also used as a timeout for waiting until IPv4 addresses are set to the all
      network interfaces in the instance after starting or restarting.
    required: false
    type: int

profiles:
    description:
    - Profile to be used by the instance.
    elements: str
    type: list

snap_url:
    default: unix:/var/snap/lxd/common/lxd/unix.socket
    description:
    - The unix domain socket path when LXD is installed by snap package manager.
    required: false
    type: str

ephemeral:
    description:
    - Whether or not the instance is ephemeral (for example V(true) or V(false)). See
      U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).
    required: false
    type: bool

client_key:
    aliases:
    - key_file
    description:
    - The client certificate key file path.
    - If not specified, it defaults to C(${HOME}/.config/lxc/client.key).
    required: false
    type: path

force_stop:
    default: false
    description:
    - If this is V(true), the C(lxd_container) forces to stop the instance when it stops
      or restarts the instance.
    required: false
    type: bool

client_cert:
    aliases:
    - cert_file
    description:
    - The client certificate file path.
    - If not specified, it defaults to C(${HOME}/.config/lxc/client.crt).
    required: false
    type: path

architecture:
    description:
    - The architecture for the instance (for example V(x86_64) or V(i686)). See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).
    required: false
    type: str

trust_password:
    description:
    - The client trusted password.
    - 'You need to set this password on the LXD server before running this module using
      the following command: C(lxc config set core.trust_password <some random password>).
      See U(https://www.stgraber.org/2016/04/18/lxd-api-direct-interaction/).'
    - If trust_password is set, this module send a request for authentication before sending
      any requests.
    required: false
    type: str

wait_for_container:
    default: false
    description:
    - If set to V(true), the tasks will wait till the task reports a success status when
      performing container operations.
    type: bool
    version_added: 4.4.0
    version_added_collection: community.general

ignore_volatile_options:
    default: false
    description:
    - If set to V(true), options starting with C(volatile.) are ignored. As a result,
      they are reapplied for each execution.
    - This default behavior can be changed by setting this option to V(false).
    - The default value changed from V(true) to V(false) in community.general 6.0.0.
    required: false
    type: bool
    version_added: 3.7.0
    version_added_collection: community.general

wait_for_ipv4_addresses:
    default: false
    description:
    - If this is V(true), the C(lxd_container) waits until IPv4 addresses are set to the
      all network interfaces in the instance after starting or restarting.
    required: false
    type: bool

Outputs

actions:
  description: List of actions performed for the instance.
  returned: success
  sample:
  - create
  - start
  type: list
addresses:
  description: Mapping from the network device name to a list of IPv4 addresses in
    the instance.
  returned: when state is started or restarted
  sample:
    eth0:
    - 10.155.92.191
  type: dict
logs:
  description: The logs of requests and responses.
  returned: when ansible-playbook is invoked with -vvvv.
  sample: (too long to be placed here)
  type: list
old_state:
  description: The old state of the instance.
  returned: when state is started or restarted
  sample: stopped
  type: str