[原创]Ansible基于Windows的管理架构之二(完结)

 

一、Windows系统预配置和Linux发版版稍有区别,远程主机系统如为Windows需预先如下配置:安装F...



一、Windows系统预配置

和Linux发版版稍有区别,远程主机系统如为Windows需预先如下配置:

  • 安装Framework 3.0+
  • 设置PowerShell本地脚本运行权限为remotesigned
  • 升级PowerShell至3.0+
  • 自动设置Windows远端管理,英文全称WS-Management(WinRM)
我们逐一介绍:

(1)安装Framework 3.0+

下载链接为:http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe。 下载至本地后双击左键安装即可,期间可能会多次重启,电脑需正常连接Internet。

(2)设置PowerShell本地脚本运行权限为remotesigned

因Windows系统默认不允许非Adminitor外的普通用户执行SP脚本,即使是管理员,如下开放P脚本执行权限。

  • 步骤1 打开CMD输入regedit.exe 如下图打开注册表。
  • 步骤2 设置SP脚本系统可运行。依次打开注册表目录HKEY_LOCAL_MACHINESOFTWAREMicrosoftPowerShell1ShellIdsMicrosoft.PowerShell如图下图修改SP脚本可执行权限为remotesigned。或者,打开PowerShell执行命令:set-executionpolicy -executionpolicy unrestricted 返回结果如图这里共4种权限:
  • Restricted—默认的设置, 不允许任何script运行;
  • AllSigned—只能运行经过数字证书签名的script;
  • RemoteSigned—运行本地的script不需要数字签名,但是运行从网络上下载的script就必须要有数字签名;
  • Unrestricted—允许所有的script运行。步骤3、升级PowerShell至3.0+PowerShell 3.0+ 需基于Windows 7 Sp1安装,Windows7系统Sp1补丁升级请参考http://windows.microsoft.com/installwindows7sp1,这里不作详细介绍。Window 7和Windows Server 2008 R2默认安装的有PowerShell,但版本号一般为2.0版本,所以我们需升级至3.0+,如下图中数字1部分表示PowerShell版本过低需3.0+版本,数字2部分表示当前PowerShell版本为2.0PowerShell至3.0+版本,下载地址如下:
https://github.com/cchurch/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1。下载至本地后,如图10-5右键选择“使用PowerShell运行”,执行完毕重启系统后,在PowerShell执行Get-Host命令结果如下图所示PowerShell版本为3.0为正常。




步骤4、自动设置Windows远端管理(WS-Management,WinRM)下载补丁脚本:
https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1,下载至本地,右击后选择“使用PowerShell运行”,执行结果没有返回错误即为正常。

如执行出现“由于此计算机上的网络连接类型之一设置为公用,因此 WinRM防火墙例外将不运行”类似报错,请在PowerShell中执行命令Enable-PSRemoting – SkipNetworkProfileCheck –Force尝试解决。

好了,远程Windows主机配置到此结束,我们验证配置的是否有问题,在Master机做如下设置:

  • 步骤1、配置Inventory 添加/etc/ansible/hosts配置。
[windows]
192.168.37.146 ansible_ssh_user="Administrator" ansible_ssh_pass="magedu@beijing" ansible_
ssh_port=5986 ansible_connection="winrm"
  • 步骤2、在Master上执行命令:
    ansible windows -m win_ping
    返回结果如下图所示:
  • 注意:Windows系统建议使用Administrator用户,避免不可预知的问题。命令运行前请设置Administrator可登录且登录密码为magedu@beijjing。

二、Windows下可用模块

Windows下可用模块虽不及Linux丰富,但基础功能均包括在内,如下介绍日常工作常用到的模块,请参考。

  • win_acl (E) —设置文件/目录属主属组权限;
  • win_copy—拷贝文件到远程Windows主机;
  • win_file —创建,删除文件或目录;
  • win_lineinfile—匹配替换文件内容;
  • win_package (E) —安装/卸载本地或网络软件包;
  • win_ping —Windows系统下的ping模块,常用来测试主机是否存活;
  • win_service—管理Windows Services服务;
  • win_user —管理Windows本地用户。
更多模块及详细功能介绍请参考官网:http://docs.ansible.com/ansible/list_of_windows_modules.html除win开头的模块外,scripts,raw,slurp,setup模块在Windows 下也可正常使用。

三、Windows Ansible模块使用实战

本节通过几个实战案例为大家演示一些常用模块用法。

案例1: 传输/etc/passwd文件至远程E:file目录下执行命令:

ansible windows -m win_copy -a 'src=/etc/passwd dest=E:filepasswd'
返回结果:

192.168.37.146 | success >> {

"changed": true,

"checksum": "896d4c79f49b42ff24f93abc25c38bc1aa20afa0",

"operation": "file_copy",

"original_basename": "passwd",

"size": 2563
}
部分返回结果诠释:

  • “operation”: “file_copy”—执行的操作为 file_copy;
  • “original_basename”: “passwd”—件名为 passwd;
  • “size”: 2563—文件大小为 2563 bytes。
Playbook写法如下:

---
- name: windows module example

hosts: windows

tasks:

- name: Move file on remote Windows Server from one location to another

win_file: src=/etc/passwd dest=E:filepasswd
案例2: 删除案例1中的E:filepasswd。执行命令:

ansible windows -m win_file -a "path=E:filepasswd state=absent"
返回结果:

192.168.37.146 | success >> {

"changed": true
}
案例3: 新增用户stanley,密码为magedu@123,属组为Administrators。执行命令:

ansible windows -m win_user -a "name=stanley passwd=magedu@123 group=Administrators"
返回结果:

192.168.37.146 | success >> {

"account_disabled": false,

"account_locked": false,

"changed": true,

"description": "",

"fullname": "stanley",

"groups": [

{

"name": "Administrators",

"path": "WinNT://WORKGROUP/LINUXLST/Administrators"

}

],

"name": "stanley",

"password_expired": true,

"password_never_expires": false,

"path": "WinNT://WORKGROUP/LINUXLST/stanley",

"sid": "S-1-5-21-3965499365-1200628009-3594530176-1004",

"state": "present",

"user_cannot_change_password": false
}
部分返回结果诠释:

  • account_disabled—禁用用户登录;
  • account_locked—解锁用户;
  • groups—用户所属组;
  • name—用户名;
  • password_expired—下次登录修改密码;
  • user_cannot_change_password—用户是否可修改密码。
案例4:重启Windows spooler服务执行命令:

ansible windows -m win_service -a "name=spooler state=restarted"
返回结果:

192.168.37.146 | success >> {

"changed": true,

"display_name": "Print Spooler",

"name": "spooler",

"start_mode": "auto",

"state": "running"
}

本章小结

本章为大家介绍了当远程主机为Windows时Ansible的管理机制,从整体配置复杂程度上相对Linux要麻烦些,现如今服务器市场Linux大为流行的背景下,各软件对Windows的支持力度确实不如预期,也曾收到业界Windows应用者反馈Ansible管理Windows期间存在意想不到的问题,其实何止Ansible,现流行的集中化管理工具对Windows的支持力度均一般。笔者Windows的维护经历提醒各位:
珍爱生命,远离Windows。

关注我们:


    关注 运维部落


微信扫一扫关注公众号

0 个评论

要回复文章请先登录注册