community.hrobot.robot 库存 – Hetzner Robot 库存来源

注意

此库存插件是 community.hrobot 集合 (版本 2.0.2) 的一部分。

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

要安装它,请使用: ansible-galaxy collection install community.hrobot

要在剧本中使用它,请指定: community.hrobot.robot

community.hrobot 1.1.0 中的新增功能

概要

  • 从 Hetzner Robot API 读取服务器。

  • 使用以 robot.ymlrobot.yaml 结尾的 YAML 配置文件。

  • 库存插件将来自 https://robot.your-server.de/doc/webservice/en.html#get-server 的所有值(以 hrobot_ 开头)添加到服务器的库存中。例如,变量 hrobot_dc 包含服务器所在的数据中心。

参数

参数

注释

cache

布尔值

切换以启用/禁用库存源数据的缓存,需要设置缓存插件才能工作。

选项

  • false ← (默认)

  • true

配置

cache_connection

字符串

缓存连接数据或路径,请阅读缓存插件文档以了解详细信息。

配置

cache_plugin

字符串

要用于库存源数据的缓存插件。

默认值: "memory"

配置

cache_prefix

字符串

用于缓存插件文件/表的前缀。

默认值: "ansible_inventory_"

配置

cache_timeout

整数

缓存持续时间(秒)。

默认值: 3600

配置

compose

字典

从 jinja2 表达式创建变量。

默认值: {}

filters

列表 / 元素=字典

在 community.hrobot 2.0.0 中添加

允许选择/取消选择此库存的主机的包含/排除筛选器列表。

筛选器按顺序处理,直到找到第一个 filters[].excludefilters[].include 匹配的筛选器。如果 filters[].exclude 匹配,则主机被排除;如果 filters[].include 匹配,则主机被包含。如果没有任何筛选器匹配,则主机被包含。

exclude

字符串

一个 Jinja2 条件。如果它与主机匹配,则该主机将被 **排除**。

只能指定 filters[].excludefilters[].include 之一。

include

字符串

一个 Jinja2 条件。如果它与主机匹配,则该主机将被 **包含**。

只能指定 filters[].excludefilters[].include 之一。

groups

字典

根据 Jinja2 条件将主机添加到组。

默认值: {}

hetzner_password

字符串 / 必需

Robot 网络服务用户的密码。

配置

hetzner_user

字符串 / 必需

Robot 网络服务用户的用户名。

配置

keyed_groups

列表 / 元素=字典

根据变量的值将主机添加到组中。

默认值:[]

default_value

字符串

在 ansible-core 2.12 中添加

当主机变量的值为空字符串时的默认值。

此选项与keyed_groups[].trailing_separator互斥。

key

字符串

用于生成组的输入字典中的键。

parent_group

字符串

键控组的父组。

prefix

字符串

键控组名称将以此前缀开头。

默认值:""

separator

字符串

用于构建键控组名称的分隔符。

默认值:"_"

trailing_separator

布尔值

在 ansible-core 2.12 中添加

将此选项设置为false 以省略主机变量的值为空字符串时keyed_groups[].separator之后的keyed_groups[].separator

此选项与keyed_groups[].default_value互斥。

选项

  • false

  • true ← (默认)

leading_separator

布尔值

在 ansible-core 2.11 中添加

keyed_groups结合使用。

默认情况下,没有提供前缀或分隔符的键控组的名称将以下划线开头。

这是因为默认前缀为"",默认分隔符为"_"

将此选项设置为false 以省略在没有提供前缀的情况下开头的下划线(或其他分隔符)。

如果组名称是从映射中导出的,则仍然使用分隔符来连接项目。

要完全不使用组名称中的分隔符,请将键控组的分隔符设置为空字符串。

选项

  • false

  • true ← (默认)

plugin

字符串 / 必需

确保这是插件源文件的令牌。

选项

  • "community.hrobot.robot"

simple_filters

字典

过滤器值对的字典。

此处列出的可用过滤器是服务器的键,例如statusserver_ip

有关可以使用的所有值,请参见https://robot.your-server.de/doc/webservice/en.html#get-server

此选项在 community.hrobot 2.0.0 之前称为filters。它已在 community.hrobotdns 1.9.0 中从filters重命名为simple_filters,旧名称在 community.hrobot 2.0.0 之前仍可用作别名。filters现在用于其他用途。

默认值: {}

strict

布尔值

如果为yes,则使无效条目成为致命错误,否则跳过并继续。

由于可以在表达式中使用事实,因此它们可能并不总是可用,因此我们默认忽略这些错误。

选项

  • false ← (默认)

  • true

use_extra_vars

布尔值

在 ansible-core 2.11 中添加

将额外的变量合并到可用于组合的变量中(最高优先级)。

选项

  • false ← (默认)

  • true

配置

注释

注意

示例

# Fetch all hosts in Hetzner Robot
plugin: community.hrobot.robot
# Filters all servers in ready state
filters:
  status: ready

# Example showing encrypted credentials and using filters
# (This assumes that Mozilla sops was used to encrypt keys/hetzner.sops.yaml, which contains two values
# hetzner_username and hetzner_password. Needs the community.sops collection to decode that file.)
plugin: community.hrobot.robot
hetzner_user: '{{ (lookup("community.sops.sops", "keys/hetzner.sops.yaml") | from_yaml).hetzner_username }}'
hetzner_password: '{{ (lookup("community.sops.sops", "keys/hetzner.sops.yaml") | from_yaml).hetzner_password }}'
filters:
  # Accept all servers in FSN1-DC1 and FSN1-DC2
  - include: >-
      hrobot_dc in ["FSN1-DC1", "FSN1-DC2"]
  # Exclude all servers that didn't match any of the above filters
  - exclude: true

# Example using constructed features to create groups
plugin: community.hrobot.robot
simple_filters:
  status: ready
  traffic: unlimited
# keyed_groups may be used to create custom groups
strict: false
keyed_groups:
  # Add e.g. groups for every data center
  - key: hrobot_dc
    separator: ""
# Use the IP address to connect to the host
compose:
  server_name_ip: hrobot_server_name ~ '-' ~ hrobot_server_ip

作者

  • Oleksandr Stepanov (@alexandrst88)

提示

每个条目类型的配置条目的优先级顺序从低到高。例如,列表中较低的变量将覆盖较高的变量。