admin 管理员组

文章数量: 1184232

目录标题

  • 一.环境信息
  • 二.Windows主机要求
  • 三.部署
    • 1.windows
    • 2.Ansible管理主机(centos7.9)
      • **注意:**
      • (1)安装pip
      • (2)安装ansible
      • (3)安装pywinrm插件
      • (4)ansible-hosts文件(inventory清单)

一.环境信息

Ansbile
操作系统:Centos 7.9
Windows
操作系统:Windows Server 2016

二.Windows主机要求

要使 Ansible 与 Windows 主机通信并使用 Windows 模块,Windows 主机必须满足以下要求:

  • Ansible 通常可以在 Microsoft 的当前和扩展支持下管理 Windows 版本。Ansible 可以管理包括 Windows 7、8.1 和 10 在内的桌面操作系统,以及包括 Windows Server 2008、2008 R2、2012、2012 R2、2016 和 2019 在内的服务器操作系统。

  • Ansible 要求在 Windows 主机上安装 PowerShell 3.0 或更高版本,并且至少要安装 .NET 4.0。

powershell 5.1安装包:
https://download.csdn/download/weixin_46031767/85017181

查看powerhsell版本

可以使用 $PSVersionTable.PSVersion 来查看版本,Major 5就是powershell的主版本

三.部署

1.windows

Server2008R2中默认开启该服务,从Server2012开始,该服务便集成在系统中默认开启,Win7默认安装此服务,但是默认为禁用状态,Win8,Win10默认开启。这种远程连接不会被客户端察觉到,也不会占用远程连接数!

winrm使用端口:http 5985 https 5986

开启远程管理服务winrm(**powershell**中运行命令)
①.查看powershell执行策略
    get-executionpolicy
②.更改powershell执行策略为remotesigned
    set-executionpolicy remotesigned
③.配置winrm service并启动服务
	winrm quickconfig
④.修改winrm配置,启用远程连接认证
	将client中的基本身份验证设置为true,允许  
    winrm set winrm/config/client/auth '@{Basic="true"}'
    将service中的allowUnencrypted设置为true,允许未加密的通讯  
    winrm set winrm/config/service '@{AllowUnencrypted="true"}'      

2.Ansible管理主机(centos7.9)

注意:

ansible-playbook去运行yml脚本也需要pywinrm插件,命令行运行ansible命令也需要,但用postman给awx对应windows模板传参数不需要pywinrm插件
千万不要用yum安装。选择pip安装,或者二进制包安装。否则,即便安装了pywinrm插件也无法管理Windows主机。会报下面错误

1.如果安装了pywinrm还是报这个错,是因为yum安装的ansible无法调用pip安装的pywinrm插件,故而建议用pip安装ansible或者用源码包安装ansible

2.一定要安装pywinrm插件,否则管理Windows Server的时候会报错下面错误
“msg”: “winrm or requests is not installed: No module named winrm”(未安装pywinrm插件)

(1)安装pip

先安装python3
博客地址:https://editor.csdn/md/?articleId=122458220
官网地址:https://www.python/ftp/python/

# 1.上传pip包或从官网下载pip包到本地
#官网链接:https://pypi/project/pip/#files
[root@localhost ~]# wget https://files.pythonhosted/packages/52/e1/06c018197d8151383f66ebf6979d951995cf495629fc54149491f5d157d0/pip-21.2.4.tar.gz

# 2.解压缩
[root@localhost ~]# tar -xvf py3-pip-21.2.4.tar.gz -C /usr/local/

# 3.安装
[root@localhost ~]# cd /usr/local/pip-21.2.4
[root@localhost pip-21.2.4]# python3 setup.py install

# 4.检查
[root@awx pip-21.2.4]# pip --version
pip 21.2.4 from /usr/local/python3.6/lib/python3.6/site-packages/pip-21.2.4-py3.6.egg/pip (python 3.6)

(2)安装ansible

pip安装的ansible需要自己创建ansible.cfg文件

[root@localhost ~]# pip install ansbile -i https://pypi.tuna.tsinghua.edu/simple/ --trusted-host pypi.tuna.tsinghua.edu

