標籤:ansible-playbook vcredist2010
需求:
遠程在windows server 2012 R2上安裝vcredist 2008, 2010, 2012和2013 四個版本。
首先在ansible的官方文檔找了一下看有沒有適合的模組直接能使用的。
最開始找到的是win_package模組: http://docs.ansible.com/ansible/win_package_module.html ,同時看到它給出來的例子.
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />
感覺可以直接拿來就用啊。把path路徑換成要安裝的exe 放置的本地路徑,不就可以了嗎?
後來發現,win_package裡的product_id 是必須的,但是在系統沒有安裝vcredist之前,這些product_id 是擷取不到的。嘗試了先在系統手動安裝四個vcredist 版本,然後再去註冊表找到product_id。然後再將product_id 寫到ansible-playbook裡,沒錯,最後還是失敗了。
好吧,只能找其他辦法了,嘗試使用powershell指令碼實現。
剛開始,測試安裝vcredits2008 的指令碼是這樣的:
vcredist_2008.ps1
$pathvargs_2008 = {C:\tmp\vcredist2008_x64.exe /S /v/qn }
Invoke-Command -ScriptBlock $pathvargs_2008
然後ansible-playbook指令碼是這樣的:
win_shell.yml :
---- name: run win_shell hosts: ec2_windows gather_facts: false tasks: - name: Install 2008 ... win_shell: c:\tmp\vcredist_2008.ps1
ansible-playbook win_shell.yml 執行一下,發現居然成功了。
依樣畫葫蘆,哢哢哢,就把其他三個版本的指令碼,換了個安裝包的名字,就以為可以了。
後來,發現vcredist2010的版本總是安裝不上。手動安裝比較了一下,軟體在安裝的時候,2010提示的資訊和其餘三個版本的有點差異,應該在powershell指令碼的參數上是不一樣的。
好吧,繼續在網上找原因。
終於找到了一篇有用的文章:《Mailbag: How to perform a silent install of the Visual C++ 2010 redistributable packages》https://blogs.msdn.microsoft.com/astebner/2010/10/20/mailbag-how-to-perform-a-silent-install-of-the-visual-c-2010-redistributable-packages/
原來在微軟的官網上一直躺著我要找的答案。文章裡提到了Silent install, Unattended install, Silent repair, Silent uninstall. 四種方式,我現在要找的就是Unattended install模式,不需要人工互動的方式。馬上將參數複製黏貼過來使用。
所以,現在的vcredist_2010.ps1 的命令變成了這樣:
$pathvargs_2010 = {C:\tmp\vcredist2010_x64.exe /passive /norestart }
Invoke-Command -ScriptBlock $pathvargs_2010
就是2010 版本安裝的指令碼不一樣而已,其他的都一樣。
最終的ansible-playbook 是這樣的:
vim win_shell.yml :
---
- name: run win_shell
hosts: ec2_windows
gather_facts: false
tasks:
- name: Install 2013 ...
win_shell: c:\tmp\vcredist_2013.ps1
- name: Install 2008 ...
win_shell: c:\tmp\vcredist_2008.ps1
- name: Install 2012
win_shell: c:\tmp\vcredist_2012.ps1
- name: Install 2010 ...
win_shell: c:\tmp\vcredist_2010.ps1
測試:
先把伺服器上之前安裝的vcredist都卸載了,再運行 ansible-playbook win_shell.yml 。
我就靜靜地等待結果,最終看到四個成功的提示。然後,在server 2012 R2 伺服器上,查看註冊表,四個版本的vcredist 都成功安裝了。
本文出自 “dayAndNight” 部落格,請務必保留此出處http://hellocjq.blog.51cto.com/11336969/1923528
ansible遠程在Windows server 2012 R2 安裝vcredist(2008 2010 2012 2013)