签到成功

知道了

CNDBA社区CNDBA社区

ansible控制windows 2019

2021-10-30 23:49 1336 0 原创 ansible
作者: hbhe0316

1.在ansible端安装pywinrmhttp://www.cndba.cn/hbhe0316/article/5015

[root@ansible ansible]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible ansible]# yum -y install python-pip
[root@ansible ansible]# pip install pywinrm

2.配置/etc/ansible/hosts文件,加入需要被接管的windows系统http://www.cndba.cn/hbhe0316/article/5015

[windows]
192.168.56.105 ansible_ssh_user="Administrator" ansible_ssh_pass="S@34eapt12" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

3.在windows服务器设置winrm
在windows开启winrm
winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;

http://www.cndba.cn/hbhe0316/article/5015
http://www.cndba.cn/hbhe0316/article/5015

winrm enumerate winrm/config/listener

针对winrm service 进行基础配置:

winrm quickconfig

查看winrm service listener:

http://www.cndba.cn/hbhe0316/article/5015

winrm e winrm/config/listener

为winrm service 配置auth:

winrm set winrm/config/service/auth @{Basic=“true”}

为winrm service 配置加密方式为允许非加密:http://www.cndba.cn/hbhe0316/article/5015http://www.cndba.cn/hbhe0316/article/5015http://www.cndba.cn/hbhe0316/article/5015

winrm set winrm/config/service @{AllowUnencrypted=“true”}

4.测试ansible是否可用Ping通windows

[root@ansible ansible]# ansible windows -m win_ping
192.168.56.105 | UNREACHABLE! => {
    "changed": false, 
    "msg": "plaintext: the specified credentials were rejected by the server", 
    "unreachable": true
}

5.使用ansible执行命令拷贝/etc/hosts文件至windows C盘

[root@ansible ansible]# ansible windows -m win_copy -a "src=/etc/hosts dest=c:/ owner=administrator"
192.168.56.105 | CHANGED => {
    "changed": true, 
    "checksum": "c57fa92967f442aefae1b01f907318e9f658b271", 
    "dest": "c://hosts", 
    "operation": "file_copy", 
    "original_basename": "hosts", 
    "size": 181, 
    "src": "/etc/hosts"
}

6.使用ansible获取ipconfig信息http://www.cndba.cn/hbhe0316/article/5015

[root@ansible ansible]# ansible -i hosts windows -m win_shell -a "ipconfig /all"
192.168.56.105 | CHANGED | rc=0 >>

Windows IP Configuration

   Host Name . . . . . . . . . . . . : WIN-SU8A4VM4R4L
   Primary Dns Suffix  . . . . . . . : 
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 锟斤拷太锟斤拷:

   Connection-specific DNS Suffix  . : 
   Description . . . . . . . . . . . : Intel(R) PRO/1000 MT Desktop Adapter
   Physical Address. . . . . . . . . : 08-00-27-8C-15-66
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::ad24:705f:d4ba:6eef%9(Preferred) 
   IPv4 Address. . . . . . . . . . . : 192.168.56.105(Preferred) 
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.56.1
   DHCPv6 IAID . . . . . . . . . . . : 67633191
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-29-0F-11-3B-08-00-27-8C-15-66
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

7.使用ansible获取hotname信息http://www.cndba.cn/hbhe0316/article/5015

[root@ansible ansible]# ansible -i hosts windows -m win_shell -a "hostname"
192.168.56.105 | CHANGED | rc=0 >>
WIN-SU8A4VM4R4L

版权声明:本文为博主原创文章,未经博主允许不得转载。

ansible

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1154056次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