在 Windows 上通过 Ansible 安装 Chocolatey 时出错
Error installing Chocolatey via Ansible on Windows
我在 Windows Server 2008 R2 上使用 Ansible 安装 Chocolatey 和 Chocolatey 包时遇到问题。在 Windows Server 2012 R2(内置 PowerShell v3.0)上一切正常。
我 运行 在 Ansible documentation 中使用 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\chocInstallza.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-properties804721eec44e8592a61904d0a62022.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
运行ning 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
您的命令中可能有一些错误的编码。
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 literals
"The term \u0027...chocolateyInstall.ps1\u0027 is not recognized..."
这只是 Ansible 正在做的事情吗?您能向我们展示 Chocolatey 安装手册 section/command 吗?
比如,你从哪里得到这个
# INSTALL FIREFOX
- name: install_firefox
win_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 的 Powershell 脚本是从 https://chocolatey.org/install.ps1
下载的
还有一件事......在巧克力安装日志中有一个例外
2015-06-17 01:13:40,156 [ERROR] - Error deserializing response of type
chocolatey.infrastructure.app.configuration.ConfigFileSettings:
Exception of type 'System.OutOfMemoryException' was thrown.
2015-06-17 01:13:40,187 [ERROR] - Exception of type System.OutOfMemoryException' was thrown.
我找到了我应该使用的地方:
winrm set winrm/config/winrs @{MaxMemoryPerShellMB="MemoryInMB"}
但对安装没有帮助
承认是愚蠢的,但改变安装顺序这样简单的解决方案有助于解决第二个 运行 错误。
我认为 JAVA_HOME 系统变量有问题(我先安装了 jdk 7 并为其设置了变量),巧克力安装 tomcat 7 需要安装 java 8(奇怪)并将系统变量设置为 jdk8.
我的 playbook 现在安装 tomcat、jdk7,将系统变量设置为 jdk7(我正在尝试使用 powershell 执行此操作),然后安装其他东西。我仍然有第一个错误(在安装巧克力时)
---
- 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
我已经修改了我的 playbook,现在我只 运行 它一次,但我有关于 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
.
.
.
希望对您有所帮助 ;)
我想我找到了解决方案..报告的问题与编码有关
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
我在 Windows Server 2008 R2 上使用 Ansible 安装 Chocolatey 和 Chocolatey 包时遇到问题。在 Windows Server 2012 R2(内置 PowerShell v3.0)上一切正常。
我 运行 在 Ansible documentation 中使用 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\chocInstallza.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-properties804721eec44e8592a61904d0a62022.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
运行ning 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
您的命令中可能有一些错误的编码。
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 literals
"The term \u0027...chocolateyInstall.ps1\u0027 is not recognized..."
这只是 Ansible 正在做的事情吗?您能向我们展示 Chocolatey 安装手册 section/command 吗?
比如,你从哪里得到这个
# INSTALL FIREFOX
- name: install_firefox
win_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 的 Powershell 脚本是从 https://chocolatey.org/install.ps1
下载的还有一件事......在巧克力安装日志中有一个例外
2015-06-17 01:13:40,156 [ERROR] - Error deserializing response of type chocolatey.infrastructure.app.configuration.ConfigFileSettings: Exception of type 'System.OutOfMemoryException' was thrown.
2015-06-17 01:13:40,187 [ERROR] - Exception of type System.OutOfMemoryException' was thrown.
我找到了我应该使用的地方:
winrm set winrm/config/winrs @{MaxMemoryPerShellMB="MemoryInMB"}
但对安装没有帮助
承认是愚蠢的,但改变安装顺序这样简单的解决方案有助于解决第二个 运行 错误。
我认为 JAVA_HOME 系统变量有问题(我先安装了 jdk 7 并为其设置了变量),巧克力安装 tomcat 7 需要安装 java 8(奇怪)并将系统变量设置为 jdk8.
我的 playbook 现在安装 tomcat、jdk7,将系统变量设置为 jdk7(我正在尝试使用 powershell 执行此操作),然后安装其他东西。我仍然有第一个错误(在安装巧克力时)
---
- 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
我已经修改了我的 playbook,现在我只 运行 它一次,但我有关于 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
.
.
.
希望对您有所帮助 ;)
我想我找到了解决方案..报告的问题与编码有关 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