-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdhcp-static.yaml
More file actions
47 lines (41 loc) · 1.3 KB
/
dhcp-static.yaml
File metadata and controls
47 lines (41 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
- name: Convert DHCP IP to Static on Windows
hosts: win-mig
gather_facts: no
collections:
- ansible.windows
vars:
interface_name: "Ethernet"
tasks:
- name: Get current network info from DHCP
ansible.windows.win_shell: |
$nic = Get-NetIPConfiguration -InterfaceAlias "{{ interface_name }}"
$obj = New-Object PSObject -Property @{
IP = $nic.IPv4Address.IPAddress
Mask = $nic.IPv4Address.PrefixLength
Gateway = $nic.IPv4DefaultGateway.NextHop
DNS = ($nic.DnsServer.ServerAddresses -join ",")
}
$obj | ConvertTo-Json
register: netinfo
args:
executable: powershell
- name: Convert JSON to dict
set_fact:
ipinfo: "{{ netinfo.stdout | from_json }}"
- name: Show detected DHCP details
debug:
msg:
- "DHCP IP: {{ ipinfo.IP }}"
- "Mask Prefix: {{ ipinfo.Mask }}"
- "Gateway: {{ ipinfo.Gateway }}"
- "DNS: {{ ipinfo.DNS }}"
- name: Convert DHCP IP to Static
ansible.windows.win_shell: |
New-NetIPAddress `
-InterfaceAlias "Ethernet" `
-IPAddress "{{ ipinfo.IP }}" `
-PrefixLength {{ ipinfo.Mask }} `
-DefaultGateway "{{ ipinfo.Gateway }}"
args:
executable: powershell