community.general.lxc_container 模块 – 管理 LXC 容器
注意
此模块是 community.general 集合(版本 10.1.0)的一部分。
如果您正在使用 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,请参阅 要求 以了解详细信息。
要在剧本中使用它,请指定:community.general.lxc_container。
概要
- LXC 容器的管理。 
要求
在执行此模块的主机上需要以下要求。
- lxc >= 2.0 # 操作系统软件包 
- python3 >= 3.5 # 操作系统软件包 
- python3-lxc # 操作系统软件包 
参数
| 参数 | 注释 | 
|---|---|
| 创建容器的归档。 这将创建正在运行的容器的 tarball。 选择 
 | |
| 创建正在运行的容器的归档时要使用的压缩类型。 选择 
 | |
| 保存归档容器的路径。 如果该路径不存在,归档方法将尝试创建它。 | |
| 容器的后端存储类型。 选择 
 | |
| 新克隆的服务器的名称。 这仅在状态为克隆时使用。 | |
| 克隆时创建容器快照。 并非所有容器存储后端都支持此功能。 如果后端存储不支持快照,则启用此功能可能会失败。 选择 
 | |
| LXC 配置文件的路径。 | |
| 在容器内运行命令。 | |
| 配置容器时要使用的  | |
| 为容器启用主机操作的容器日志。 选择 
 | |
| 设置  选择 
 | |
| 将 rootfs 目录放在 DIR 下。 | |
| 文件系统大小。 默认值:  | |
| 创建 fstype TYPE。 默认值:  | |
| 逻辑卷的名称,默认为容器名称。 如果未指定,则默认为  | |
| 将容器放置在  | |
| 容器的名称。 | |
| 定义容器的状态。 如果使用  在克隆操作发生时,运行的容器将被停止,并且在克隆完成后,原始容器状态将被恢复。 选择 
 | |
| 在 LXC 创建中使用的模板名称。 默认值:  | |
| 构建容器时的模板选项。 | |
| 使用名为 TP 的 LVM 精简池。 | |
| 如果后端存储是 LVM,请指定卷组的名称。 默认值:  | |
| 在给定的 zfsroot 下创建 zfs。 | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持: 无 | 可以在  | |
| 支持: 无 | 当处于 diff 模式时,将返回有关已更改的内容(或可能需要在  | 
说明
注意
- 容器必须具有唯一的名称。如果您尝试创建的容器名称在用户命名空间中已存在,该模块将简单地返回“未更改”。 
- container_command可用于除- absent以外的任何状态。如果与状态- stopped一起使用,容器将首先- started,执行命令,然后再次- stopped容器。同样,如果- state=stopped并且容器不存在,它将首先被创建、- started、执行命令,然后- stopped。如果在变量中使用“|”,您可以在变量本身中使用常见的脚本格式。- container_command选项将始终作为 BASH 执行。当使用- container_command时,会在- /tmp/目录中创建一个日志文件,其中包含执行的任何命令的- stdout和- stderr。
- 如果 - archive=true,系统将尝试创建正在运行的容器的压缩 tarball。- archive选项支持 LVM 支持的容器,并且在创建存档时会创建正在运行的容器的快照。
- 如果您的发行版没有 - python3-lxc的软件包(这是此模块的要求),则可以从 https://github.com/lxc/python3-lxc 上的源代码安装,或者使用包名- lxc通过 pip 安装。
示例
- name: Create a started container
  community.general.lxc_container:
    name: test-container-started
    container_log: true
    template: ubuntu
    state: started
    template_options: --release trusty
- name: Create a stopped container
  community.general.lxc_container:
    name: test-container-stopped
    container_log: true
    template: ubuntu
    state: stopped
    template_options: --release trusty
- name: Create a frozen container
  community.general.lxc_container:
    name: test-container-frozen
    container_log: true
    template: ubuntu
    state: frozen
    template_options: --release trusty
    container_command: |
      echo 'hello world.' | tee /opt/started-frozen
# Create filesystem container, configure it, and archive it, and start it.
- name: Create filesystem container
  community.general.lxc_container:
    name: test-container-config
    backing_store: dir
    container_log: true
    template: ubuntu
    state: started
    archive: true
    archive_compression: none
    container_config:
      - "lxc.aa_profile=unconfined"
      - "lxc.cgroup.devices.allow=a *:* rmw"
    template_options: --release trusty
# Create an lvm container, run a complex command in it, add additional
# configuration to it, create an archive of it, and finally leave the container
# in a frozen state. The container archive will be compressed using bzip2
- name: Create a frozen lvm container
  community.general.lxc_container:
    name: test-container-lvm
    container_log: true
    template: ubuntu
    state: frozen
    backing_store: lvm
    template_options: --release trusty
    container_command: |
      apt-get update
      apt-get install -y vim lxc-dev
      echo 'hello world.' | tee /opt/started
      if [[ -f "/opt/started" ]]; then
          echo 'hello world.' | tee /opt/found-started
      fi
    container_config:
      - "lxc.aa_profile=unconfined"
      - "lxc.cgroup.devices.allow=a *:* rmw"
    archive: true
    archive_compression: bzip2
  register: lvm_container_info
- name: Debug info on container "test-container-lvm"
  ansible.builtin.debug:
    var: lvm_container_info
- name: Run a command in a container and ensure its in a "stopped" state.
  community.general.lxc_container:
    name: test-container-started
    state: stopped
    container_command: |
      echo 'hello world.' | tee /opt/stopped
- name: Run a command in a container and ensure its it in a "frozen" state.
  community.general.lxc_container:
    name: test-container-stopped
    state: frozen
    container_command: |
      echo 'hello world.' | tee /opt/frozen
- name: Start a container
  community.general.lxc_container:
    name: test-container-stopped
    state: started
- name: Run a command in a container and then restart it
  community.general.lxc_container:
    name: test-container-started
    state: restarted
    container_command: |
      echo 'hello world.' | tee /opt/restarted
- name: Run a complex command within a "running" container
  community.general.lxc_container:
    name: test-container-started
    container_command: |
      apt-get update
      apt-get install -y curl wget vim apache2
      echo 'hello world.' | tee /opt/started
      if [[ -f "/opt/started" ]]; then
          echo 'hello world.' | tee /opt/found-started
      fi
# Create an archive of an existing container, save the archive to a defined
# path and then destroy it.
- name: Archive container
  community.general.lxc_container:
    name: test-container-started
    state: absent
    archive: true
    archive_path: /opt/archives
# Create a container using overlayfs, create an archive of it, create a
# snapshot clone of the container and and finally leave the container
# in a frozen state. The container archive will be compressed using gzip.
- name: Create an overlayfs container archive and clone it
  community.general.lxc_container:
    name: test-container-overlayfs
    container_log: true
    template: ubuntu
    state: started
    backing_store: overlayfs
    template_options: --release trusty
    clone_snapshot: true
    clone_name: test-container-overlayfs-clone-snapshot
    archive: true
    archive_compression: gzip
  register: clone_container_info
- name: Debug info on container "test-container"
  ansible.builtin.debug:
    var: clone_container_info
- name: Clone a container using snapshot
  community.general.lxc_container:
    name: test-container-overlayfs-clone-snapshot
    backing_store: overlayfs
    clone_name: test-container-overlayfs-clone-snapshot2
    clone_snapshot: true
- name: Create a new container and clone it
  community.general.lxc_container:
    name: test-container-new-archive
    backing_store: dir
    clone_name: test-container-new-archive-clone
- name: Archive and clone a container then destroy it
  community.general.lxc_container:
    name: test-container-new-archive
    state: absent
    clone_name: test-container-new-archive-destroyed-clone
    archive: true
    archive_compression: gzip
- name: Start a cloned container.
  community.general.lxc_container:
    name: test-container-new-archive-destroyed-clone
    state: started
- name: Destroy a container
  community.general.lxc_container:
    name: '{{ item }}'
    state: absent
  with_items:
    - test-container-stopped
    - test-container-started
    - test-container-frozen
    - test-container-lvm
    - test-container-config
    - test-container-overlayfs
    - test-container-overlayfs-clone
    - test-container-overlayfs-clone-snapshot
    - test-container-overlayfs-clone-snapshot2
    - test-container-new-archive
    - test-container-new-archive-clone
    - test-container-new-archive-destroyed-clone
返回值
常见的返回值记录在此处,以下是此模块特有的字段
| 键 | 描述 | 
|---|---|
| 容器信息 返回: 成功 | |
| 容器的最终状态 返回: 成功,当 archive 为 true 时 示例:  | |
| 如果容器被克隆 返回: 成功,当指定 clone_name 时 示例:  | |
| lxc init 进程的 pid 返回: 成功 示例:  | |
| 容器网络接口的列表 返回: 成功 示例:  | |
| ip 列表 返回: 成功 示例:  | |
| lxc 容器的名称 返回: 成功 示例:  | |
| 容器的最终状态 返回: 成功 示例:  | 
