openstack.cloud.resource 模块 – 管理 OpenStack 云资源

注意

此模块是 openstack.cloud 集合 (版本 2.3.0) 的一部分。

如果您使用的是 ansible 包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install openstack.cloud。您需要其他要求才能使用此模块,有关详细信息,请参阅 要求

要在 playbook 中使用它,请指定:openstack.cloud.resource

概要

  • 创建、更新和删除 OpenStack 云资源。

要求

执行此模块的主机需要以下要求。

  • python >= 3.6

  • openstacksdk >= 1.0.0

参数

参数

注释

api_timeout

整数

套接字层在 API 调用超时之前应等待多长时间。如果省略此参数,则不会将任何内容传递给 requests 库。

attributes

字典 / 必需

在 openstacksdk 的资源类中定义的资源属性。

attributes 是一组键值对,其中每个键都是一个属性名,例如 id,而值则包含其对应的属性值,例如 ddad2d86-02a6-444d-80ae-1cc2fb023784

定义属性键 idname 或任何一组唯一标识资源的属性键。如果多个资源与给定的属性集匹配,则此模块将失败。

有关属性的完整列表,请打开 openstacksdk 中的任何资源类,例如 https://opendev.org/openstack/openstacksdk/ 中的 openstack/compute/v2/server.py 文件,以查看服务器属性。

auth

字典

包含云的认证插件策略所需的认证信息。对于默认的 *password* 插件,这将包含 *auth_url*、*username*、*password*、*project_name* 以及任何关于域的信息(例如,*user_domain_name* 或 *project_domain_name*),如果云支持它们的话。对于其他插件,此参数需要包含该认证插件所需的任何参数。如果提供了命名的云或存在 OpenStack OS_* 环境变量,则不需要此参数。

auth_type

字符串

要使用的认证插件的名称。如果云使用密码认证以外的其他方式,则应在此处指示插件的名称,并相应地更新 *auth* 参数的内容。

ca_cert

别名:cacert

字符串

CA 证书捆绑包的路径,可用作验证 SSL API 请求的一部分。

client_cert

别名:cert

字符串

客户端证书的路径,用作 SSL 事务的一部分。

client_key

别名:key

字符串

客户端密钥的路径,用作 SSL 事务的一部分。

cloud

任意

要操作的命名云或云配置。如果 *cloud* 是字符串,则它引用在 OpenStack clouds.yaml 文件中定义的命名云配置。为 *auth* 和 *auth_type* 提供默认值。如果提供了 *auth* 或存在 OpenStack OS_* 环境变量,则不需要此参数。如果 *cloud* 是字典,则它包含完整的云配置,就像 clouds.yaml 的一部分一样。

interface

别名:endpoint_type

字符串

从服务目录中获取的端点 URL 类型。

选择项

  • "admin"

  • "internal"

  • "public" ← (默认)

non_updateable_attributes

列表 / 元素=字符串

无法更新的属性名称列表。

当未指定 *non_updateable_attributes* 时,在更新期间将比较 *attributes* 中的所有属性与现有资源。

当同时指定 *updateable_attributes* 和 *non_updateable_attributes* 时,在更新期间,只有列在 *updateable_attributes* 中但不在 *non_updateable_attributes* 中的属性才会被考虑。

region_name

字符串

区域名称。

sdk_log_level

字符串

OpenStackSDK 的日志级别

选择项

  • "INFO" ← (默认)

  • "DEBUG"

sdk_log_path

字符串

OpenStackSDK 日志文件的路径。如果为空,则不写入日志

service

字符串 / 必需

此资源所属的 OpenStack 服务。

例如 block_storagecomputenetwork

service 必须是 openstacksdk 中使用的 OpenStack 服务的小写名称。有关可用服务的列表,请访问 https://opendev.org/openstack/openstacksdkopenstack 目录中的大多数子目录对应于 OpenStack 服务,但 cloudcommon 和其他辅助目录除外。

state

字符串

资源应该是 present 还是 absent

选择项

  • "present" ← (默认)

  • "absent"

timeout

整数

Ansible 应该等待请求的资源多长时间。

默认值: 180

type

字符串 / 必需

资源的类型名称。

例如 ipnetworkrouterserver

type 必须是小写的 openstacksdk 资源类名称。资源类定义在 openstacksdk 的服务文件夹中。例如,访问 https://opendev.org/openstack/openstacksdk,切换到 openstack 目录,切换到任何服务目录,例如 compute,选择一个 API 版本目录,例如 v2,并在 *.py 文件中查找所有可用的资源类,例如 Server

updateable_attributes

列表 / 元素=字符串

可以更新的属性名称列表。

如果未指定 updateable_attributes,则在更新期间会将 attributes 中的所有属性与现有资源进行比较。

当同时指定 *updateable_attributes* 和 *non_updateable_attributes* 时,在更新期间,只有列在 *updateable_attributes* 中但不在 *non_updateable_attributes* 中的属性才会被考虑。

validate_certs

别名:verify

布尔值

是否验证 SSL API 请求。

在 Ansible 2.3 之前,默认值为 true

选择项

  • false

  • true

wait

布尔值

Ansible 是否应该等到资源达到其目标 state

只有一部分 OpenStack 资源会报告状态。如果 wait 设置为 true,则不支持状态处理的资源将无限期阻塞。

选择项

  • false ← (默认)

  • true

注释

