ansible自動化管理windows系統

一,ansible配置

1、簡介
Ansible 從1.7+版本開始支持Windows,但管理機必須爲Linux系統,遠程主機的通信方式也由Linux下的SSH變爲PowerShell,管理機需要安裝Python的pywinrm模塊,但PowerShell需3.0+版本且Management Framework 3.0+版本,實測Windows 7 SP1和Windows Server 2008 R2及以上版本系統經簡單配置可正常與Ansible通信。
2、環境準備
以下配置在CentOS7.4_x64下
安裝pip及相關依賴

下載pip
#wget  
#python get-pip.py
安裝依賴
#pip install pywinrm paramiko PyYAML Jinja2 httplib2 six

3、安裝ansible

#yum -y install ansible

4,配置ansible文件

#vim /etc/ansible/hosts

[win7]

172.16.70.80 ansible_ssh_user="administrator" ansible_ssh_pass="root" ansible_ssh_port=5985 ansible_connection="winrm"

注意上信息在一行;以空格隔開,[win7] 是這臺主機的標題;下面的是ip和連接信息等;

二、被管理端win7配置

1、環境簡介
和Linux稍有區別,被管理端系統如果是Windows系列時;需預先有以下配置:
安裝Framework 3.0+ (有可能需要下載)
配置powershell策略爲remotesigned (需要修改)
升級PowerShell至3.0+(win7默認是2.0)
設置Windows遠端管理,英文全稱WS-Management(WinRM)

2、環境配置
a、升級或安裝Framework 4.5
如果Framework版不滿足請至微軟官方下載
b、修改powershell策略爲remotesigned 打開powershell終端

PS C:\Users\Administrator>set-executionpolicy remotesigned


查看powershell 

PS C:\Users\Administrator>get-executionpolicy 


設置Windows遠端管理(WS-Management,WinRM)服務
winrm 服務默認都是未啓用的狀態;注意以下操作在cmd中執行,而非powershell中
對winrm服務進行基礎配置:

C:\Users\Administrator>winrm quickconfig


C:\Users\Administrator>winrm e winrm/config/listener


C:\Users\Administrator>winrm set winrm/config/service/auth @{Basic="true"}


C:\Users\Administrator>winrm set winrm/config/service @{AllowUnencrypted="true"}

在ansible服務器上ping遠程windows服務器

 #ansible win7 -m win_ping 


可以用ansible-playbook添加用戶,將用戶加入到對應的組裏

#vim  adduser.yml 

- name: Add a group and user

  hosts: all

  gather_facts: false

  tasks:

    - name: Add User

      win_user:

        name: ops

        password: "A123456"

        state: present

        groups:

          - "Remote Desktop Users"

          - Users


執行playbook文件,登陸服務器查看用戶狀態

#ansible-playbook adduser.yml 








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章