在Windows上通过Ansible安装Chocolatey时出错

时间:2015-06-16 13:57:44

标签: powershell ansible windows-server-2008-r2 ansible-playbook chocolatey

我在使用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\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在从%TEMP%解压缩到%PROGRAMDATA%时遇到了问题。因此,在chocolateyInstall.ps1运行%TEMP%\chocolatey\helpers之后(我认为这是一条好路径)我有这样的错误:

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

7 个答案:

答案 0 :(得分:1)

我认为我找到了解决方案..报告的问题是编码 Ansible默认使用编码和语言 ansible.cfg

...
module_lang    = C
...

但你可以覆盖它以将特定变量托管为

ansible_module_lang=cp1252
ansible_ssh_port=5986
ansible_connection=winrm

这将解决您的问题

答案 1 :(得分:1)

问题是目标计算机每个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

答案 2 :(得分:0)

您的命令可能有一些错误的编码。

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

  

“无法识别...... \ chocolateyInstall.ps1 \ u0027 ......”

这只是Ansible正在做的事情吗?你能告诉我们Chocolatey安装playbook部分/命令吗?

喜欢,你从哪里得到这个

#   INSTALL FIREFOX
    - name: install_firefox
      win_chocolatey:

答案 3 :(得分:0)

我没有巧克力安装。

Powershell脚本win_chocolatey.ps1应该安装包(包管理器)。

在安装之前,它应该在Windows机器上搜索巧克力安装。在win_chocolatey.ps1脚本的代码下面:

// This code is C++11 but it's not essential to the problem.
// The current code calls copy constructors more than necessary.
#include <map>

#include <cassert>


template<class K, class V>
struct zero_map
{
    struct proxy
    {
        std::map<K, V> *container;
        K key;

        operator V()
        {
            auto it = container->find(key);
            if (it == container->end())
                return V();
            return *it;
        }
        void operator = (V value)
        {
            if (value == V())
            {
                container->erase(key);
            }
            else
            {
                // probably should use .insert() and conditionally assign
                (*container)[key] = value;
            }
        }
    };

    std::map<K, V> _inner;

    proxy operator[](K k)
    {
        return proxy{&_inner, k};
    }
};

int main()
{
    zero_map<int, int> foo;
    assert (foo._inner.size() == 0);
    foo[1] = 0;
    assert (foo._inner.size() == 0);
    foo[0] = 1;
    assert (foo._inner.size() == 1);
    foo[0] = 0;
    assert (foo._inner.size() == 0);
}

如你所见,如果在机器上找不到巧克力,就会安装巧克力。

https://chocolatey.org/install.ps1

下载用于安装chocolatey的Powershell脚本

还有一件事......在巧克力安装日志中有一个例外

  

2015-06-17 01:13:40,156 [错误] - 反序列化类型的响应时出错   chocolatey.infrastructure.app.configuration.ConfigFileSettings:   抛出了类型'System.OutOfMemoryException'的异常。

     

2015-06-17 01:13:40,187 [错误] - 抛出了System.OutOfMemoryException类型的异常。

我发现了我应该使用的地方:

  

winrm set winrm / config / winrs @ {MaxMemoryPerShellMB =“MemoryInMB”}

但它无法帮助安装

答案 4 :(得分:0)

它需要安装了Windows Management Framework 3.0的Windows Server 2008 R2 SP1。

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

  1. 从Microsoft网站
  2. 在Windows Server 2008上安装.NET Framework 4.0
  3. 在PowerShell窗口中执行的命令:Set-ExecutionPolicy RemoteSigned
  4. 从Microsoft站点安装Windows Management Framework 3.0
  5. 来自ansible文档的已安装更新:http://support.microsoft.com/kb/2842230
  6. 从ansible文档执行系统准备脚本:https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
  7. 现在,在清除系统上第一次运行playbook后我遇到错误(安装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
    

    第三次运行没问题,没有错误,但我对此解决方案不满意。我试图修复它,所以如果有人知道解决方案,请帮助我:)。

    由于

答案 5 :(得分:0)

承认但是如此简单的解决方案,因为改变安装顺序有助于第二次运行错误,这是愚蠢的。

我认为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

答案 6 :(得分:0)

我修改了我的剧本,现在我只运行一次,但我有关于choco安装的错误(与之前相同)。如果这对某人有帮助就是忽略剧本中失败的解决方案:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {

    String[] languages = { "C","C++","Java","C#","PHP","JavaScript","jQuery","AJAX","JSON" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                //Create Array Adapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, languages);
                //Find TextView control
        AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.languages);
        //Set the number of characters the user must type before the drop down list is shown
                acTextView.setThreshold(1);
                //Set the adapter
        acTextView.setAdapter(adapter);
    }
}

我希望它会有所帮助;)