ansible

Ansible - Is there a way to change a Windows Server IP address using Ansible?


I want to use Ansible to setup a new domain controller. I have a script to clone a new server from a template, now how do I set the IP address of the Windows Server statically using Ansible?


Solution

  • Spent a good few hours finding the answer to the same question :) Hope below helps:

    - name: Set up static IP address
      win_shell: "Get-NetIpAddress -InterfaceAlias 'Ethernet' | New-NetIpAddress -IpAddress 192.168.1.120 -PrefixLength 24 -DefaultGateway 192.168.1.1"
      async: 100 # Using "fire-and-forget" asynchronous execution for this task, otherwise it will always fail and timeout
      poll: 0
    
    - name: Change ansible's ip address for each host
      set_fact:
        ansible_host: "192.168.1.120"
    
    - name: Wait for the hosts network interface to come back up
      local_action:
        module: wait_for
        host: "{{ ansible_host }}"
        port: 5985
        delay: 10
        state: started
      register: wait_result
    
    - name: Create a test folder to celebrate success
      win_file:
        path: C:\itworks
        state: directory