首页 技术 正文
技术 2022年11月10日
0 收藏 525 点赞 5,012 浏览 2636 个字

一、本地环境:

centos 7    192.168.10.10      主控机

centos 7    192.168.10.130    被控机

Windows7  192.168.10.131   被控机

二、安装ansible准备环境

1、安装ansible前,我们使用国内的yum源进行安装。这里使用阿里的第三方软件源

[root@localhost ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]#yum clean all
[root@localhost ~]#yum makecache
[root@localhost ~]#yum install ansible -y

2、设置ssh免秘钥登陆

[root@localhost ~]# ssh-keygen -N ''    // 生成公钥和私钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): // 这里默认回车即可
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Uzb5QDBxve8hKWRCVUCNj1Ptc3na3DZp2Udc16XN5vU root@localhost.localdomain
The key's randomart image is:
+---[RSA ]----+
| +=**.. +|
| .+..+ .++|
| . *+ o..O|
| .o=+o o+B|
| S+ ..o BE|
| .. o +=B|
| . o.oo|
| . |
| |
+----[SHA256]-----+

如果选择默认路径,则秘钥文件在当前用户的家目录~/.ssh/下

ansible简易使用

3、将秘钥发给客户端

[root@localhost ~]# ssh-copy-id root@192.168.10.130

这里有个问题需要注意下,如果主控端以前使用ssh连接过被控端,在给客户端发送秘钥的过程中可能会有些小问题,发送完成后你可能会发现~/.ssh这个目录不见了。不要着急,在执行一次生成秘钥,然后再次将秘钥发给客户端即可。最终你会看到像下面这个样子的:

ansible简易使用

4、验证ssh免秘钥登陆

[root@localhost ~]# ssh 192.168.10.130

登陆成功后,看起来像下面这样:

ansible简易使用

三、ansible的简单使用

1、使用ansible测试被监控主机是否存活

[root@localhost ~]# ansible 192.168.10.130 -m ping   // 指定主机查看是否存活

如果看到下面这样,说明主机存活。

ansible简易使用

1.1查询被控端主机名

[root@localhost ~]# ansible damo -m command -a 'hostname'

ansible简易使用

2、使用ansible控制Windows主机

2.1首先要确保本机Microsoft .NET Framework 4.0以上版本http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe

2.2下载powershell 4.0 https://www.microsoft.com/zh-cn/download/details.aspx?id=40855

关于如何安装这里就不再多说了。安装完成后需要重启,重启后我们的powershell应该是这个版本:

ansible简易使用

2.3设置远程策略允许远程主机连接

set-executionpolicy remotesigned   // 注意需要以管理员身份来运行

ansible简易使用

3、启动winrm服务

winrm quickconfig

ansible简易使用

3.1查看winrm服务

winrm e winrm/config/listener

ansible简易使用

3.2为winrm service 配置auth:

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

ansible简易使用

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

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

ansible简易使用

好了Windows到这里就告一段落,下面我们进行测试。

4、主控机上需要安装Python winrm模块

[root@localhost ~]# pip install pywinrm

这里需要注意:ansible使用的是Python2.x版本如果安装了多版本的Python3.x的环境要注意在使用pip安装pywinrm模块时要确定模块安装到哪个Python环境中去了,否则ansible有可能会找不到winrm模块,所以为了避免这种混乱尽量在主控端只安装一个Python2.7版本。如果是系统默认yum安装的Python2.7,你还需要yum  install  python-pip  -y  安装pip。

5、在ansible的主机配置文件中添加Windows主机

[root@localhost ~]# vim /etc/ansible/hosts
[linux] // linux主机
192.168.10.130
[windows] // Windows 主机
192.168.10.131 ansible_ssh_user="id_iot" ansible_ssh_pass="123.com" ansible_ssh_port= ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

6、测试Windows主机是否存活

[root@localhost ~]# ansible windows -m win_ping

ansible简易使用

6.1查看Windows当前登陆用户

[root@localhost ~]# ansible windows -m win_command -a "whoami"

ansible简易使用

6.2在被控端主机上创建文件夹

[root@localhost ~]# ansible 192.168.10.131 -m win_file -a 'dest=c:\config_dir state=directory'

ansible简易使用

未完待续。。。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294