繁体   English   中英

在 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 文档中运行 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。

我已经执行了一些步骤来运行它。

  1. 从Microsoft网站在Windows Server 2008上安装了.NET Framework 4.0
  2. 在Powershell窗口中执行的命令:Set-ExecutionPolicy RemoteSigned
  3. 从Microsoft网站安装了Windows Management Framework 3.0
  4. 通过Ansible文档安装的更新: http : //support.microsoft.com/kb/2842230
  5. Ansible文档中已执行的系统准备脚本: https : //github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

现在,在清晰的系统第一次运行的剧本上运行后,我遇到了错误(在安装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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM