HostTech DNS 指南

community.dns 集合 提供了多个模块用于处理 HostTech DNS 服务。这些模块同时支持旧的 基于 WSDL 的 API 和新的 基于 JSON 的 REST API

该集合提供了六个模块用于处理 HostTech DNS

它还提供了一个清单插件

身份验证、要求和 API

HostTech 目前有两个用于处理 DNS 记录的 API:旧的基于 WSDL 的 API 和新的基于 JSON 的 REST API。如果可能,我们建议使用新的 JSON REST API。

JSON REST API

要使用 JSON REST API,您需要创建一个 API 令牌。您可以在“DNS 编辑器”的“API”部分管理 API 令牌。您必须将令牌提供给模块的 hosttech_token 选项

- community.dns.hosttech_dns_record:
    hosttech_token: '{{ token }}'
    ...

在本指南中的示例中,我们将省略身份验证选项。请注意,您可以使用 module_defaults 在全局范围内设置它们(参见 模块默认值)。

WSDL API

要使用 WSDL API,您需要设置 API 凭据。这些凭据可以在“Servercenter”中的“解决方案”部分下的“DNS 工具”设置中找到和更改。用户名是固定的,但密码可以更改。必须将凭据提供给模块的 hosttech_usernamehosttech_password 选项。

您还需要安装 lxml Python 模块 才能使用 WSDL API。这可以在使用模块之前完成。

- name: Make sure lxml is installed
  pip:
    name: lxml

- community.dns.hosttech_dns_record:
    hosttech_username: '{{ username }}'
    hosttech_password: '{{ password }}'
    ...

在本指南中的示例中,我们将省略身份验证选项。请注意,您可以使用 module_defaults 在全局范围内设置它们(参见 模块默认值)。

使用 community.dns.hosttech 模块默认组

为了避免在每个任务中都为所有 Hosttech DNS 模块指定常用参数,您可以使用 community.dns.hosttech 模块默认组。

---
- name: Hosttech DNS
  hosts: localhost
  gather_facts: false
  module_defaults:
    group/community.dns.hosttech
      hosttech_username: '{{ username }}'
      hosttech_password: '{{ password }}'
  tasks:
    - name: Query zone information
      community.dns.hosttech_dns_zone_info:
        zone_name: example.com
      register: result

    - name: Set A records for www.example.com
      community.dns.hosttech_dns_record_set:
        state: present
        zone_name: example.com
        type: A
        prefix: www
        value:
          - 192.168.0.1

所有两个任务都将使用为模块默认组设置的选项。

使用 DNS 区域

community.dns.hosttech_dns_zone_info 模块 允许查询区域信息。区域可以通过其名称和 ID(整数)来标识。

- name: Query zone information by name
  community.dns.hosttech_dns_zone_info:
    zone_name: example.com
  register: result

- name: Query zone information by ID
  community.dns.hosttech_dns_zone_info:
    zone_id: 42
  register: result

该模块返回区域名称和区域 ID,因此该模块可用于在区域 ID 和区域名称之间进行转换。

- ansible.builtin.debug:
    msg: |
        The zone ID: {{ result.zone_id }}
        The zone name: {{ result.zone_name }}

使用 DNS 记录

注意

