首页 > 解决方案 > Ansible - 有没有办法使用 Ansible 更改 Windows Server IP 地址?

问题描述

我想使用 Ansible 设置一个新的域控制器。我有一个从模板克隆新服务器的脚本,现在如何使用 Ansible 静态设置 Windows Server 的 IP 地址?

标签: ansible

解决方案


花了好几个小时寻找同一个问题的答案:) 希望下面的帮助:

- 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

推荐阅读