ansible.windows.win_dhcp_lease module – Manage Windows Server DHCP Leases

Note

This module is part of the ansible.windows collection (version 2.5.0).

It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install ansible.windows. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: ansible.windows.win_dhcp_lease.

New in ansible.windows 2.6.0

Synopsis

  • Manage Windows Server DHCP Leases (IPv4 Only)

  • Adds, Removes and Modifies DHCP Leases and Reservations

  • Task should be delegated to a Windows DHCP Server

Requirements

The below requirements are needed on the host that executes this module.

  • This module requires Windows Server 2012 or Newer

Parameters

Parameter

Comments

description

string

Specifies the description for reservation being created.

Only applicable to l(type=reservation).

dns_hostname

string

Specifies the DNS hostname of the client for which the IP address lease is to be added.

dns_regtype

string

Indicates the type of DNS record to be registered by the DHCP. server service for this lease.

l(a) results in an A record being registered.

l(aptr) results in both A and PTR records to be registered.

l(noreg) results in no DNS records being registered.

Choices:

  • "aptr" ← (default)

  • "a"

  • "noreg"

duration

integer

Specifies the duration of the DHCP lease in days.

The duration value only applies to l(type=lease).

Defaults to the duration specified by the DHCP server configuration.

Only applicable to l(type=lease).

ip

string

The IPv4 address of the client server/computer.

This is a required parameter, if l(mac) is not set.

Can be used to identify an existing lease/reservation, instead of l(mac).

mac

string

Specifies the client identifier to be set on the IPv4 address.

This is a required parameter, if l(ip) is not set.

Windows clients use the MAC address as the client ID.

Linux and other operating systems can use other types of identifiers.

Can be used to identify an existing lease/reservation, instead of l(ip).

reservation_name

string

Specifies the name of the reservation being created.

Only applicable to l(type=reservation).

scope_id

string

Specifies the scope identifier as defined by the DHCP server.

This is a required parameter, if l(state=present) and the reservation or lease doesn’t already exist. Not required if updating an existing lease or reservation.

state

string

Specifies the desired state of the DHCP lease or reservation.

Choices:

  • "present" ← (default)

  • "absent"

type

string

The type of DHCP address.

Leases expire as defined by l(duration).

When l(duration) is not specified, the server default is used.

Reservations are permanent.

Choices:

  • "reservation" ← (default)

  • "lease"

Examples

- name: Ensure DHCP reservation exists
  ansible.windows.win_dhcp_lease:
    type: reservation
    ip: 192.168.100.205
    scope_id: 192.168.100.0
    mac: 00:B1:8A:D1:5A:1F
    dns_hostname: "{{ ansible_inventory }}"
    description: Testing Server

- name: Ensure DHCP lease or reservation does not exist
  ansible.windows.win_dhcp_lease:
    mac: 00:B1:8A:D1:5A:1F
    state: absent

- name: Ensure DHCP lease or reservation does not exist
  ansible.windows.win_dhcp_lease:
    ip: 192.168.100.205
    state: absent

- name: Convert DHCP lease to reservation & update description
  ansible.windows.win_dhcp_lease:
    type: reservation
    ip: 192.168.100.205
    description: Testing Server

- name: Convert DHCP reservation to lease
  ansible.windows.win_dhcp_lease:
    type: lease
    ip: 192.168.100.205

# Modify an existing lease by running the following tasks
- name: Remove old lease
  win_dhcp_lease:
    mac: "00:11:22:33:44:55"
    state: absent

- name: Create new lease with updated properties
  win_dhcp_lease:
    mac: "00:11:22:33:44:55"
    ip: "192.168.100.100"
    duration: 14
    state: present

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

lease

dictionary

New/Updated DHCP object parameters

Returned: When l(state=present)

Sample: {"address_state": "InactiveReservation", "client_id": "0a-0b-0c-04-05-aa", "description": "Really Fancy", "ip_address": "172.16.98.230", "name": null, "scope_id": "172.16.98.0"}

Authors

  • Joe Zollo (@joezollo)