community.windows.win_hosts (2.2.0) — module

Manages hosts file entries on Windows.

Authors: Micah Hunsberger (@mhunsber)

Install collection

Install with ansible-galaxy collection install community.windows:==2.2.0


Add to requirements.yml

  collections:
    - name: community.windows
      version: 2.2.0

Description

Manages hosts file entries on Windows.

Maps IPv4 or IPv6 addresses to canonical names.

Adds, removes, or sets cname records for ip and hostname pairs.

Modifies %windir%\\system32\\drivers\\etc\\hosts.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add ::1 as an AAAA record for localhost
  community.windows.win_hosts:
    state: present
    canonical_name: localhost
    ip_address: '::1'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove hosts entries with canonical name 'bar'
  community.windows.win_hosts:
    state: absent
    canonical_name: bar
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove 10.2.0.1 from the list of hosts
  community.windows.win_hosts:
    state: absent
    ip_address: 10.2.0.1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Ensure all name resolution is handled by DNS
  community.windows.win_hosts:
    state: absent

Inputs

    
state:
    choices:
    - absent
    - present
    default: present
    description:
    - Whether the entry should be present or absent.
    - If only I(canonical_name) is provided when C(state=absent), then all hosts entries
      with the canonical name of I(canonical_name) will be removed.
    - If only I(ip_address) is provided when C(state=absent), then all hosts entries with
      the ip address of I(ip_address) will be removed.
    - If I(ip_address) and I(canonical_name) are both omitted when C(state=absent), then
      all hosts entries will be removed.
    type: str

action:
    choices:
    - add
    - remove
    - set
    default: set
    description:
    - Controls the behavior of I(aliases).
    - Only applicable when C(state=present).
    - If C(add), each alias in I(aliases) will be added to the host entry.
    - If C(set), each alias in I(aliases) will be added to the host entry, and other aliases
      will be removed from the entry.
    type: str

aliases:
    description:
    - A list of additional names (cname records) for the host entry.
    - Only applicable when C(state=present).
    elements: str
    type: list

ip_address:
    description:
    - The ip address for the host entry.
    - Can be either IPv4 (A record) or IPv6 (AAAA record).
    - Required for C(state=present).
    type: str

canonical_name:
    description:
    - A canonical name for the host entry.
    - required for C(state=present).
    type: str

See also