要使用Ansible连接到Windows机器,需要进行以下步骤:
安装所需软件:
配置Ansible控制节点:
在Ansible控制节点上,打开ansible.cfg
配置文件并添加以下内容:
[defaults]
transport = winrm
在inventory
文件中,添加Windows机器的主机信息:
[windows]
windows_machine ansible_host=
配置Windows机器:
winrm quickconfig
确保WinRM服务已启动并运行。测试连接:
ansible windows -m win_ping
如果返回SUCCESS
,则表示连接成功。代码示例: 以下是一个使用Ansible连接到Windows机器的简单Playbook示例:
---
- name: Connect to Windows machine
hosts: windows
gather_facts: no
tasks:
- name: Run a command on Windows
win_command: ipconfig /all
register: output
- name: Print command output
debug:
var: output.stdout_lines
在这个示例中,我们使用win_command
模块在Windows机器上运行ipconfig /all
命令,并将输出保存在output
变量中。然后,使用debug
模块打印输出。
执行示例Playbook的命令为:
ansible-playbook -i inventory playbook.yml
这样就可以连接到Windows机器并执行命令。
上一篇:Ansible连接到AKS集群