ansible控制windows机器文档

一、ansible管理主机必须使用linux服务器

a. 首先linux服务器需要安装ansible
b. centos服务器可以使用yum安装

yum install ansible

c. 管理机必须为Linux系统且需预安装Python Winrm模块

pip install http://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm

至此linux服务器配置完毕。

二、配置需要管理的windows服务器

Ansible 从1.7+版本开始支持Windows,远程主机的通信方式也由SSH变更为PowerShell,同时管理机必须预安装Python的Winrm模块,方可和远程Windows主机正常通信。

注意:(Windows机器需要安装或升级powershell4.0以上版本,Server2008R2默认的版本是2.0,因此必须升级至4.0版本。对于Server2012及以上的版本默认是4.0版本,不需要升级,升级powershell需要重新服务器才能生效。)

a. 检查powershell版本

  1. 查看powershell版本
  2. 打开运行--->输入powershell启动powershell
  3. 在powershell终端上执行get-host命令可以查powershell版本

b. Windows客户端配置winrm,启用powershell远程管理

  1. 查看powershell执行策略

    get-executionpolicy
    
  2. 更改powershell执行策略为remotesigned

    set-executionpolicy remotesigned
    
  3. 配置winrm service并启动服务

    winrm quickconfig
    
  4. 查看winrm service启动监听状态

    winrm enumerate winrm/config/listener
    
  5. 修改winrm配置,启用远程连接认证

    为winrm service 配置auth:

    winrm set winrm/config/service/auth '@{Basic="true"}'
    

    为winrm service 配置加密方式为允许非加密:

    winrm set winrm/config/service '@{AllowUnencrypted="true"}'
    

三、调试

 ansible 172.16.10.23 -m win_ping

通过此条命令可检查连接是否通畅

上图是我在linux服务器ansible配置模块信息
主要模块配置site.yaml

- hosts: all
  tasks:
    - name: Move file on remote Windows Server from one location to another
      win_copy:
        src: /opt/ansible-win/test
        dest: C:\Users\Administrator\Desktop\test\

注意配置文件格式需要对齐。上面是可以将文件复制到windows服务器的配置.

hosts配置文件如下:

10.0.17.** ansible_ssh_user="administrator" ansible_ssh_pass="passwd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

启动命令

./run.sh

run.sh配置

#!/usr/bin/env bash
# filename: run.sh
ansible-playbook -i hosts site.yaml
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容