doina

一个小菜鸟运维工程师.

winrm or requests is not installed: No module named winrm

ansible管理windows主机

ansible管理windows服务器,开启windows的winrm功能后,
具体配置方法百度搜索一哈,一搜一大把…

配置ansible

到ansible端配置hosts文件,添加windows主机,格式如下:

[windows]
10.241.0.4

[windows:vars]
ansible_ssh_user='Administrator'
ansible_ssh_pass='Password'
ansible_ssh_port=5985
ansible_connection='winrm'
ansible_winrm_server_cert_validation=ignore

测试windows主机

报错如下, 百度搜了半天没有搜索到,最终在股沟发现一篇文章,地址:
https://access.redhat.com/solutions/3356681

[root@squid ~]# ansible windows -m win_ping
10.241.0.4 | FAILED! => {
    "msg": "winrm or requests is not installed: No module named winrm"
}

安装pip

#下载安装包
[root@squid ~]# wget http://download.baiyongjie.com/python/pip/pip-8.1.0.tar.gz
[root@squid ~]# wget http://download.baiyongjie.com/python/pip/setuptools-33.1.1.zip

#解压安装包
[root@squid ~]# unzip setuptools-33.1.1.zip
[root@squid ~]# tar zxvf pip-8.1.0.tar.gz

#安装setuptools
[root@squid ~]# cd setuptools-33.1.1
[root@squid ~]# python2.7  setup.py  install

#安装pip
[root@squid ~]# cd ../
[root@squid ~]# cd pip-8.1.0
[root@squid ~]# python2.7 setup.py install
[root@squid ~]# pip2.7 install --upgrade pip

[root@squid ~]# pip2.7  --version
pip 18.0 from /usr/lib/python2.7/site-packages/pip (python 2.7)

安装pywinrm模块

[root@squid ~]# pip2 install "pywinrm>=0.2.2"
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pywinrm>=0.2.2
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/0d/12/13a3117bbd2230043aa32dcfa2198c33269665eaa1a8fa26174ce49b338f/pywinrm-0.3.0-py2.py3-none-any.whl
Requirement already satisfied: six in /usr/lib/python2.7/site-packages (from pywinrm>=0.2.2) (1.9.0)
Requirement already satisfied: requests-ntlm>=0.3.0 in /usr/lib/python2.7/site-packages (from pywinrm>=0.2.2) (1.1.0)
Requirement already satisfied: requests>=2.9.1 in /usr/lib/python2.7/site-packages (from pywinrm>=0.2.2) (2.19.1)
Requirement already satisfied: xmltodict in /usr/lib/python2.7/site-packages (from pywinrm>=0.2.2) (0.11.0)
Requirement already satisfied: cryptography>=1.3 in /usr/lib64/python2.7/site-packages (from requests-ntlm>=0.3.0->pywinrm>=0.2.2) (1.7.2)
Requirement already satisfied: ntlm-auth>=1.0.2 in /usr/lib/python2.7/site-packages (from requests-ntlm>=0.3.0->pywinrm>=0.2.2) (1.2.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python2.7/site-packages (from requests>=2.9.1->pywinrm>=0.2.2) (2018.8.13)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python2.7/site-packages (from requests>=2.9.1->pywinrm>=0.2.2) (3.0.4)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /usr/lib/python2.7/site-packages (from requests>=2.9.1->pywinrm>=0.2.2) (1.23)
Requirement already satisfied: idna<2.8,>=2.5 in /usr/lib/python2.7/site-packages (from requests>=2.9.1->pywinrm>=0.2.2) (2.7)
Requirement already satisfied: pyasn1>=0.1.8 in /usr/lib/python2.7/site-packages (from cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm>=0.2.2) (0.1.9)
Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages/setuptools-33.1.1-py2.7.egg (from cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm>=0.2.2) (33.1.1)
Requirement already satisfied: enum34 in /usr/lib/python2.7/site-packages (from cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm>=0.2.2) (1.0.4)
Requirement already satisfied: ipaddress in /usr/lib/python2.7/site-packages (from cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm>=0.2.2) (1.0.16)
Requirement already satisfied: cffi>=1.4.1 in /usr/lib64/python2.7/site-packages (from cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm>=0.2.2) (1.6.0)
Requirement already satisfied: pycparser in /usr/lib/python2.7/site-packages (from cffi>=1.4.1->cryptography>=1.3->requests-ntlm>=0.3.0->pywinrm>=0.2.2) (2.14)
Installing collected packages: pywinrm
Successfully installed pywinrm-0.3.0

安装后测试正常

[root@squid ~]# ansible windows -m win_ping  
10.241.0.4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
点赞
  1. Raj Rajeshwar Singh Rathore说道:

    Thanks, the solution worked.

    1. baiyongjie说道:

      I'm glad I helped you

  2. 匿名说道:

    I followed all the steps.
    But, still the error persists.

  3. 匿名说道:

    I am getting some errors.
    The process is not getting completed

  4. rex说道:

    兄弟,我专门来感谢你的,这个问题困扰了我3天,终于解决!

  5. 匿名说道:

    that work for me

发表评论

邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据