Ansible window通信安装(转载)

简介: 转载:http://blog.51cto.com/mengix/1862959一、必须安装在ansible的Linux管控主机上安装控制Windows的组件  1.

转载:http://blog.51cto.com/mengix/1862959

一、必须安装在ansible的Linux管控主机上安装控制Windows的组件

  1.安装pywinrm,kerberos

二、配置Windows主机

  1.安装安装Framework 4.5(最低3.0)

  2.修改注册列表:设置powershell本地脚本运行权限为remotesigned

  2.升级到powershell-3.0

  3.配置winrm

三、功能测试

下面开始...

一、配置ansible管控机器

使用Python的pip安装pywinrm及kerberos

$ sudopip installhttp://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm

$ sudopip installkerberos

在安装kerberos之前需要安装

1$ sudoapt-get installlibkrb5-dev

否则报错为

$ sudopip installkerberos

running build_ext

building 'kerberos'extension

creating build

creating build/temp.linux-x86_64-2.7

creating build/temp.linux-x86_64-2.7/src

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c src/base64.c -o build/temp.linux-x86_64-2.7/src/base64.o sh: 1: krb5-config: not found

gcc: error: sh:: No such fileor directory

gcc: error: 1:: No such fileor directory

gcc: error: krb5-config:: No such fileor directory

gcc: error: not: No such fileor directory

gcc: error: found: No such fileor directory

error: command'gcc'failed with exitstatus 1

----------------------------------------

Command /usr/bin/python-c "import setuptools;__file__='/etc/ansible/scripts/build/kerberos/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))"install--single-version-externally-managed --record /tmp/pip-Q3eBF1-record/install-record.txt failed with error code 1

Storing complete log in/home/ansible/.pip/pip.log

二、配置windows主机

查看系统中.Net版本及powershell版本

.Net版本为CLRVersion;powershell使用$host中Version

PS C:\Users\ABC> $psversiontable

Name                           Value

----                           -----

CLRVersion                       2.0.50727.5485

BuildVersion                      6.1.7601.17514

PSVersion                        2.0

WSManStackVersion                    2.0

PSCompatibleVersions                  1.0, 2.0}

SerializationVersion                  1.1.0.1

PSRemotingProtocolVersion                2.1


PS C:\Users\ABC> $host



Name               : ConsoleHost

Version              : 2.0

InstanceId            : 586e26ae-8630-4add-aa4c-d864726f5384

UI                : System.Management.Automation.Internal.Host.InternalHostUserInterface

CurrentCulture          : zh-CN

CurrentUICulture         : zh-CN

PrivateData            : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy

IsRunspacePushed         : False

Runspace             : System.Management.Automation.Runspaces.LocalRunspace

1.安装安装Framework 4.5(最低3.0)

http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe

2.修改注册列表:设置powershell本地脚本运行权限为remotesigned

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft\PowerShell

img_57fa794bcbd9dc8cccd0652a58246c60.png

2.升级到powershell-3.0及winrm

官网上说下载并执行下述脚本,但是一直不成功,原因是一直下载不下来安装包(可能的情况是网络不可达,大家懂得)

https://github.com/cchurch/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1

于是手动找通过微软官网找到下载地址win7_x64升级包如下

https://www.microsoft.com/en-us/download/confirmation.aspx?id=34595

下载powershell-3.0的更新补丁,此补丁同时集成WMF3.0,winrm等

地址:

https://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.1-KB2506143-x64.msu

(注意:这个更新包依赖于.net3.0以上版本,如果未安装.net,会有提示“此更新不适应于您的计算机”)

安装完成之后需要重启,重启后检验powershell版本

1> get-host

img_6d6ae1ca0c11e13f47853168dedba280.png

3.配置winrm

3.1 下载下面脚本,使用powershell运行,自动配置好winrm

https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

(注意:官网教程直到上面结束,但是一般执行完之后检验winrm是否已经运行,因为ansible管控机需要通过端口进行访问winrm)

3.2 在计算机上运行winrm服务

powershell 3.0中执行

1> winrm qc

img_fbd6c1a02f580a2a44c452c31a3fd5b0.png

这个提示,请将网络设置将工作网络 改成 家庭网络 就可以了,特殊情况可能需要重启,

img_a31e4c4cd0925bdd82fa15472ad1e4e2.png

配置好网络类型重新启动服务,结果如下:

img_98b6f8edc31aac8f89ed3b0300346a26.png

开启后,需要查看配置是否已经开启

3.3  主要是下图Auth中Basic设置为true,service中AllowUnencrypted设置为true

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

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

img_1e14023a5511e6f6b4eee1db802503fa.png

三、功能测试

配置ansible控制机

配置方法有两种:

第一种:

在/etc/ansible/hosts中

[windows]

192.168.1.11

[windows:vars]

ansible_ssh_user="Administrator"

ansible_ssh_pass="123456"

ansible_ssh_port=5986

ansible_connection="winrm"

$ ansible windows -m win_ping

192.168.1.11 | SUCCESS => {

    "changed": false, 

    "ping": "pong"

}

第二种

在/etc/ansible/hosts中

[windows]

192.168.1.11 ansible_ssh_user="Administrator"ansible_ssh_pass="123456"ansible_ssh_port=5986 ansible_connection="winrm"

