-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwin-mig.yaml
More file actions
58 lines (50 loc) · 1.75 KB
/
win-mig.yaml
File metadata and controls
58 lines (50 loc) · 1.75 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
47
48
49
50
51
52
53
54
55
56
57
- hosts: win-mig
gather_facts: no
collections:
- ansible.windows
tasks:
# 1. Detect active interface automatically
- name: Detect actual interface
ansible.windows.win_shell: |
(Get-NetIPConfiguration | Where-Object {$_.IPv4Address}).InterfaceAlias
register: iface
args:
executable: powershell
- name: Set interface fact
set_fact:
interface_name: "{{ iface.stdout_lines[0] | trim }}"
# 2. Get current DHCP information
- name: Get current network info from DHCP
ansible.windows.win_shell: |
$nic = Get-NetIPConfiguration -InterfaceAlias "{{ interface_name }}"
$obj = [PSCustomObject]@{
IP = $nic.IPv4Address.IPAddress
Mask = $nic.IPv4Address.PrefixLength
Gateway = $nic.IPv4DefaultGateway.NextHop
DNS = ($nic.DnsServer.ServerAddresses -join ",")
}
$obj | ConvertTo-Json -Depth 4
register: netinfo
args:
executable: powershell
- name: Convert JSON to dict
set_fact:
ipinfo: "{{ netinfo.stdout | trim | from_json }}"
# 5. Apply static IP
- name: Apply static IP
ansible.windows.win_shell: |
New-NetIPAddress `
-InterfaceAlias "{{ interface_name }}" `
-IPAddress "{{ ipinfo.IP }}" `
-PrefixLength {{ ipinfo.Mask }} `
-DefaultGateway "{{ ipinfo.Gateway }}"
args:
executable: powershell
# Override DHCP DNS → your custom DNS
- name: Set static DNS servers (192.168.1.250, 8.8.8.8)
ansible.windows.win_shell: |
Set-DnsClientServerAddress `
-InterfaceAlias "{{ interface_name }}" `
-ServerAddresses @("192.168.1.250","8.8.8.8")
args:
executable: powershell