[英]Error installing Chocolatey via Ansible on Windows
我在 Windows Server 2008 R2 上使用 Ansible 安裝 Chocolatey 和 Chocolatey 包時遇到問題。 在 Windows Server 2012 R2 上一切正常(它有內置的 PowerShell v3.0)。
我在Ansible 文檔中運行 PowerShell 腳本時遇到問題,但我運行了Set-ExecutionPolicy RemoteSigned
,這有幫助。 我安裝了 Windows PowerShell 3.0 以便 Ansible 可以運行。 現在,當我運行劇本時,出現以下錯誤:
failed: [192.168.1.1] => {"failed": true, "parsed": false}
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 6/16/2015 6:16 AM chocInstall
Downloading https://chocolatey.org/api/v2/package/chocolatey/ to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip
Download 7Zip commandline tool
Downloading https://chocolatey.org/7za.exe to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\7za.exe
Extracting C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall...
Installing chocolatey on this machine
{
"changed": false,
"msg": "The term \u0027C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\chocolatey\\chocInstall\\tools\>\chocolateyInstall.ps1\u0027 is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.",
"failed": true
}
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Processing archive: C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip
Extracting _rels\.rels
Extracting chocolatey.nuspec
Extracting tools\chocolateyInstall.ps1
Extracting tools\chocolateysetup.psm1
Extracting tools\init.ps1
Extracting tools\chocolateyInstall\choco.exe
Extracting tools\chocolateyInstall\choco.exe.ignore
Extracting package\services\metadata\core-properties\61804721eec44e8592a61904d0a62022.psmdcp
Extracting [Content_Types].xml
Everything is Ok
Files: 9
Size: 3738621
Compressed: 1259522
FATAL: all hosts have already failed -- aborting
第二次運行后,我有一個不同的錯誤:
failed: [192.168.1.1] => {"changed": false, "failed": true}
msg: The specified module 'C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocInstall\tools\chocolateyInstall\helpers\chocolateyInstaller.psm1' was not loaded because no valid module file was found in any module directory.
FATAL: all hosts have already failed -- aborting
我注意到 Ansible 在將 Chocolatey 從%TEMP%
解包到%PROGRAMDATA%
遇到問題。 因此,在從%TEMP%\\chocolatey\\helpers
運行chocolateyInstall.ps1
后(我認為這是一個好路徑),我遇到了這個錯誤:
failed: [192.168.1.1] => {"changed": false, "choco_error_cmd": "choco.exe list --local-only chocolatey", "choco_error_log": "",
"failed": true} msg: Error checking installation status for chocolatey
FATAL: all hosts have already failed -- aborting
我需要自動化安裝和配置工具,例如:jdk、tomcat、firefox 等。以下是我的劇本示例:
---
- hosts: windows
vars:
java:
JAVA_HOME: "C:\\Program Files\\Java\\jdk1.7.0_76"
tasks:
# INSTALL FIREFOX
- name: install_firefox
win_chocolatey:
name: firefox -y
state: present
# INSTALL AND SET JAVA_HOME
- name: install_and_set_java_home
win_chocolatey:
name: jdk7 -y
version: 7.0.76
environment: java
state: present
我想我找到了解決方案..報告的問題是默認情況下編碼Ansible使用編碼和語言為ansible.cfg
...
module_lang = C
...
但是您可以覆蓋它以將特定變量托管為
ansible_module_lang=cp1252
ansible_ssh_port=5986
ansible_connection=winrm
這將解決您的問題
問題是目標計算機在每個PowerShell進程中不允許足夠的內存。
您可以通過運行以下命令檢查當前分配:
get-item wsman:localhost\Shell\MaxMemoryPerShellMB
默認情況下為300MB。 對於Ansible受管機器,我通常將其設置為2GB,這樣可以立即清除System.OutOfMemoryException
周圍的錯誤。
這可以直接在主機上完成,也可以通過Ansible完成:
- name: set PowerShell memory allowance to 2GB
win_shell: set-item wsman:localhost\Shell\MaxMemoryPerShellMB 2048
您的命令中可能有一些錯誤的編碼。
Installing chocolatey on this machine
{
"changed": false,
"msg": "The term \u0027C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\chocolatey\\chocInstall\\tools\>\chocolateyInstall.ps1\u0027 is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.",
"failed": true
}
撇號被打印為Unicode文字
“無法識別術語\\ u0027 ... chocolateyInstall.ps1 \\ u0027 ...”
這只是Ansible正在做的事情嗎? 您能告訴我們Chocolatey安裝手冊部分/命令嗎?
就像,你從哪里得到的
# INSTALL FIREFOX
- name: install_firefox
win_chocolatey:
我沒有運行類似Chocolatey的安裝。
Powershell腳本win_chocolatey.ps1應該安裝軟件包(軟件包管理器)。
在安裝之前,它應該在Windows計算機上搜索Chocolatey安裝。 在win_chocolatey.ps1腳本的代碼下面:
#!powershell
# This file is part of Ansible
#
# Copyright 2014, Trond Hindenes <trond@hindenes.com>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
$ErrorActionPreference = "Stop"
# WANT_JSON
# POWERSHELL_COMMON
$params = Parse-Args $args;
$result = New-Object PSObject;
Set-Attr $result "changed" $false;
If ($params.name)
{
$package = $params.name
}
Else
{
Fail-Json $result "missing required argument: name"
}
If ($params.force)
{
$force = $params.force | ConvertTo-Bool
}
Else
{
$force = $false
}
If ($params.upgrade)
{
$upgrade = $params.upgrade | ConvertTo-Bool
}
Else
{
$upgrade = $false
}
If ($params.version)
{
$version = $params.version
}
Else
{
$version = $null
}
If ($params.source)
{
$source = $params.source.ToString().ToLower()
}
Else
{
$source = $null
}
If ($params.showlog)
{
$showlog = $params.showlog | ConvertTo-Bool
}
Else
{
$showlog = $null
}
If ($params.state)
{
$state = $params.state.ToString().ToLower()
If (($state -ne "present") -and ($state -ne "absent"))
{
Fail-Json $result "state is $state; must be present or absent"
}
}
Else
{
$state = "present"
}
Function Chocolatey-Install-Upgrade
{
[CmdletBinding()]
param()
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0
if ($ChocoAlreadyInstalled -eq $null)
{
#We need to install chocolatey
iex ((new-object net.webclient).DownloadString("https://chocolatey.org/install.ps1"))
$result.changed = $true
$script:executable = "C:\ProgramData\chocolatey\bin\choco.exe"
}
else
{
$script:executable = "choco.exe"
if ((choco --version) -lt '0.9.9')
{
Choco-Upgrade chocolatey
}
}
}
Function Choco-IsInstalled
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=1)]
[string]$package
)
$cmd = "$executable list --local-only $package"
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$LastExitCode"
Throw "Error checking installation status for $package"
}
If ("$results" -match " $package .* (\d+) packages installed.")
{
return $matches[1] -gt 0
}
$false
}
Function Choco-Upgrade
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=1)]
[string]$package,
[Parameter(Mandatory=$false, Position=2)]
[string]$version,
[Parameter(Mandatory=$false, Position=3)]
[string]$source,
[Parameter(Mandatory=$false, Position=4)]
[bool]$force
)
if (-not (Choco-IsInstalled $package))
{
throw "$package is not installed, you cannot upgrade"
}
$cmd = "$executable upgrade -dv -y $package"
if ($version)
{
$cmd += " -version $version"
}
if ($source)
{
$cmd += " -source $source"
}
if ($force)
{
$cmd += " -force"
}
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error installing $package"
}
if ("$results" -match ' upgraded (\d+)/\d+ package\(s\)\. ')
{
if ($matches[1] -gt 0)
{
$result.changed = $true
}
}
}
Function Choco-Install
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=1)]
[string]$package,
[Parameter(Mandatory=$false, Position=2)]
[string]$version,
[Parameter(Mandatory=$false, Position=3)]
[string]$source,
[Parameter(Mandatory=$false, Position=4)]
[bool]$force,
[Parameter(Mandatory=$false, Position=5)]
[bool]$upgrade
)
if (Choco-IsInstalled $package)
{
if ($upgrade)
{
Choco-Upgrade -package $package -version $version -source $source -force $force
}
return
}
$cmd = "$executable install -dv -y $package"
if ($version)
{
$cmd += " -version $version"
}
if ($source)
{
$cmd += " -source $source"
}
if ($force)
{
$cmd += " -force"
}
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error installing $package"
}
$result.changed = $true
}
Function Choco-Uninstall
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=1)]
[string]$package,
[Parameter(Mandatory=$false, Position=2)]
[string]$version,
[Parameter(Mandatory=$false, Position=3)]
[bool]$force
)
if (-not (Choco-IsInstalled $package))
{
return
}
$cmd = "$executable uninstall -dv -y $package"
if ($version)
{
$cmd += " -version $version"
}
if ($force)
{
$cmd += " -force"
}
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error uninstalling $package"
}
$result.changed = $true
}
Try
{
Chocolatey-Install-Upgrade
if ($state -eq "present")
{
Choco-Install -package $package -version $version -source $source `
-force $force -upgrade $upgrade
}
else
{
Choco-Uninstall -package $package -version $version -force $force
}
Exit-Json $result;
}
Catch
{
Fail-Json $result $_.Exception.Message
}
如您所見,如果在計算機上找不到它,請安裝Chocolatey。
可從https://chocolatey.org/install.ps1下載用於安裝Chocolatey的Powershell腳本
還有一件事... Chocolatey安裝日志中有一個例外
2015-06-17 01:13:40,156 [ERROR]-反序列化Chocolatey.infrastructure.app.configuration.ConfigFileSettings類型的響應時出錯:引發了'System.OutOfMemoryException'類型的異常。
2015-06-17 01:13:40,187 [錯誤]-引發了類型為System.OutOfMemoryException的異常。
我發現應該使用的地方:
winrm設置winrm / config / winrs @ {MaxMemoryPerShellMB =“ MemoryInMB”}
但這對安裝沒有幫助
它需要具有已安裝的Windows Management Framework 3.0的Windows Server 2008 R2 SP1。
我已經執行了一些步驟來運行它。
現在,在清晰的系統第一次運行的劇本上運行后,我遇到了錯誤(在安裝firefox時):
failed: [192.168.1.117] => {"failed": true, "parsed": false}
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2015-06-17 15:32 chocInstall
Downloading https://chocolatey.org/api/v2/package/chocolatey/ to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip
Download 7Zip commandline tool
Downloading https://chocolatey.org/7za.exe to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\7za.exe
Extracting C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall...
Installing chocolatey on this machine
{
"changed": false,
"msg": "The term \u0027C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\chocolatey\\chocInstall\\tools\\chocolateyInstall.ps1\u0027 is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.",
"failed": true
}
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Processing archive: C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip
Extracting _rels\.rels
Extracting chocolatey.nuspec
Extracting tools\chocolateyInstall.ps1
Extracting tools\chocolateysetup.psm1
Extracting tools\init.ps1
Extracting tools\chocolateyInstall\choco.exe
Extracting tools\chocolateyInstall\choco.exe.ignore
Extracting package\services\metadata\core-properties\61804721eec44e8592a61904d0a62022.psmdcp
Extracting [Content_Types].xml
Everything is Ok
Files: 9
Size: 3738621
Compressed: 1259522
FATAL: all hosts have already failed -- aborting
第二次運行后,安裝tomcat一切正常。 錯誤:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/connection_plugins/winrm.py", line 161, in exec_command
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/connection_plugins/winrm.py", line 122, in _winrm_exec
self.shell_id = self.protocol.open_shell()
File "/usr/local/lib/python2.7/dist-packages/winrm/protocol.py", line 118, in open_shell
rs = self.send_message(xmltodict.unparse(rq))
File "/usr/local/lib/python2.7/dist-packages/winrm/protocol.py", line 190, in send_message
return self.transport.send_message(message)
File "/usr/local/lib/python2.7/dist-packages/winrm/transport.py", line 110, in send_message
raise WinRMTransportError('http', error_message)
WinRMTransportError: 500 WinRMTransport. Bad HTTP response returned from server. Code 500
fatal: [192.168.1.117] => failed to exec cmd PowerShell -NoProfile -NonInteractive -EncodedCommand KABOAGUAdwAtAEkAdABlAG0AIAAtAFQAeQBwAGUAIABEAGkAcgBlAGMAdABvAHIAeQAgAC0AUABhAHQAaAAgACQAZQBuAHYAOgB0AGUAbQBwACAALQBOAGEAbQBlACAAIgBhAG4AcwBpAGIAbABlAC0AdABtAHAALQAxADQAMwA0ADUANAA4ADAAOQAyAC4ANgAtADEANgAzADkANAAwADEAMwA4ADUAMAAwADIAMQAzACIAKQAuAEYAdQBsAGwATgBhAG0AZQAgAHwAIABXAHIAaQB0AGUALQBIAG8AcwB0ACAALQBTAGUAcABhAHIAYQB0AG8AcgAgACcAJwA7AA==
FATAL: all hosts have already failed -- aborting
第三次運行可以,沒有錯誤,但是我對這種解決方案不滿意。 我正在嘗試修復它,所以如果有人知道解決方案,請幫助我:)
謝謝
承認是很愚蠢的,但是如此簡單的解決方案(如更改安裝順序)有助於解決第二次運行錯誤。
我認為JAVA_HOME系統變量存在問題(我先安裝了jdk 7並為其設置了變量),tomcat 7的巧克力安裝需要安裝Java 8(奇怪)並將系統變量設置為jdk8。
我的劇本現在安裝了tomcat,jdk7,將系統變量設置為jdk7(我正在嘗試使用powershell進行此操作),然后安裝其他東西。 我仍然遇到第一個錯誤(安裝Chocolatey時)
---
- hosts: windows
vars:
java:
JAVA_HOME: "C:\\Program Files\\Java\\jdk1.7.0_76"
tasks:
# INSTALL FIREFOX
- name: install_firefox
win_chocolatey:
name: firefox -y
state: present
# TOMCAT INSTALL
- name: install_tomcat
win_chocolatey:
name: tomcat -y
version: 7.0.59
state: present
# INSTALL AND SET JAVA_HOME
- name: install_and_set_java_home
win_chocolatey:
name: jdk7 -y
version: 7.0.76
environment: java
state: present
# STOP TOMCAT SERVICE
- name: tomcat_service_auto_stop
win_service:
name: Apache Tomcat 7.0 Tomcat7
start_mode: auto
state: stopped
...
...
...
# DOWNLOAD SERVER.XML FOR TOMCAT
- name: download_server_xml
win_get_url:
url: http://192.168.1.107:8000/server.xml
dest: C:\Program Files\Apache Software Foundation\tomcat\apache-tomcat-7.0.59\conf\server.xml
# DOWNLOAD SQL DRIVER
- name: download_sql_driver
win_get_url:
url: http://192.168.1.107:8000/sqljdbc4.jar
dest: C:\Program Files\Apache Software Foundation\tomcat\apache-tomcat-7.0.59\lib\sqljdbc4.jar
# OPEN PORT 80 FOR TOMCAT
- name: Open_Port_80_for_Tomcat
script: ../scripts/portsWin2008.ps1
# START TOMCAT SERVICE
- name: tomcat_service_start
win_service:
name: Apache Tomcat 7.0 Tomcat7
state: started
我已經修改了劇本,但現在只運行了一次,但是出現了關於choco安裝的錯誤(與之前相同)。 如果對某人有幫助,這里是忽略劇本失敗的解決方案:
---
- hosts: windows
tasks:
# INSTALL CHOCO
- name: install_chocolatey
win_chocolatey:
name: chocolatey -y
state: present
ignore_errors: yes
# INSTALL FIREFOX
- name: install_firefox
win_chocolatey:
name: firefox -y
state: present
.
.
.
我希望它會有所幫助;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.