要注意的是 端口方面ssl即https方式的使用5986,http使用5985。

区别于控制Linux主机,win主机的命令,需要加上win_,具体支持情况请见官网

http://docs.ansible.com/ansible/list_of_windows_modules.html

我这里做部分常用测试

传文件

删文件

创建用户(创建用户,更改密码,创建可以进行远程桌面登录的用户)

执行cmd命令(重启电脑)

1. 传文件到windows系统

$ ansible windows -m win_copy -a "src=/etc/passwd dest=e:\share"

192.168.1.11 | SUCCESS => {

    "changed": true, 

    "checksum": "a82c2bd7de3d9a5c5adddd5e4fc7cbf1703720b7", 

    "operation": "file_copy", 

    "original_basename": "passwd", 

    "size": 1755

}

img_b509605d8eca94c91d26b4aab99a7077.png

2. 删除文件

$ ansible windows -m win_file -a "dest=e:\share\passwd state=absent"

192.168.1.11 | SUCCESS => {

    "changed": true

}

img_7ffbb1ae176fe708f000750e77b119d8.png

3.创建用户

$ ansible windows -m win_user -a "name=aa passwd=123456"

192.168.1.11 | SUCCESS => {

    "account_disabled": false, 

    "account_locked": false, 

    "changed": true, 

    "description": "", 

    "fullname": "aa", 

    "groups": [], 

    "name": "aa", 

    "password_expired": true, 

    "password_never_expires": false, 

    "path": "WinNT://WORKGROUP/ABC-PC/aa", 

    "sid": "S-1-5-21-37586581-19248684-93829760-1006", 

    "state": "present", 

    "user_cannot_change_password": false

}

场景需求:

创建一个名叫user1的管理员用户,要求能够远程访问

$ ansible windows -m win_user -a "name=user1 password=123 groups='Administrators,Remote Desktop Users'"

192.168.1.11 | SUCCESS => {

    "account_disabled": false, 

    "account_locked": false, 

    "changed": true, 

    "description": "", 

    "fullname": "user1", 

    "groups": [

        {

            "name": "Administrators", 

            "path": "WinNT://WORKGROUP/ABC-PC/Administrators"

        }, 

        {

            "name": "Remote Desktop Users", 

            "path": "WinNT://WORKGROUP/ABC-PC/Remote Desktop Users"

        }

    ], 

    "name": "user1", 

    "password_expired": false, 

    "password_never_expires": false, 

    "path": "WinNT://WORKGROUP/ABC-PC/user1", 

    "sid": "S-1-5-21-375864581-19248684-93852960-1009", 

    "state": "present", 

    "user_cannot_change_password": false

}

4. 执行cmd命令

重启的第一种方式

1$ ansible windows -m win_shell -a "shutdown -r -t 1"

第二种方式,不加参数等同于第一种方式

1$ ansible windows -m win_reboot

目录
相关文章
|
4月前
|
Kubernetes Shell 网络安全
ansible的安装和简单的块使用
Ansible是一种自动化工具,用于配置管理、应用程序部署和协调云部署。它是一个开源工具,使用Python编写,通过SSH协议与远程主机通信。
123 2
ansible的安装和简单的块使用
|
Linux 开发工具
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)(二)
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)
200 0
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)(二)
|
缓存 Linux
yum安装ansible报错如何解决
本篇内容记录了yum安装ansible报错的解决方案。
1026 0
yum安装ansible报错如何解决
|
23小时前
|
存储 运维 Shell
Ansible自动化运维工具安装和基本使用
Ansible 是一款无代理的IT自动化工具,通过SSH连接目标主机执行配置管理、应用部署和云端管理任务。它使用YAML编写的Playbook定义任务,核心组件包括Playbook、模块、主机清单、变量等。Ansible的优势在于易用、功能强大、无须在目标主机安装额外软件,并且开源。安装过程涉及配置网络源、yum安装和SSH密钥设置。通过定义主机清单和使用模块进行通信测试,确保连接成功。
Ansible自动化运维工具安装和基本使用
|
8月前
|
缓存
yum install ansible无法直接安装Ansible的解决方法
准备三台机器: server.example.com node1.example.com node2.example.com 配置IP,主机名,/etc/hosts
310 0
|
4月前
|
Linux Python
百度搜索:蓝易云【centos使用pip安装ansible教程。】
以上是在CentOS中使用pip安装Ansible的简要步骤。请注意,这是一个基本指南,实际操作中可能会有特定的配置和依赖项。建议参考Ansible官方文档或其他详细教程以获得更全面和准确的信息。
50 0
|
8月前
|
运维 Ubuntu 应用服务中间件
【运维知识进阶篇】Ansible自动化运维-Ansible安装与主机列表
【运维知识进阶篇】Ansible自动化运维-Ansible安装与主机列表
117 0
|
11月前
|
Kubernetes 负载均衡 Docker
ansible学习之旅(ansible依托kubeadm安装一个简单的k8s集群)
ansible学习之旅(ansible依托kubeadm安装一个简单的k8s集群)
119 0
|
网络安全 数据安全/隐私保护
ansible安装及免密
ansible安装及免密
275 0
|
Web App开发 消息中间件 存储