community.windows.win_hosts 模块 – 管理 Windows 系统上的 hosts 文件条目。

注意

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

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

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

要在剧本中使用它,请指定:community.windows.win_hosts

摘要

  • 管理 Windows 系统上的 hosts 文件条目。

  • 将 IPv4 或 IPv6 地址映射到规范名称。

  • 添加、删除或设置 ip 和主机名对的 cname 记录。

  • 修改 %windir%\\system32\\drivers\\etc\\hosts。

参数

参数

注释

action

字符串

控制 _aliases_ 的行为。

仅当 state=present 时适用。

如果为 add,则 _aliases_ 中的每个别名都将添加到主机条目中。

如果为 set,则 _aliases_ 中的每个别名都将添加到主机条目中,而其他别名将从条目中删除。

选项

  • "add"

  • "remove"

  • "set" ← (默认)

aliases

列表 / 元素=字符串

主机条目的附加名称 (cname 记录) 列表。

仅当 state=present 时适用。

canonical_name

字符串

主机条目的规范名称。

对于 state=present 为必填项。

ip_address

字符串

主机条目的 IP 地址。

可以是 IPv4 (A 记录) 或 IPv6 (AAAA 记录)。

对于 state=present 为必填项。

state

字符串

条目是否存在。

如果仅在 state=absent 时提供 _canonical_name_,则将删除所有规范名称为 _canonical_name_ 的主机条目。

如果仅在 state=absent 时提供 _ip_address_,则将删除所有 IP 地址为 _ip_address_ 的主机条目。

如果在 state=absent 时同时省略 _ip_address_ 和 _canonical_name_,则将删除所有主机条目。

选项

  • "absent"

  • "present" ← (默认)

备注

注意

  • 每个规范名称只能映射到一个 IPv4 地址和一个 IPv6 地址。如果使用 state=present 提供 _canonical_name_,并且发现它映射到另一个与 _ip_address_ 类型相同但唯一不同的 IP 地址,则 _canonical_name_ 和所有 _aliases_ 将从条目中删除,并添加到具有提供的 IP 地址的条目中。

  • 每个别名只能映射到一个规范名称。如果使用 state=present 提供 _aliases_,并且发现某个别名映射到另一个规范名称,则该别名将从条目中删除,并根据 _action_ 将其添加到具有提供的规范名称的条目中或从中删除。

另请参见

另请参见

ansible.windows.win_template

将文件模板输出到远程服务器。

ansible.windows.win_file

创建、触摸或删除文件或目录。

ansible.windows.win_copy

将文件复制到 Windows 主机的远程位置。

示例

- name: Add 127.0.0.1 as an A record for localhost
  community.windows.win_hosts:
    state: present
    canonical_name: localhost
    ip_address: 127.0.0.1

- name: Add ::1 as an AAAA record for localhost
  community.windows.win_hosts:
    state: present
    canonical_name: localhost
    ip_address: '::1'

- name: Remove 'bar' and 'zed' from the list of aliases for foo (192.168.1.100)
  community.windows.win_hosts:
    state: present
    canonical_name: foo
    ip_address: 192.168.1.100
    action: remove
    aliases:
      - bar
      - zed

- name: Remove hosts entries with canonical name 'bar'
  community.windows.win_hosts:
    state: absent
    canonical_name: bar

- name: Remove 10.2.0.1 from the list of hosts
  community.windows.win_hosts:
    state: absent
    ip_address: 10.2.0.1

- name: Ensure all name resolution is handled by DNS
  community.windows.win_hosts:
    state: absent

作者

  • Micah Hunsberger (@mhunsber)