默认情况下,此集合中的模块和插件返回和接受的 TXT 记录值是不带引号的。这意味着您不必添加双引号 ("),也不必转义双引号(如 \")和反斜杠(如 \\)。所有与 DNS 记录一起工作的模块和插件都支持 txt_transformation 选项,该选项允许配置此行为。

查询 DNS 记录和记录集

community.dns.hosttech_dns_record_set_info 模块 允许从 API 查询 DNS 记录集。它可以用来查询单个记录集。

- name: Query single record
  community.dns.hosttech_dns_record_set_info:
    zone_name: example.com
    type: A  # IPv4 addresses
    what: single_record  # default value
    # Either specify a record name:
    record: www.example.com
    # Or a record prefix ('' is the zone itself):
    prefix: www
  register: result

- name: Show IPv4 addresses if record exists
  ansible.builtin.debug:
    msg: >
      IPv4s are {{ result.set.value | join(', ') }},
      TTL is {{ result.set.ttl }}
  when: result.set

- name: Show that record is not set
  ansible.builtin.debug:
    msg: There is no A record for www.example.com
  when: not result.set

在本节的所有示例中,您可以将 zone_name=example.com 替换为 zone_id=42,其中 zone_id 是区域的整数 ID。

您还可以查询记录名称或前缀的所有记录集列表。

- name: Query all records for www.example.com
  community.dns.hosttech_dns_record_set_info:
    zone_name: example.com
    what: all_types_for_record
    # Either specify a record name:
    record: www.example.com
    # Or a record prefix ('' is the zone itself):
    prefix: www
  register: result

- name: Show all records for www.example.com
  ansible.builtin.debug:
    msg: >
      {{ item.type }} record with TTL {{ item.ttl }} has
      values {{ item.value | join(', ') }}
  loop: result.sets

最后,您可以查询区域的所有记录集。

- name: Query all records for a zone
  community.dns.hosttech_dns_record_set_info:
    zone_name: example.com
    what: all_records
  register: result

- name: Show all records for the example.com zone
  ansible.builtin.debug:
    msg: >
      {{ item.type }} record for {{ item.record }} with
      TTL {{ item.ttl }} has values {{ item.value | join(', ') }}
  loop: result.sets

如果您对单个 DNS 记录而不是记录集感兴趣,则应使用 community.dns.hosttech_dns_record_info 模块。它支持与 community.dns.hosttech_dns_record_set_info 模块 相同的限制选项。

创建和更新单个 DNS 记录

如果您不想添加/删除值,而是替换值,您将会对修改**记录集**而不是单个记录感兴趣。这在处理CNAMESOA记录时尤其重要。

community.dns.hosttech_dns_record 模块允许设置、更新和删除单个DNS记录。设置和更新可以按如下方式进行。记录将按记录名称和类型匹配,TTL值如有必要将被更新。

- name: Add an A record with value 1.1.1.1 for www.example.com, resp. make sure the TTL is 300
  community.dns.hosttech_dns_record:
    state: present
    zone_name: example.com
    type: A  # IPv4 addresses
    # Either specify a record name:
    record: www.example.com
    # Or a record prefix ('' is the zone itself):
    prefix: www
    value: 1.1.1.1
    ttl: 300

要删除记录,只需使用state=absent。记录将按记录名称和类型匹配,TTL将被忽略。

- name: Remove A values for www.example.com
  community.dns.hosttech_dns_record:
    state: absent
    zone_name: example.com
    type: A  # IPv4 addresses
    record: www.example.com
    value: 1.1.1.1

对于相同记录名称,具有其他值的相同类型记录将被忽略。

创建和更新DNS记录集

community.dns.hosttech_dns_record_set 模块允许设置、更新和删除DNS记录集。设置和更新可以按如下方式进行。

- name: Make sure record is set to the given value
  community.dns.hosttech_dns_record_set:
    state: present
    zone_name: example.com
    type: A  # IPv4 addresses
    # Either specify a record name:
    record: www.example.com
    # Or a record prefix ('' is the zone itself):
    prefix: www
    value:
      - 1.1.1.1
      - 8.8.8.8

如果您想确认记录具有某个特定值,请设置on_existing=keep。使用keep_and_warn代替将发出警告,而keep_and_fail将使模块失败。

要删除值,您可以使用值[]覆盖值,或者使用state=absent

- name: Remove A values for www.example.com
  community.dns.hosttech_dns_record_set:
    state: present
    zone_name: example.com
    type: A  # IPv4 addresses
    record: www.example.com
    value: []

- name: Remove TXT values for www.example.com
  community.dns.hosttech_dns_record_set:
    zone_name: example.com
    type: TXT
    prefix: www
    state: absent

- name: Remove specific AAAA values for www.example.com
  community.dns.hosttech_dns_record_set:
    zone_name: example.com
    type: AAAA  # IPv6 addresses
    prefix: www
    state: absent
    on_existing: keep_and_fail
    ttl: 300
    value:
      - '::1'

在第三个示例中,on_existing=keep_and_fail存在,并给出了显式值和TTL。这使得模块仅在www.example.com的AAAA记录的当前值为::1且TTL为300时才删除当前值。如果设置了其他值,模块将不会进行任何更改,但会失败。这对于避免意外删除不想更改的值很有用。要发出警告而不是失败,请使用on_existing=keep_and_warn,而要简单地不做任何更改,并且没有任何指示,请使用on_existing=keep

DNS记录集的批量同步

如果您想一次设置/更新多个记录,甚至确保您提供的精确记录集存在且仅此而已,您可以使用community.dns.hosttech_dns_record_sets 模块

以下示例显示如何一次设置/更新多个记录。

- name: Make sure that multiple records are present
  community.dns.hosttech_dns_record_sets:
    zone_name: example.com
    record_sets:
      - prefix: www
        type: A
        value:
          - 1.1.1.1
          - 8.8.8.8
      - prefix: www
        type: AAAA
        value:
          - '::1'

下一个示例显示如何确保只有给定的记录可用,而所有其他记录都被删除。请注意,对于record_sets[].type=NS记录,我们使用了record_sets[].ignore=true,这允许我们跳过该值。它告诉模块它不应该触碰example.comNS记录。

- name: Make sure that multiple records are present
  community.dns.hosttech_dns_record_sets:
    zone_name: example.com
    prune: true
    record_sets:
      - prefix: www
        type: A
        value:
          - 1.1.1.1
          - 8.8.8.8
      - prefix: www
        type: AAAA
        value:
          - '::1'
      - prefix: ''
        type: NS
        ignore: true