(3)安装pywinrm插件

报下面错误需要安装pywinrm插件

 [root@awx windows]# ansible -i inventory windows -m ping
[WARNING]: Unhandled error in Python interpreter discovery for host 192.168.6.155: winrm or requests is not installed: No module named 'winrm'
192.168.6.155 | FAILED! => {
    "msg": "winrm or requests is not installed: No module named 'winrm'"
}

使用国内的源,下载会更快

[root@awx ~]# pip install pywinrm -i https://pypi.tuna.tsinghua.edu/simple/ --trusted-host pypi.tuna.tsinghua.edu
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting pywinrm
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2c/54/6bfa7eb7986b3c8afe93e95ce7369d13447f65eee893129b1cf1490065de/pywinrm-0.4.2-py2.py3-none-any.whl (44 kB)
     |████████████████████████████████| 44 kB 214 kB/s 
Collecting xmltodict
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/fd/30d5c1d3ac29ce229f6bdc40bbc20b28f716e8b363140c26eff19122d8a5/xmltodict-0.12.0-py2.py3-none-any.whl (9.2 kB)
Collecting requests-ntlm>=0.3.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/03/4b/8b9a1afde8072c4d5710d9fa91433d504325821b038e00237dc8d6d833dc/requests_ntlm-1.1.0-py2.py3-none-any.whl (5.7 kB)
Collecting six
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting requests>=2.9.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2d/61/08076519c80041bc0ffa1a8af0cbd3bf3e2b62af10435d269a9d0f40564d/requests-2.27.1-py2.py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 215 kB/s 
Collecting certifi>=2017.4.17
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/37/45/946c02767aabb873146011e665728b680884cd8fe70dde973c640e45b775/certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
     |████████████████████████████████| 149 kB 1.4 MB/s 
Collecting idna<4,>=2.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/04/a2/d918dcd22354d8958fe113e1a3630137e0fc8b44859ade3063982eacd2a4/idna-3.3-py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 1.4 MB/s 
Collecting charset-normalizer~=2.0.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/84/3e/1037abe6498e65d645ce7a22d3402605d49a3b2c7f20c3abb027760da4f0/charset_normalizer-2.0.10-py3-none-any.whl (39 kB)
Collecting urllib3<1.27,>=1.21.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4e/b8/f5a25b22e803f0578e668daa33ba3701bb37858ec80e08a150bd7d2cf1b1/urllib3-1.26.8-py2.py3-none-any.whl (138 kB)
     |████████████████████████████████| 138 kB 1.4 MB/s 
Requirement already satisfied: cryptography>=1.3 in /usr/local/python3.6/lib/python3.6/site-packages (from requests-ntlm>=0.3.0->pywinrm) (36.0.1)
Collecting ntlm-auth>=1.0.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ff/84/97c550164b54942b0e908c31ef09d9469f3ba4cd7332a671e2125732f63b/ntlm_auth-1.5.0-py2.py3-none-any.whl (29 kB)
Requirement already satisfied: cffi>=1.12 in /usr/local/python3.6/lib/python3.6/site-packages (from cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm) (1.15.0)
Requirement already satisfied: pycparser in /usr/local/python3.6/lib/python3.6/site-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm) (2.21)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests, ntlm-auth, xmltodict, six, requests-ntlm, pywinrm
Successfully installed certifi-2021.10.8 charset-normalizer-2.0.10 idna-3.3 ntlm-auth-1.5.0 pywinrm-0.4.2 requests-2.27.1 requests-ntlm-1.1.0 six-1.16.0 urllib3-1.26.8 xmltodict-0.12.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

(4)ansible-hosts文件(inventory清单)

[root@awx windows]# cat inventory 
[windows]
192.168.6.155

[windows:vars]
ansible_ssh_user=administrator
ansible_ssh_pass="redhat@123"
ansible_ssh_port=5985
ansible_connection="winrm" 
ansible_winrm_server_cert_validation=ignore

(5)验证

[root@awx windows]# ansible -i inventory windows -m win_ping
192.168.6.155 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

本文标签: 机器 ansible Windows