注意

  • 此模块不支持所有 OpenStack 云资源。资源处理必须遵循 openstacksdk 的 CRUD 结构,使用并提供 <service>.<type>s<service>.find_<type><service>.create_<type><service>.update_<type><service>.delete_<type> 函数。如果找不到这些函数,模块将在应用任何更改之前失败。

  • 可以使用标准的 OpenStack 环境变量(例如 OS_USERNAME),而不是提供显式值。

  • 身份验证信息由 openstacksdk 驱动,这意味着值可以来自 /etc/ansible/openstack.yaml、/etc/openstack/clouds.yaml 或 ~/.config/openstack/clouds.yaml 中的 yaml 配置文件,然后来自标准环境变量,最后来自 playbook 中的显式参数。更多信息可在 https://docs.openstack.org/openstacksdk/ 找到。

示例

- name: Create external network
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: network
    attributes:
      name: ansible_network_external
      is_router_external: true
    wait: true
  register: network_external

- name: Create external subnet
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: subnet
    attributes:
      cidr: 10.6.6.0/24
      ip_version: 4
      name: ansible_external_subnet
      network_id: "{{ network_external.resource.id }}"
  register: subnet_external

- name: Create external port
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: port
    attributes:
      name: ansible_port_external
      network_id: "{{ network_external.resource.id }}"
      fixed_ips:
        - ip_address: 10.6.6.50
    non_updateable_attributes:
      - fixed_ips

- name: Create internal network
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: network
    attributes:
      name: ansible_network_internal
      is_router_external: false
    wait: true
  register: network_internal

- name: Create internal subnet
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: subnet
    attributes:
      cidr: 10.7.7.0/24
      ip_version: 4
      name: ansible_internal_subnet
      network_id: "{{ network_internal.resource.id }}"
  register: subnet_internal

- name: Create internal port
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: port
    attributes:
      name: ansible_port_internal
      network_id: "{{ network_internal.resource.id }}"
      fixed_ips:
        - ip_address: 10.7.7.100
          subnet_id: "{{ subnet_internal.resource.id }}"
  register: port_internal

- name: Create router
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: router
    attributes:
      name: ansible_router
      external_gateway_info:
        enable_snat: true
        external_fixed_ips:
          - ip_address: 10.6.6.10
            subnet_id: "{{ subnet_external.resource.id }}"
        network_id: "{{ network_external.resource.id }}"
    wait: true

- name: Attach router to internal subnet
  openstack.cloud.router:
    cloud: devstack-admin
    name: ansible_router
    network: "{{ network_external.resource.id }}"
    external_fixed_ips:
      - ip: 10.6.6.10
        subnet: "{{ subnet_external.resource.id }}"
    interfaces:
      - net: "{{ network_internal.resource.id }}"
        subnet: "{{ subnet_internal.resource.id }}"
        portip: 10.7.7.1

- name: Create floating ip address
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: ip
    attributes:
      name: 10.6.6.150
      floating_ip_address: 10.6.6.150
      floating_network_id: "{{ network_external.resource.id }}"
      port_id: "{{ port_internal.resource.id }}"
  register: ip

- name: List images
  openstack.cloud.resources:
    cloud: devstack-admin
    service: image
    type: image
  register: images

- name: Identify CirrOS image id
  set_fact:
    image_id: "{{
      images.resources|community.general.json_query(query)|first }}"
  vars:
    query: "[?starts_with(name, 'cirros')].id"

- name: List compute flavors
  openstack.cloud.resources:
    cloud: devstack-admin
    service: compute
    type: flavor
  register: flavors

- name: Identify m1.tiny flavor id
  set_fact:
    flavor_id: "{{
      flavors.resources|community.general.json_query(query)|first }}"
  vars:
    query: "[?name == 'm1.tiny'].id"

- name: Create server
  openstack.cloud.resource:
    cloud: devstack-admin
    service: compute
    type: server
    attributes:
      name: ansible_server
      image_id: "{{ image_id }}"
      flavor_id: "{{ flavor_id }}"
      networks:
        - uuid: "{{ network_internal.resource.id }}"
          port: "{{ port_internal.resource.id }}"
    non_updateable_attributes:
      - name
      - image_id
      - flavor_id
      - networks
    wait: true

- name: Detach floating ip address
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: ip
    attributes:
      floating_ip_address: 10.6.6.150
      port_id: !!null

- name: Delete server
  openstack.cloud.resource:
    cloud: devstack-admin
    service: compute
    type: server
    attributes:
      name: ansible_server
    state: absent
    wait: true

- name: Delete floating ip address
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: ip
    attributes:
      floating_ip_address: 10.6.6.150
    state: absent

- name: Detach router from internal subnet
  openstack.cloud.router:
    cloud: devstack-admin
    name: ansible_router
    network: "{{ network_external.resource.id }}"
    external_fixed_ips:
      - ip: 10.6.6.10
        subnet: "{{ subnet_external.resource.id }}"
    interfaces: []

- name: Delete router
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: router
    attributes:
      name: ansible_router
    state: absent
    wait: true

- name: Delete internal port
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: port
    attributes:
      name: ansible_port_internal
    state: absent

- name: Delete internal subnet
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: subnet
    attributes:
      name: ansible_internal_subnet
    state: absent

- name: Delete internal network
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: network
    attributes:
      name: ansible_network_internal
    state: absent
    wait: true

- name: Delete external port
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: port
    attributes:
      name: ansible_port_external
    state: absent

- name: Delete external subnet
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: subnet
    attributes:
      name: ansible_external_subnet
    state: absent

- name: Delete external network
  openstack.cloud.resource:
    cloud: devstack-admin
    service: network
    type: network
    attributes:
      name: ansible_network_external
    state: absent
    wait: true

返回值

公共返回值已在 此处 记录,以下是此模块特有的字段

描述

resource

字典

描述已识别(并可能已修改)的 OpenStack 云资源的字典。

返回值:statepresent 时成功。

作者

  • OpenStack Ansible SIG