概述
Ansible是自動化運維工具,基于Python開發(fā),實現(xiàn)了批量系統(tǒng)配置、批量程序部署朝氓、批量運行命令等功能。Ansible是基于模塊(module)和劇本(playbook)工作主届。
安裝指南
本次在Ubuntu上安裝Ansible 2.7 . 更詳細安裝方式參見: Ansible 安裝指南
服務器端的要求
目前Ansible可以從安裝了Python 2(2.7版)或Python 3(3.5及更高版本)的任何機器上運行圃验∑兰埽控制計算機不支持Windows狭归。
這包括Red Hat活合,Debian,CentOS绘闷,macOS筷屡,任何BSD等等。
服務器端安裝Ansible
通過Apt (Ubuntu)安裝最新版本
這里有一個PPA版本的Ubuntu源簸喂。
要在您的計算機上配置PPA并安裝ansible毙死,請運行以下命令:
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt-get install ansible
:exclamation:
在較舊的Ubuntu發(fā)行版中,“software-properties-common”被稱為“python-software-properties”喻鳄。
Windows 指南
設置Windows主機
主機要求
要使Ansible與Windows主機通信并使用Windows模塊扼倘,Windows主機必須滿足以下要求:
- Ansible支持的Windows版本通常與Microsoft當前和擴展支持下的版本相匹配。支持的桌面操作系統(tǒng)包括Windows 7,8.1和10除呵,受支持的服務器操作系統(tǒng)包括Windows Server 2008,2008 R2,2012,2012 R2和2016再菊。
- Ansible需要PowerShell 3.0或更高版本,并且至少要在Windows主機上安裝.NET 4.0颜曾。
- 應創(chuàng)建并激活**WinRM listener **纠拔。更多細節(jié)可以在下面找到。
:exclamation:
雖然這些是Ansible連接的基本要求泛豪,但是一些Ansible模塊還有其他要求稠诲,例如較新的OS或PowerShell版本侦鹏。請參閱模塊的文檔頁面以確定主機是否滿足這些要求。
升級PowerShell和.NET框架
升級powershell需要重啟服務器才能生效臀叙。
Ansible需要PowerShell 3.0版和.NET Framework 4.0或更高版本才能在較舊的操作系統(tǒng)(如Server 2008和Windows 7)上運行略水。基本OS鏡像不符合此要求劝萤。您可以使用Upgrade-PowerShell.ps1腳本來更新這些腳本渊涝。
這是如何從PowerShell運行此腳本的示例:
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Upgrade-PowerShell.ps1"
$file = "$env:temp\Upgrade-PowerShell.ps1"
$username = "Administrator"
$password = "Password"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
# version can be 3.0, 4.0 or 5.1
&$file -Version 5.1 -Username $username -Password $password -Verbose
完成后,您將需要刪除自動登錄并將執(zhí)行策略設置回默認值Restricted
床嫌。您可以使用以下PowerShell命令執(zhí)行此操作:
# this isn't needed but is a good security practice to complete
Set-ExecutionPolicy -ExecutionPolicy Restricted -Force
$reg_winlogon_path = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty -Path $reg_winlogon_path -Name AutoAdminLogon -Value 0
Remove-ItemProperty -Path $reg_winlogon_path -Name DefaultUserName -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $reg_winlogon_path -Name DefaultPassword -ErrorAction SilentlyContinue
該腳本的工作原理是檢查需要安裝哪些程序(例如.NET Framework 4.5.2)以及需要什么樣的PowerShell版本跨释。如果需要重新啟動并且設置了username
和password
參數(shù),則腳本將在重新啟動時自動重新啟動并登錄厌处。該腳本將繼續(xù)鳖谈,直到不再需要執(zhí)行任何操作且PowerShell版本與目標版本匹配為止。如果未設置username
和 password
參數(shù)嘱蛋,腳本將提示用戶手動重新引導并在需要時登錄蚯姆。當用戶下次登錄時五续,腳本將從停止的位置繼續(xù)洒敏,并且該過程將繼續(xù),直到不再需要其他操作為止疙驾。
:exclamation:
如果在Server 2008上運行凶伙,則必須安裝SP2。如果在Server 2008 R2或Windows 7上運行它碎,則必須安裝SP1函荣。
Windows Server 2008只能安裝PowerShell 3.0; 指定較新的版本將導致腳本失敗。
username
和password
參數(shù)都是存儲在注冊表中的純文本扳肛。確保在腳本完成后運行清理命令傻挂,以確保主機上仍未存儲憑據(jù)。
WinRM內(nèi)存補丁
在PowerShell v3.0上運行時挖息,WinRM服務存在一個錯誤金拒,它限制了WinRM可用的內(nèi)存量。如果未安裝此補丁套腹,Ansible將無法在Windows主機上執(zhí)行某些命令绪抛。這些補丁應作為系統(tǒng)引導或映像過程的一部分安裝。Install-WMF3Hotfix.ps1腳本可用于在受影響的主機上安裝此修補程序电禀。
以下PowerShell命令將安裝此修補程序:
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1"
$file = "$env:temp\Install-WMF3Hotfix.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
WinRM設置
一旦Powershell升級到至少3.0版本幢码,最后一步是配置WinRM服務,以便Ansible可以連接到它尖飞。WinRM服務有兩個主要組件症副,用于管理Ansible如何與Windows主機連接:listener
和service
配置設置店雅。
可以在下面閱讀有關(guān)每個組件的詳細信息,也可以使用腳本ConfigureRemotingForAnsible.ps1來進行基本設置瓦糕。此腳本使用自簽名證書設置HTTP和HTTPS偵聽器底洗,并在服務上啟用Basic
身份驗證選項。
要使用此腳本咕娄,請在PowerShell中運行以下命令:
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
:exclamation:
ConfigureRemotingForAnsible.ps1腳本僅用于培訓和開發(fā)目的亥揖,不應在生產(chǎn)環(huán)境中使用,因為它啟用了
Basic
這本質(zhì)上不安全(如身份驗證)圣勒。
WinRM Listener
WinRM服務偵聽一個或多個端口上的請求费变。每個端口都必須創(chuàng)建并配置一個偵聽器。
要查看在WinRM服務上運行的當前偵聽器圣贸,請運行以下命令:
winrm quickconfig
winrm enumerate winrm/config/Listener
運行后輸出如下:
Listener
Address = *
Transport = HTTP
Port = 5985
Hostname
Enabled = true
URLPrefix = wsman
CertificateThumbprint
ListeningOn = 10.0.2.15, 127.0.0.1, 192.168.56.155, ::1, fe80::5efe:10.0.2.15%6, fe80::5efe:192.168.56.155%8, fe80::
ffff:ffff:fffe%2, fe80::203d:7d97:c2ed:ec78%3, fe80::e8ea:d765:2c69:7756%7
Listener
Address = *
Transport = HTTPS
Port = 5986
Hostname = SERVER2016
Enabled = true
URLPrefix = wsman
CertificateThumbprint = E6CDAA82EEAF2ECE8546E05DB7F3E01AA47D76CE
ListeningOn = 10.0.2.15, 127.0.0.1, 192.168.56.155, ::1, fe80::5efe:10.0.2.15%6, fe80::5efe:192.168.56.155%8, fe80::
ffff:ffff:fffe%2, fe80::203d:7d97:c2ed:ec78%3, fe80::e8ea:d765:2c69:7756%7
修改winrm配置挚歧,啟用遠程連接認證
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
windows 防火墻配置
配置防火墻:
- 添加防火墻信任規(guī)則,允許5985-5986端口通過
- 打開防火墻高級配置吁峻,選擇入站規(guī)則滑负,在點擊新建規(guī)則
- 填寫一下信息
- TCP
- 信任端口5985-5986
- 填寫新建規(guī)則名稱
Ansible服務器端配置并管理Windows
添加windows客戶端連接信息: 編輯/etc/ansible/hosts
, 添加客戶端主機信息(ansible服務端的配置)
[windows]
192.168.2.2 ansible_user="Administrator" ansible_password="Password" ansible_port=5986 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=basic
ping 遠程windows主機
$ ansible 192.168.2.2 -m win_ping
192.168.2.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
創(chuàng)建目錄
$ ansible 192.168.2.2 -m win_file -a 'path=D:\\test state=directory'
192.168.2.2 | CHANGED => {
"changed": true
}
下發(fā)文件
$ ansible 192.168.2.2 -m win_copy -a 'src=/etc/hosts dest=D:\\hosts.txt'
192.168.2.2 | CHANGED => {
"changed": true,
"checksum": "f6d471689e1233342a8e43a130ff40a6ea0b9f51",
"dest": "D:\\hosts.txt",
"operation": "file_copy",
"original_basename": "hosts",
"size": 635,
"src": "/etc/hosts"
}
刪除文件
# ansible 192.168.2.2 -m win_file -a 'dest=d:\\config_dir\\hosts.txt state=absent'
刪除目錄
# ansible 192.168.2.2 -m win_file -a 'dest=d:\\config_dir2 state=absent'
執(zhí)行cmd命令
# ansible 192.168.2.2 -m win_shell -a 'ipconfig'
重啟windows
# ansible 192.168.2.2 -m win_reboot
# ansible 192.168.2.2 -m win_shell -a 'shutdown -r -t 0'
創(chuàng)建用戶
遠程在windows客戶端上創(chuàng)建用戶
# ansible 192.168.2.2 -m win_user -a "name=testuser1 passwd=123456"
windows服務管理
# ansible 192.168.2.2 -m win_shell -a “net stop|start zabbix_agent”
完整的windows module見: Windows modules