Search code examples
powershellwindows-server-2008-r2ansiblechocolatey

Error installing Chocolatey via Ansible on Windows


I have a problem installing Chocolatey and Chocolatey packages using Ansible on Windows Server 2008 R2. Everything worked fine on Windows Server 2012 R2 (it has built-in PowerShell v3.0).

I had a problem running the PowerShell scripts in the Ansible documentation, but I ran Set-ExecutionPolicy RemoteSigned, which helped. I installed Windows PowerShell 3.0 so Ansible could run. Now, when I run the playbook, I have this error:

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

After second run, I have a different error:

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

I noticed that Ansible has a problem unpacking Chocolatey from %TEMP% to %PROGRAMDATA%. So, after running chocolateyInstall.ps1 from %TEMP%\chocolatey\helpers (I think it's good path) I have this error:

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

I need to automate the installation and configuration tools such as: jdk, tomcat, firefox, etc. Here is an example of my playbook:

---
- 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

Solution

  • I have modified my playbook, now I run it only once but I have the error (same as earlier) about choco installation. If it helps for someone here is the solution for ignoring failure in playbook:

    ---
    - 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
    .
    .
    .
    

    I hope it will help ;)