官方文档查看:https://docs.ansible.com/ansible/latest/user_guide/windows.html
Ansible 从1.7+版本开始支持Windows,但前提是管理机必须为Linux系统,远程主机的通信方式也由SSH变更为PowerShell,同时管理机必须预安装Python的Winrm模块,方可和远程Windows主机正常通信,但PowerShell需3.0+版本且Management Framework 3.0+版本。 简单总结如下: (1)管理机必须为Linux系统且需预安装Python Winrm模块 (2) 底层通信基于PowerShell,版本为3.0+,Management Framework版本为3.0+ (3) 远程主机开启Winrm服务
常见执行策略有一下几种: Unrestricted:权限最高,可以不受限制执行任何脚本。 Default:为Powershell默认的策略:Restricted,不允许任何脚本执行。 AllSigned:所有脚本都必须经过签名才能在运行。 RemoteSigned:本地脚本无限制,但是对来自网络的脚本必须经过签名。
(1)winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;
winrm enumerate winrm/config/listener(2)针对winrm service 进行基础配置: 此过程还具有打开所需端口的防火墙并启动WinRM服务的额外好处 针对http
winrm quickconfig对于https服务配置,使用
winrm quickconfig -transport:https(3)查看winrm service listener:
winrm e winrm/config/listener(4)修改winrm配置,启用远程连接认证 为winrm service 配置auth:
winrm set winrm/config/service/auth '@{Basic="true"}'等效命令
Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true为winrm service 配置加密方式为允许非加密: AllowUnencrypted: 此选项定义WinRM是否允许在没有消息加密的情况下在HTTP上运行的通信。只有当Ansible_winrm_Transfer为NTLM、Kerberos或Credit ssp时,才有可能进行消息级加密。默认情况下,这是false,只应在调试WinRM消息时设置为true。这只应用于开发和调试目的,因为从Ansible发送的任何内容都可以查看、操作,远程会话也可以被同一网络上的任何人完全接管。
winrm set winrm/config/service '@{AllowUnencrypted="true"}'远程windows主机配置到此结束
添加如下内容
[windows] 192.168.52.12 ansible_ssh_user="Administrator" ansible_ssh_pass="P@ssw0rd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore192.168.52.12 为windows主机ip ansible_winrm_server_cert_validation=ignore 不需要证书认证
查看是否ping通 出现该提示是因为需预安装Python Winrm模块
如果没有安装pip, 请先安装对应于你的Python版本的pip:
sudo easy_install pip再安装winrm
pip install pywinrm这个时候再执行
ansible 192.168.52.12 -m win_ping可以看到是可以ping通了的
官方命令介绍: https://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html#windows-modules
存活状态如下 windows管制状态如下:
执行命令成功,说明shell执行本身没有问题
ansible windows -m win_shell -a "get-executionpolicy"https://docs.ansible.com/ansible/latest/modules/win_copy_module.html#win-copy-module
ansible windows -m win_copy -a 'src=/root/windows-tranport/test.txt dest=E:\centosFile\test.txt'报错:
Exception calling "Run" with "1" argument(s): "Exception calling "Invoke" with "0" argument(s): "Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Illegal characters in path."" At line:47 char:5 + $output = $entrypoint.Run($payload) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ScriptMethodRuntimeException看不懂啥问题, 把目标文件名去掉就可以运行成功了,但是目录或者文件名只能用原始的,不能指定拷贝后文件的重命名。 试了几次,发现是目标路径的分隔符问题,用\会有问题,换成/ 或者\ 就OK
ansible 192.168.52.12 -m win_copy -a 'src=/root/windows-tranport/test.txt dest=E:\centosFile\' 192.168.52.12 | SUCCESS => { "changed": true, "checksum": "04f29fd9c9d1b851a54d4199950b8d352fd48263", "dest": "'E:\\centosFile\\test.txt'", "operation": "file_copy", "original_basename": "test.txt", "size": 127, "src": "/root/windows-tranport/test.txt" }返回结果
operation: “file_copy”—执行的操作为 file_copy;original_basename: 文件名;size: 文件大小 bytes。src: 源路径dest:目标路径checksum:复制文件到远程主机,每次备份会产生一个md5sum,如果两次赋值文件的md5sum相同,那么就不会再次执行复制动作参考:https://www.cnblogs.com/dachenzi/p/8916521.html
-u : remote user,默认使用root用户登陆 -i : Inventory,指定主机,默认是/etc/ansible/hosts -m :指定模块的名称(不指定-m,那么默认是command模块) -a : 模块的参数(比如使用command模块,那么-a参数就是要执行的命令) -k : 用来提示输入远程主机的密码(基于用户密码登录) -f : 一次执行几个进程(并发数量),默认为5个 –sudo : 执行命令时使用 sudo 权限(需要用户具有sudo权限) –key-file: 建立SSH链接的私钥文件 –list-hosts: 列出匹配到的服务器列表
/etc/ansible/hosts:(默认的Inventory) [test] # 用于定义主机组,all表示所有主机,所以尽量避免使用all作为组名 127.0.0.1 # 该组的主机列表,可以是主机名(dns解析)或者IP地址,或者用符号来表示连续的主机 192.168.1.[80:88] # 表示 192.168.1.80 - 192.168.1.88 [web:children] # 表示子组 nginx # nginx组 test # test组
配置文件中的变量及参数:
ansible_ssh_user : 用于指定管理远程主机的帐号 ansible_ssh_host : 用于指定被管理的主机 ansible_ssh_port :用于指定ssh的端口 ansible_ssh_private_key_file :指定key文件 host_key_checking=False :当第一次连接主机时,会提示yes/no,跳过此次环节 ansible_connection=“winrm” : windows的连接方式 ansible_winrm_server_cert_validation=ignore
例子 test 为hosts配置的测试的主机组
ansible test -m file -a 'src=/etc/fstab dest=/tmp/fstab state=link' -k # 等于 ln -s /etc/fstab /tmp/fstab ansible test -m file -a 'path=/tmp/fstab state=absent' -k # 删除文件 ansible test -m file -a 'path=/tmp/lixin.txt state=touch owner=nobody group=nobody mode=666' -k # 创建文件指定其属主,属组,权限Ansible通过模块来执行具体的操作,由于模块功能千差万别,所以执行模块操作后,Ansible会根据不同的需要返回不同的结果,Ansible中一些常见的返回值如下:
changed:几乎所有的Ansible模块都会返回该变量,表示模块是否对远程主机进行了操作failed:如果模块未能执行完成,将返回failed和true msg:模块执行失败的原因,常见的错误如ssh连接失败,没有执行权限等rc:与命令行相关的模块会返会rc,表示执行Linux名的返回码 stdout:与rc相似,返回的是标准输出的结果stderr:与rc详细,返回的是标准差错误输出 backup_file:所有存在backup选项的模块,用来返回备份文件的路径results:应用在Playbook中存在循环的情况,返回多个结果5、setup 用于获取系统信息的一个模块 https://buji595.github.io/2019/05/27/Ansible Ad-hoc常用Module/#setup
# 查看模块参数 [root@ansible ~]# ansible-doc -s setup # 查看系统所有信息 [root@ansible ~]# ansible 192.168.1.31 -m setup # filter 对系统信息进行过滤 [root@ansible ~]# ansible 192.168.1.31 -m setup -a 'filter=ansible_all_ipv4_addresses' # 常用的过滤选项 ansible_all_ipv4_addresses 所有的ipv4地址 ansible_all_ipv6_addresses 所有的ipv6地址 ansible_architecture 系统的架构 ansible_date_time 系统时间 ansible_default_ipv4 系统的默认ipv4地址 ansible_distribution 系统名称 ansible_distribution_file_variety 系统的家族 ansible_distribution_major_version 系统的版本 ansible_domain 系统所在的域 ansible_fqdn 系统的主机名 ansible_hostname 系统的主机名,简写 ansible_os_family 系统的家族 ansible_processor_cores cpu的核数 ansible_processor_count cpu的颗数 ansible_processor_vcpus cpu的个数