2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2016, Hiroaki Nakamura <hnakamur@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: lxd_container
|
2021-11-20 08:20:24 +01:00
|
|
|
short_description: Manage LXD instances
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- Management of LXD containers and virtual machines.
|
2020-03-09 10:11:07 +01:00
|
|
|
author: "Hiroaki Nakamura (@hnakamur)"
|
2023-02-22 14:41:46 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
2023-02-24 09:22:26 +01:00
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
version_added: 6.4.0
|
|
|
|
diff_mode:
|
|
|
|
support: full
|
|
|
|
version_added: 6.4.0
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- Name of an instance.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2022-04-22 22:32:32 +02:00
|
|
|
project:
|
|
|
|
description:
|
|
|
|
- 'Project of an instance.
|
2023-10-04 19:48:54 +02:00
|
|
|
See U(https://documentation.ubuntu.com/lxd/en/latest/projects/).'
|
2022-04-22 22:32:32 +02:00
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
version_added: 4.8.0
|
2020-03-09 10:11:07 +01:00
|
|
|
architecture:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- 'The architecture for the instance (for example V(x86_64) or V(i686)).
|
2023-10-04 19:48:54 +02:00
|
|
|
See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).'
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
config:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- 'The config for the instance (for example V({"limits.cpu": "2"})).
|
2023-10-04 19:48:54 +02:00
|
|
|
See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).'
|
2021-11-20 08:20:24 +01:00
|
|
|
- If the instance already exists and its "config" values in metadata
|
2023-10-04 19:48:54 +02:00
|
|
|
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).
|
2023-06-15 15:47:13 +02:00
|
|
|
- The keys starting with C(volatile.) are ignored for this comparison when O(ignore_volatile_options=true).
|
2020-12-01 21:28:40 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
2021-09-20 20:31:26 +02:00
|
|
|
ignore_volatile_options:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- If set to V(true), options starting with C(volatile.) are ignored. As a result,
|
2021-09-20 20:31:26 +02:00
|
|
|
they are reapplied for each execution.
|
2023-06-15 15:47:13 +02:00
|
|
|
- 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.
|
2021-09-20 20:31:26 +02:00
|
|
|
type: bool
|
|
|
|
required: false
|
2022-10-25 08:07:21 +02:00
|
|
|
default: false
|
2021-09-20 20:31:26 +02:00
|
|
|
version_added: 3.7.0
|
2020-12-01 21:28:40 +01:00
|
|
|
profiles:
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- Profile to be used by the instance.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: list
|
2021-02-16 07:11:37 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
devices:
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- 'The devices for the instance
|
2023-06-15 15:47:13 +02:00
|
|
|
(for example V({ "rootfs": { "path": "/dev/kvm", "type": "unix-char" }})).
|
2023-10-04 19:48:54 +02:00
|
|
|
See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).'
|
2020-12-01 21:28:40 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
ephemeral:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- Whether or not the instance is ephemeral (for example V(true) or V(false)).
|
2023-10-04 19:48:54 +02:00
|
|
|
See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/instances/instance_get).
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
source:
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- 'The source for the instance
|
2024-04-06 12:17:43 +02:00
|
|
|
(for example V({ "type": "image", "mode": "pull", "server": "https://cloud-images.ubuntu.com/releases/",
|
|
|
|
"protocol": "simplestreams", "alias": "22.04" })).'
|
2023-10-04 19:48:54 +02:00
|
|
|
- 'See U(https://documentation.ubuntu.com/lxd/en/latest/api/) for complete API documentation.'
|
2023-06-15 15:47:13 +02:00
|
|
|
- 'Note that C(protocol) accepts two choices: V(lxd) or V(simplestreams).'
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
2020-12-01 21:28:40 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
choices:
|
|
|
|
- started
|
|
|
|
- stopped
|
|
|
|
- restarted
|
|
|
|
- absent
|
|
|
|
- frozen
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- Define the state of an instance.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
default: started
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-07-30 11:30:25 +02:00
|
|
|
target:
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- 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.
|
2020-07-30 11:30:25 +02:00
|
|
|
The name should respond to same name of the node you see in C(lxc cluster list).
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
version_added: 1.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout:
|
|
|
|
description:
|
2021-11-20 08:20:24 +01:00
|
|
|
- A timeout for changing the state of the instance.
|
2020-03-09 10:11:07 +01:00
|
|
|
- This is also used as a timeout for waiting until IPv4 addresses
|
2021-11-20 08:20:24 +01:00
|
|
|
are set to the all network interfaces in the instance after
|
2020-03-09 10:11:07 +01:00
|
|
|
starting or restarting.
|
|
|
|
required: false
|
|
|
|
default: 30
|
2020-12-01 21:28:40 +01:00
|
|
|
type: int
|
2021-11-20 08:20:24 +01:00
|
|
|
type:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- Instance type can be either V(virtual-machine) or V(container).
|
2021-11-20 08:20:24 +01:00
|
|
|
required: false
|
|
|
|
default: container
|
|
|
|
choices:
|
|
|
|
- container
|
|
|
|
- virtual-machine
|
|
|
|
type: str
|
|
|
|
version_added: 4.1.0
|
2020-03-09 10:11:07 +01:00
|
|
|
wait_for_ipv4_addresses:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- If this is V(true), the C(lxd_container) waits until IPv4 addresses
|
2021-11-20 08:20:24 +01:00
|
|
|
are set to the all network interfaces in the instance after
|
2020-03-09 10:11:07 +01:00
|
|
|
starting or restarting.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
type: bool
|
2022-01-28 07:08:52 +01:00
|
|
|
wait_for_container:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- If set to V(true), the tasks will wait till the task reports a
|
2022-01-28 07:08:52 +01:00
|
|
|
success status when performing container operations.
|
|
|
|
default: false
|
|
|
|
type: bool
|
|
|
|
version_added: 4.4.0
|
2020-03-09 10:11:07 +01:00
|
|
|
force_stop:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- If this is V(true), the C(lxd_container) forces to stop the instance
|
2021-11-20 08:20:24 +01:00
|
|
|
when it stops or restarts the instance.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
type: bool
|
|
|
|
url:
|
|
|
|
description:
|
|
|
|
- The unix domain socket path or the https URL for the LXD server.
|
|
|
|
required: false
|
|
|
|
default: unix:/var/lib/lxd/unix.socket
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
snap_url:
|
|
|
|
description:
|
|
|
|
- The unix domain socket path when LXD is installed by snap package manager.
|
|
|
|
required: false
|
|
|
|
default: unix:/var/snap/lxd/common/lxd/unix.socket
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
client_key:
|
|
|
|
description:
|
|
|
|
- The client certificate key file path.
|
2020-12-01 21:28:40 +01:00
|
|
|
- If not specified, it defaults to C(${HOME}/.config/lxc/client.key).
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
aliases: [ key_file ]
|
2021-02-12 07:58:30 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
client_cert:
|
|
|
|
description:
|
|
|
|
- The client certificate file path.
|
2020-12-01 21:28:40 +01:00
|
|
|
- If not specified, it defaults to C(${HOME}/.config/lxc/client.crt).
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
aliases: [ cert_file ]
|
2021-02-12 07:58:30 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
trust_password:
|
|
|
|
description:
|
|
|
|
- The client trusted password.
|
2021-10-07 06:57:45 +02:00
|
|
|
- '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/).'
|
2020-03-09 10:11:07 +01:00
|
|
|
- If trust_password is set, this module send a request for
|
|
|
|
authentication before sending any requests.
|
|
|
|
required: false
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2021-11-20 08:20:24 +01:00
|
|
|
- Instances can be a container or a virtual machine, both of them must have unique name. If you attempt to create an instance
|
2020-03-09 10:11:07 +01:00
|
|
|
with a name that already existed in the users namespace the module will
|
|
|
|
simply return as "unchanged".
|
2021-11-20 08:20:24 +01:00
|
|
|
- There are two ways to run commands inside a container or virtual machine, using the command
|
2020-03-09 10:11:07 +01:00
|
|
|
module or using the ansible lxd connection plugin bundled in Ansible >=
|
2021-11-20 08:20:24 +01:00
|
|
|
2.1, the later requires python to be installed in the instance which can
|
2020-03-09 10:11:07 +01:00
|
|
|
be done with the command module.
|
2021-11-20 08:20:24 +01:00
|
|
|
- You can copy a file from the host to the instance
|
2023-06-15 15:47:13 +02:00
|
|
|
with the Ansible M(ansible.builtin.copy) and M(ansible.builtin.template) module
|
|
|
|
and the P(community.general.lxd#connection) connection plugin.
|
2020-03-09 10:11:07 +01:00
|
|
|
See the example below.
|
2021-11-20 08:20:24 +01:00
|
|
|
- You can copy a file in the created instance to the localhost
|
2022-06-22 22:43:48 +02:00
|
|
|
with C(command=lxc file pull instance_name/dir/filename filename).
|
2020-03-09 10:11:07 +01:00
|
|
|
See the first example below.
|
2024-04-06 12:17:43 +02:00
|
|
|
- linuxcontainers.org has phased out LXC/LXD support with March 2024
|
|
|
|
(U(https://discuss.linuxcontainers.org/t/important-notice-for-lxd-users-image-server/18479)).
|
|
|
|
Currently only Ubuntu is still providing images.
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# An example for creating a Ubuntu container and install python
|
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Create a started container
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lxd_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: mycontainer
|
2021-09-20 20:31:26 +02:00
|
|
|
ignore_volatile_options: true
|
2020-03-09 10:11:07 +01:00
|
|
|
state: started
|
|
|
|
source:
|
|
|
|
type: image
|
|
|
|
mode: pull
|
2024-04-06 12:17:43 +02:00
|
|
|
server: https://cloud-images.ubuntu.com/releases/
|
|
|
|
protocol: simplestreams
|
|
|
|
alias: "22.04"
|
2020-03-09 10:11:07 +01:00
|
|
|
profiles: ["default"]
|
|
|
|
wait_for_ipv4_addresses: true
|
|
|
|
timeout: 600
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Check python is installed in container
|
2020-03-09 10:11:07 +01:00
|
|
|
delegate_to: mycontainer
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.raw: dpkg -s python
|
2020-03-09 10:11:07 +01:00
|
|
|
register: python_install_check
|
|
|
|
failed_when: python_install_check.rc not in [0, 1]
|
|
|
|
changed_when: false
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Install python in container
|
2020-03-09 10:11:07 +01:00
|
|
|
delegate_to: mycontainer
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.raw: apt-get install -y python
|
2020-03-09 10:11:07 +01:00
|
|
|
when: python_install_check.rc == 1
|
|
|
|
|
|
|
|
# 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
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lxd_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: mycontainer
|
2021-09-20 20:31:26 +02:00
|
|
|
ignore_volatile_options: true
|
2020-03-09 10:11:07 +01:00
|
|
|
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
|
|
|
|
|
2024-04-06 12:17:43 +02:00
|
|
|
# An example of creating a ubuntu-minial container
|
|
|
|
- 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 Ubuntu minimal images
|
|
|
|
server: https://cloud-images.ubuntu.com/minimal/releases/
|
|
|
|
protocol: simplestreams
|
|
|
|
alias: "22.04"
|
|
|
|
profiles: ["default"]
|
|
|
|
wait_for_ipv4_addresses: true
|
|
|
|
timeout: 600
|
|
|
|
|
2022-04-22 22:32:32 +02:00
|
|
|
# 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
|
2024-04-06 12:17:43 +02:00
|
|
|
server: https://cloud-images.ubuntu.com/releases/
|
|
|
|
alias: "22.04"
|
2022-04-22 22:32:32 +02:00
|
|
|
profiles: ["default"]
|
|
|
|
wait_for_ipv4_addresses: true
|
|
|
|
timeout: 600
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
# An example for deleting a container
|
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Delete a container
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lxd_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: mycontainer
|
|
|
|
state: absent
|
2021-11-20 08:20:24 +01:00
|
|
|
type: container
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# An example for restarting a container
|
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Restart a container
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lxd_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: mycontainer
|
|
|
|
state: restarted
|
2021-11-20 08:20:24 +01:00
|
|
|
type: container
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# An example for restarting a container using https to connect to the LXD server
|
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Restart a container
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lxd_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
# Note your container must be in the inventory for the below example.
|
|
|
|
#
|
|
|
|
# [containers]
|
|
|
|
# mycontainer ansible_connection=lxd
|
|
|
|
#
|
|
|
|
- hosts:
|
|
|
|
- mycontainer
|
|
|
|
tasks:
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Copy /etc/hosts in the created container to localhost with name "mycontainer-hosts"
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.fetch:
|
2020-03-09 10:11:07 +01:00
|
|
|
src: /etc/hosts
|
|
|
|
dest: /tmp/mycontainer-hosts
|
|
|
|
flat: true
|
2020-07-30 11:30:25 +02:00
|
|
|
|
|
|
|
# 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
|
2021-09-20 20:31:26 +02:00
|
|
|
ignore_volatile_options: true
|
2020-07-30 11:30:25 +02:00
|
|
|
state: started
|
|
|
|
source:
|
|
|
|
type: image
|
|
|
|
mode: pull
|
2024-04-06 12:17:43 +02:00
|
|
|
alias: "22.04"
|
2020-07-30 11:30:25 +02:00
|
|
|
target: node01
|
|
|
|
|
|
|
|
- name: Create container on another node
|
|
|
|
community.general.lxd_container:
|
|
|
|
name: new-container-2
|
2021-09-20 20:31:26 +02:00
|
|
|
ignore_volatile_options: true
|
2020-07-30 11:30:25 +02:00
|
|
|
state: started
|
|
|
|
source:
|
|
|
|
type: image
|
|
|
|
mode: pull
|
2024-04-06 12:17:43 +02:00
|
|
|
alias: "22.04"
|
2020-07-30 11:30:25 +02:00
|
|
|
target: node02
|
2021-11-20 08:20:24 +01:00
|
|
|
|
|
|
|
# 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
|
2024-05-26 14:08:04 +02:00
|
|
|
server: [...] # URL to the image server
|
2021-11-20 08:20:24 +01:00
|
|
|
alias: debian/11
|
|
|
|
timeout: 600
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
addresses:
|
2021-11-20 08:20:24 +01:00
|
|
|
description: Mapping from the network device name to a list of IPv4 addresses in the instance.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: when state is started or restarted
|
|
|
|
type: dict
|
|
|
|
sample: {"eth0": ["10.155.92.191"]}
|
|
|
|
old_state:
|
2021-11-20 08:20:24 +01:00
|
|
|
description: The old state of the instance.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: when state is started or restarted
|
|
|
|
type: str
|
|
|
|
sample: "stopped"
|
|
|
|
logs:
|
|
|
|
description: The logs of requests and responses.
|
|
|
|
returned: when ansible-playbook is invoked with -vvvv.
|
|
|
|
type: list
|
|
|
|
sample: "(too long to be placed here)"
|
|
|
|
actions:
|
2021-11-20 08:20:24 +01:00
|
|
|
description: List of actions performed for the instance.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: success
|
|
|
|
type: list
|
2022-08-04 20:22:16 +02:00
|
|
|
sample: ["create", "start"]
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
2023-02-22 14:41:46 +01:00
|
|
|
import copy
|
2020-03-09 10:11:07 +01:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.lxd import LXDClient, LXDClientException
|
2020-07-30 11:30:25 +02:00
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# LXD_ANSIBLE_STATES is a map of states that contain values of methods used
|
|
|
|
# when a particular state is evoked.
|
|
|
|
LXD_ANSIBLE_STATES = {
|
|
|
|
'started': '_started',
|
|
|
|
'stopped': '_stopped',
|
|
|
|
'restarted': '_restarted',
|
|
|
|
'absent': '_destroyed',
|
2023-02-22 14:41:46 +01:00
|
|
|
'frozen': '_frozen',
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# ANSIBLE_LXD_STATES is a map of states of lxd containers to the Ansible
|
|
|
|
# lxc_container module state parameter value.
|
|
|
|
ANSIBLE_LXD_STATES = {
|
|
|
|
'Running': 'started',
|
|
|
|
'Stopped': 'stopped',
|
|
|
|
'Frozen': 'frozen',
|
|
|
|
}
|
|
|
|
|
|
|
|
# ANSIBLE_LXD_DEFAULT_URL is a default value of the lxd endpoint
|
|
|
|
ANSIBLE_LXD_DEFAULT_URL = 'unix:/var/lib/lxd/unix.socket'
|
|
|
|
|
|
|
|
# CONFIG_PARAMS is a list of config attribute names.
|
|
|
|
CONFIG_PARAMS = [
|
2024-02-28 21:45:46 +01:00
|
|
|
'architecture', 'config', 'devices', 'ephemeral', 'profiles', 'source', 'type'
|
2020-03-09 10:11:07 +01:00
|
|
|
]
|
|
|
|
|
2023-02-22 14:41:46 +01:00
|
|
|
# CONFIG_CREATION_PARAMS is a list of attribute names that are only applied
|
|
|
|
# on instance creation.
|
2024-02-28 21:45:46 +01:00
|
|
|
CONFIG_CREATION_PARAMS = ['source', 'type']
|
2023-02-22 14:41:46 +01:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
class LXDContainerManagement(object):
|
|
|
|
def __init__(self, module):
|
|
|
|
"""Management of LXC containers via Ansible.
|
|
|
|
|
|
|
|
:param module: Processed Ansible Module.
|
|
|
|
:type module: ``object``
|
|
|
|
"""
|
|
|
|
self.module = module
|
|
|
|
self.name = self.module.params['name']
|
2022-04-22 22:32:32 +02:00
|
|
|
self.project = self.module.params['project']
|
2020-03-09 10:11:07 +01:00
|
|
|
self._build_config()
|
|
|
|
|
|
|
|
self.state = self.module.params['state']
|
|
|
|
|
|
|
|
self.timeout = self.module.params['timeout']
|
|
|
|
self.wait_for_ipv4_addresses = self.module.params['wait_for_ipv4_addresses']
|
|
|
|
self.force_stop = self.module.params['force_stop']
|
|
|
|
self.addresses = None
|
2020-07-30 11:30:25 +02:00
|
|
|
self.target = self.module.params['target']
|
2022-01-28 07:08:52 +01:00
|
|
|
self.wait_for_container = self.module.params['wait_for_container']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
self.type = self.module.params['type']
|
|
|
|
|
2020-12-01 21:28:40 +01:00
|
|
|
self.key_file = self.module.params.get('client_key')
|
|
|
|
if self.key_file is None:
|
|
|
|
self.key_file = '{0}/.config/lxc/client.key'.format(os.environ['HOME'])
|
|
|
|
self.cert_file = self.module.params.get('client_cert')
|
|
|
|
if self.cert_file is None:
|
|
|
|
self.cert_file = '{0}/.config/lxc/client.crt'.format(os.environ['HOME'])
|
2020-03-09 10:11:07 +01:00
|
|
|
self.debug = self.module._verbosity >= 4
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self.module.params['url'] != ANSIBLE_LXD_DEFAULT_URL:
|
|
|
|
self.url = self.module.params['url']
|
|
|
|
elif os.path.exists(self.module.params['snap_url'].replace('unix:', '')):
|
|
|
|
self.url = self.module.params['snap_url']
|
|
|
|
else:
|
|
|
|
self.url = self.module.params['url']
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg=e.msg)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.client = LXDClient(
|
|
|
|
self.url, key_file=self.key_file, cert_file=self.cert_file,
|
|
|
|
debug=self.debug
|
|
|
|
)
|
|
|
|
except LXDClientException as e:
|
|
|
|
self.module.fail_json(msg=e.msg)
|
2024-02-28 21:45:46 +01:00
|
|
|
|
|
|
|
# LXD (3.19) Rest API provides instances endpoint, failback to containers and virtual-machines
|
|
|
|
# https://documentation.ubuntu.com/lxd/en/latest/rest-api/#instances-containers-and-virtual-machines
|
|
|
|
self.api_endpoint = '/1.0/instances'
|
|
|
|
check_api_endpoint = self.client.do('GET', '{0}?project='.format(self.api_endpoint), ok_error_codes=[404])
|
|
|
|
|
|
|
|
if check_api_endpoint['error_code'] == 404:
|
|
|
|
if self.type == 'container':
|
|
|
|
self.api_endpoint = '/1.0/containers'
|
|
|
|
elif self.type == 'virtual-machine':
|
|
|
|
self.api_endpoint = '/1.0/virtual-machines'
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
self.trust_password = self.module.params.get('trust_password', None)
|
|
|
|
self.actions = []
|
2023-02-22 14:41:46 +01:00
|
|
|
self.diff = {'before': {}, 'after': {}}
|
|
|
|
self.old_instance_json = {}
|
|
|
|
self.old_sections = {}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def _build_config(self):
|
|
|
|
self.config = {}
|
|
|
|
for attr in CONFIG_PARAMS:
|
|
|
|
param_val = self.module.params.get(attr, None)
|
|
|
|
if param_val is not None:
|
|
|
|
self.config[attr] = param_val
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _get_instance_json(self):
|
2022-04-22 22:32:32 +02:00
|
|
|
url = '{0}/{1}'.format(self.api_endpoint, self.name)
|
|
|
|
if self.project:
|
|
|
|
url = '{0}?{1}'.format(url, urlencode(dict(project=self.project)))
|
|
|
|
return self.client.do('GET', url, ok_error_codes=[404])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _get_instance_state_json(self):
|
2022-04-22 22:32:32 +02:00
|
|
|
url = '{0}/{1}/state'.format(self.api_endpoint, self.name)
|
|
|
|
if self.project:
|
|
|
|
url = '{0}?{1}'.format(url, urlencode(dict(project=self.project)))
|
|
|
|
return self.client.do('GET', url, ok_error_codes=[404])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2021-11-20 08:20:24 +01:00
|
|
|
def _instance_json_to_module_state(resp_json):
|
2020-03-09 10:11:07 +01:00
|
|
|
if resp_json['type'] == 'error':
|
|
|
|
return 'absent'
|
|
|
|
return ANSIBLE_LXD_STATES[resp_json['metadata']['status']]
|
|
|
|
|
|
|
|
def _change_state(self, action, force_stop=False):
|
2022-04-22 22:32:32 +02:00
|
|
|
url = '{0}/{1}/state'.format(self.api_endpoint, self.name)
|
|
|
|
if self.project:
|
|
|
|
url = '{0}?{1}'.format(url, urlencode(dict(project=self.project)))
|
2020-03-09 10:11:07 +01:00
|
|
|
body_json = {'action': action, 'timeout': self.timeout}
|
|
|
|
if force_stop:
|
|
|
|
body_json['force'] = True
|
2023-02-22 14:41:46 +01:00
|
|
|
if not self.module.check_mode:
|
|
|
|
return self.client.do('PUT', url, body_json=body_json)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _create_instance(self):
|
2022-04-22 22:32:32 +02:00
|
|
|
url = self.api_endpoint
|
|
|
|
url_params = dict()
|
|
|
|
if self.target:
|
|
|
|
url_params['target'] = self.target
|
|
|
|
if self.project:
|
|
|
|
url_params['project'] = self.project
|
|
|
|
if url_params:
|
|
|
|
url = '{0}?{1}'.format(url, urlencode(url_params))
|
2020-03-09 10:11:07 +01:00
|
|
|
config = self.config.copy()
|
|
|
|
config['name'] = self.name
|
2024-02-28 21:45:46 +01:00
|
|
|
if self.type not in self.api_endpoint:
|
|
|
|
config['type'] = self.type
|
2023-02-22 14:41:46 +01:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.do('POST', url, config, wait_for_container=self.wait_for_container)
|
2020-03-09 10:11:07 +01:00
|
|
|
self.actions.append('create')
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _start_instance(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
self._change_state('start')
|
|
|
|
self.actions.append('start')
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _stop_instance(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
self._change_state('stop', self.force_stop)
|
|
|
|
self.actions.append('stop')
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _restart_instance(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
self._change_state('restart', self.force_stop)
|
|
|
|
self.actions.append('restart')
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _delete_instance(self):
|
2022-04-22 22:32:32 +02:00
|
|
|
url = '{0}/{1}'.format(self.api_endpoint, self.name)
|
|
|
|
if self.project:
|
|
|
|
url = '{0}?{1}'.format(url, urlencode(dict(project=self.project)))
|
2023-02-22 14:41:46 +01:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.do('DELETE', url)
|
2020-03-09 10:11:07 +01:00
|
|
|
self.actions.append('delete')
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _freeze_instance(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
self._change_state('freeze')
|
|
|
|
self.actions.append('freeze')
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _unfreeze_instance(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
self._change_state('unfreeze')
|
2023-02-22 14:41:46 +01:00
|
|
|
self.actions.append('unfreeze')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _instance_ipv4_addresses(self, ignore_devices=None):
|
2020-03-09 10:11:07 +01:00
|
|
|
ignore_devices = ['lo'] if ignore_devices is None else ignore_devices
|
2023-02-22 14:41:46 +01:00
|
|
|
data = (self._get_instance_state_json() or {}).get('metadata', None) or {}
|
2024-09-01 20:55:02 +02:00
|
|
|
network = {
|
|
|
|
k: v
|
|
|
|
for k, v in data.get('network', {}).items()
|
|
|
|
if k not in ignore_devices
|
|
|
|
}
|
|
|
|
addresses = {
|
|
|
|
k: [a['address'] for a in v['addresses'] if a['family'] == 'inet']
|
|
|
|
for k, v in network.items()
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
return addresses
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _has_all_ipv4_addresses(addresses):
|
|
|
|
return len(addresses) > 0 and all(len(v) > 0 for v in addresses.values())
|
|
|
|
|
|
|
|
def _get_addresses(self):
|
|
|
|
try:
|
|
|
|
due = datetime.datetime.now() + datetime.timedelta(seconds=self.timeout)
|
|
|
|
while datetime.datetime.now() < due:
|
|
|
|
time.sleep(1)
|
2021-11-20 08:20:24 +01:00
|
|
|
addresses = self._instance_ipv4_addresses()
|
2023-02-22 14:41:46 +01:00
|
|
|
if self._has_all_ipv4_addresses(addresses) or self.module.check_mode:
|
2020-03-09 10:11:07 +01:00
|
|
|
self.addresses = addresses
|
|
|
|
return
|
|
|
|
except LXDClientException as e:
|
|
|
|
e.msg = 'timeout for getting IPv4 addresses'
|
|
|
|
raise
|
|
|
|
|
|
|
|
def _started(self):
|
|
|
|
if self.old_state == 'absent':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._create_instance()
|
|
|
|
self._start_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if self.old_state == 'frozen':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._unfreeze_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
elif self.old_state == 'stopped':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._start_instance()
|
|
|
|
if self._needs_to_apply_instance_configs():
|
|
|
|
self._apply_instance_configs()
|
2020-03-09 10:11:07 +01:00
|
|
|
if self.wait_for_ipv4_addresses:
|
|
|
|
self._get_addresses()
|
|
|
|
|
|
|
|
def _stopped(self):
|
|
|
|
if self.old_state == 'absent':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._create_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if self.old_state == 'stopped':
|
2021-11-20 08:20:24 +01:00
|
|
|
if self._needs_to_apply_instance_configs():
|
|
|
|
self._start_instance()
|
|
|
|
self._apply_instance_configs()
|
|
|
|
self._stop_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if self.old_state == 'frozen':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._unfreeze_instance()
|
|
|
|
if self._needs_to_apply_instance_configs():
|
|
|
|
self._apply_instance_configs()
|
|
|
|
self._stop_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def _restarted(self):
|
|
|
|
if self.old_state == 'absent':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._create_instance()
|
|
|
|
self._start_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if self.old_state == 'frozen':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._unfreeze_instance()
|
|
|
|
if self._needs_to_apply_instance_configs():
|
|
|
|
self._apply_instance_configs()
|
|
|
|
self._restart_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
if self.wait_for_ipv4_addresses:
|
|
|
|
self._get_addresses()
|
|
|
|
|
|
|
|
def _destroyed(self):
|
|
|
|
if self.old_state != 'absent':
|
|
|
|
if self.old_state == 'frozen':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._unfreeze_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
if self.old_state != 'stopped':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._stop_instance()
|
|
|
|
self._delete_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def _frozen(self):
|
|
|
|
if self.old_state == 'absent':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._create_instance()
|
|
|
|
self._start_instance()
|
|
|
|
self._freeze_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if self.old_state == 'stopped':
|
2021-11-20 08:20:24 +01:00
|
|
|
self._start_instance()
|
|
|
|
if self._needs_to_apply_instance_configs():
|
|
|
|
self._apply_instance_configs()
|
|
|
|
self._freeze_instance()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _needs_to_change_instance_config(self, key):
|
2020-03-09 10:11:07 +01:00
|
|
|
if key not in self.config:
|
|
|
|
return False
|
2023-02-22 14:41:46 +01:00
|
|
|
|
|
|
|
if key == 'config':
|
|
|
|
# self.old_sections is already filtered for volatile keys if necessary
|
|
|
|
old_configs = dict(self.old_sections.get(key, None) or {})
|
2021-09-20 20:31:26 +02:00
|
|
|
for k, v in self.config['config'].items():
|
|
|
|
if k not in old_configs:
|
|
|
|
return True
|
|
|
|
if old_configs[k] != v:
|
|
|
|
return True
|
|
|
|
return False
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2023-02-22 14:41:46 +01:00
|
|
|
old_configs = self.old_sections.get(key, {})
|
2020-03-09 10:11:07 +01:00
|
|
|
return self.config[key] != old_configs
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _needs_to_apply_instance_configs(self):
|
2023-02-22 14:41:46 +01:00
|
|
|
for param in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS):
|
|
|
|
if self._needs_to_change_instance_config(param):
|
|
|
|
return True
|
|
|
|
return False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
def _apply_instance_configs(self):
|
2023-02-22 14:41:46 +01:00
|
|
|
old_metadata = copy.deepcopy(self.old_instance_json).get('metadata', None) or {}
|
|
|
|
body_json = {}
|
|
|
|
for param in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS):
|
|
|
|
if param in old_metadata:
|
|
|
|
body_json[param] = old_metadata[param]
|
|
|
|
|
|
|
|
if self._needs_to_change_instance_config(param):
|
|
|
|
if param == 'config':
|
|
|
|
body_json['config'] = body_json.get('config', None) or {}
|
|
|
|
for k, v in self.config['config'].items():
|
|
|
|
body_json['config'][k] = v
|
|
|
|
else:
|
|
|
|
body_json[param] = self.config[param]
|
|
|
|
self.diff['after']['instance'] = body_json
|
2022-04-22 22:32:32 +02:00
|
|
|
url = '{0}/{1}'.format(self.api_endpoint, self.name)
|
|
|
|
if self.project:
|
|
|
|
url = '{0}?{1}'.format(url, urlencode(dict(project=self.project)))
|
2023-02-22 14:41:46 +01:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.do('PUT', url, body_json=body_json)
|
2021-11-20 08:20:24 +01:00
|
|
|
self.actions.append('apply_instance_configs')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Run the main method."""
|
|
|
|
|
2024-09-01 20:55:02 +02:00
|
|
|
def adjust_content(content):
|
|
|
|
return content if not isinstance(content, dict) else {
|
|
|
|
k: v for k, v in content.items() if not (self.ignore_volatile_options and k.startswith('volatile.'))
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
if self.trust_password is not None:
|
|
|
|
self.client.authenticate(self.trust_password)
|
2021-09-20 20:31:26 +02:00
|
|
|
self.ignore_volatile_options = self.module.params.get('ignore_volatile_options')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
self.old_instance_json = self._get_instance_json()
|
2024-09-01 20:55:02 +02:00
|
|
|
self.old_sections = {
|
|
|
|
section: adjust_content(content)
|
|
|
|
for section, content in self.old_instance_json.get('metadata', {}).items()
|
2023-02-22 14:41:46 +01:00
|
|
|
if section in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS)
|
2024-09-01 20:55:02 +02:00
|
|
|
}
|
2023-02-22 14:41:46 +01:00
|
|
|
|
|
|
|
self.diff['before']['instance'] = self.old_sections
|
|
|
|
# preliminary, will be overwritten in _apply_instance_configs() if called
|
|
|
|
self.diff['after']['instance'] = self.config
|
|
|
|
|
2021-11-20 08:20:24 +01:00
|
|
|
self.old_state = self._instance_json_to_module_state(self.old_instance_json)
|
2023-02-22 14:41:46 +01:00
|
|
|
self.diff['before']['state'] = self.old_state
|
|
|
|
self.diff['after']['state'] = self.state
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
action = getattr(self, LXD_ANSIBLE_STATES[self.state])
|
|
|
|
action()
|
|
|
|
|
|
|
|
state_changed = len(self.actions) > 0
|
|
|
|
result_json = {
|
|
|
|
'log_verbosity': self.module._verbosity,
|
|
|
|
'changed': state_changed,
|
|
|
|
'old_state': self.old_state,
|
2023-02-22 14:41:46 +01:00
|
|
|
'actions': self.actions,
|
|
|
|
'diff': self.diff,
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
if self.client.debug:
|
|
|
|
result_json['logs'] = self.client.logs
|
|
|
|
if self.addresses is not None:
|
|
|
|
result_json['addresses'] = self.addresses
|
|
|
|
self.module.exit_json(**result_json)
|
|
|
|
except LXDClientException as e:
|
|
|
|
state_changed = len(self.actions) > 0
|
|
|
|
fail_params = {
|
|
|
|
'msg': e.msg,
|
|
|
|
'changed': state_changed,
|
2023-02-22 14:41:46 +01:00
|
|
|
'actions': self.actions,
|
|
|
|
'diff': self.diff,
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
if self.client.debug:
|
|
|
|
fail_params['logs'] = e.kwargs['logs']
|
|
|
|
self.module.fail_json(**fail_params)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Ansible Main module."""
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(
|
|
|
|
type='str',
|
2023-02-22 14:41:46 +01:00
|
|
|
required=True,
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2022-04-22 22:32:32 +02:00
|
|
|
project=dict(
|
|
|
|
type='str',
|
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
architecture=dict(
|
|
|
|
type='str',
|
|
|
|
),
|
|
|
|
config=dict(
|
|
|
|
type='dict',
|
|
|
|
),
|
2021-09-20 20:31:26 +02:00
|
|
|
ignore_volatile_options=dict(
|
|
|
|
type='bool',
|
2022-10-25 08:07:21 +02:00
|
|
|
default=False,
|
2021-09-20 20:31:26 +02:00
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
devices=dict(
|
|
|
|
type='dict',
|
|
|
|
),
|
|
|
|
ephemeral=dict(
|
|
|
|
type='bool',
|
|
|
|
),
|
|
|
|
profiles=dict(
|
|
|
|
type='list',
|
2021-02-16 07:11:37 +01:00
|
|
|
elements='str',
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
source=dict(
|
|
|
|
type='dict',
|
|
|
|
),
|
|
|
|
state=dict(
|
2021-02-23 23:19:27 +01:00
|
|
|
choices=list(LXD_ANSIBLE_STATES.keys()),
|
2023-02-22 14:41:46 +01:00
|
|
|
default='started',
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2020-07-30 11:30:25 +02:00
|
|
|
target=dict(
|
|
|
|
type='str',
|
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout=dict(
|
|
|
|
type='int',
|
|
|
|
default=30
|
|
|
|
),
|
2021-11-20 08:20:24 +01:00
|
|
|
type=dict(
|
|
|
|
type='str',
|
|
|
|
default='container',
|
|
|
|
choices=['container', 'virtual-machine'],
|
|
|
|
),
|
2022-01-28 07:08:52 +01:00
|
|
|
wait_for_container=dict(
|
|
|
|
type='bool',
|
2023-02-22 14:41:46 +01:00
|
|
|
default=False,
|
2022-01-28 07:08:52 +01:00
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
wait_for_ipv4_addresses=dict(
|
|
|
|
type='bool',
|
2023-02-22 14:41:46 +01:00
|
|
|
default=False,
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
force_stop=dict(
|
|
|
|
type='bool',
|
2023-02-22 14:41:46 +01:00
|
|
|
default=False,
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
url=dict(
|
|
|
|
type='str',
|
2023-02-22 14:41:46 +01:00
|
|
|
default=ANSIBLE_LXD_DEFAULT_URL,
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
snap_url=dict(
|
|
|
|
type='str',
|
2023-02-22 14:41:46 +01:00
|
|
|
default='unix:/var/snap/lxd/common/lxd/unix.socket',
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
client_key=dict(
|
2021-02-12 07:58:30 +01:00
|
|
|
type='path',
|
2023-02-22 14:41:46 +01:00
|
|
|
aliases=['key_file'],
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
client_cert=dict(
|
2021-02-12 07:58:30 +01:00
|
|
|
type='path',
|
2023-02-22 14:41:46 +01:00
|
|
|
aliases=['cert_file'],
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2023-02-22 14:41:46 +01:00
|
|
|
trust_password=dict(type='str', no_log=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2023-02-22 14:41:46 +01:00
|
|
|
supports_check_mode=True,
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
2021-09-28 07:07:55 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
lxd_manage = LXDContainerManagement(module=module)
|
|
|
|
lxd_manage.run()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|