ansible搭建和基本使用

本文详细描述了在Linux和Windows客户端上安装Python、OpenSSH、PowerShell4.0以及配置WinRM的过程,包括Windows升级PowerShell、安装.NETFramework、防火墙设置和Ansible的使用,如Inventory文件配置与基本操作指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端搭建

linux

安装Python(python2版本,并且必须存在路径/usr/bin/python)
安装openssh-server,并且配置允许root远程连接(推荐)

windows

Windows Server 2008R2客户端升级至powershell4.0

配置winrm之前检查系统版本,以及powershell版本,如果是Server2008R2版本,则需要升级powershell至4.0版本。Server2012R2以上的版本不需要升级powershell。

1. 检查powershell版本
在powershell终端上执行get-host命令可以查看powershell版本。未升级前显示的是2.0版本

2. 下载并安装Microsoft .NET Framework 4.5
下载地址:
https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe

3. 下载并安装powershell4.0(Windows Management Framework 4.0 )
下载地址:
https://download.microsoft.com/download/3/D/6/3D61D262-8549-4769-A660-230B67E15B25/Windows6.1-KB2819745-x64-MultiPkg.msu
注意: 先安装.NET Framework 4.5 ,然后安装powershell4.0,安装完成之后重启windows服务器

4. 升级完powershell4.0后检查
get-host命令查看powershell版本
Windows客户端配置winrm,启用powershell远程管理
打开powershell终端,按以下步骤执行命令(正常情况不会报错,如果有报错,请检查输入的命令是否正确,或者手动输入命令进行配置)
1. 查看powershell执行策略
get-executionpolicy

2. 更改powershell执行策略为remotesigned
set-executionpolicy remotesigned

3. 配置winrm service并启动服务
winrm quickconfig

4. 查看winrm service启动监听状态
winrm enumerate winrm/config/listener

5. 修改winrm配置,启用远程连接认证
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

6.Windows客户端防火墙配置
通过命令winrm enumerate winrm/config/listener检查winrm服务正确启动之后
添加防火墙信任规则,允许5985端口通过

服务端搭建

pip安装

pip install ansible可以直接安装上,只是配置文件不知道在哪里

包管理安装

apt/yum install ansible
pip2 install pywinrm(用来支持远程控制windows)
ansible --version(验证安装结果)
ansible-doc -l (列出所有ansible模块)

基本使用

Ansible Inventory文件

Inventory中文文档

Inventory文件通常用于定义要管理的主机的认证信息,例如ssh登录用户名、密码以及key相关信息。可以同时操作一个组的多台主机,组与主机组之间的关系都是通过inventory文件配置。配置文件路径为:/etc/ansible/hosts

vim /etc/ansible/hosts
# 方法一 主机+端口+密码
[groupname]
192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
[groupname]
192.168.1.32 ansible_ssh_user="Administrator" ansible_ssh_pass="zteict123" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

执行命令

ansible groupname或ip -m shell -a "ip a"
ansible groupname -m win_shell -a "ipconfig"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ordar123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值