Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Refactor win_chocolatey module #398

Merged
merged 1 commit into from
Jun 11, 2015
Merged

Conversation

elventear
Copy link
Contributor

With the latest changes the win_chocolatey does not work at all starting with Chocolatey version 0.9.9+.

Provided changes:

  • Refactor code to be more robust. Run main logic inside a try {} catch {}
    block. If there is any error, bail out and log all the command output
    automatically.
  • Rely on error code generated by chocolatey instead of scraping text
    output to determine success/failure.
  • Add support for unattended installs: (-y flag is a requirement by
    chocolatey)
  • Before (un)installing, check existence of files.
  • Use functions to abstract logic
  • The great rewrite of 0.9.9, the choco interface has changed, check
    if chocolatey is installed and an older version. If so upgrade to
    latest.
  • Allow upgrading packages that are already installed
  • Use verbose logging for chocolate actions
  • Adding functionality to specify a source for a chocolatey repository.
    (@smadam813)
  • Removing pre-determined sources and adding specified source url in
    it's place. (@smadam813)

For reasons behind removing built-in sources, see:

@elventear
Copy link
Contributor Author

If anyone can provide further testing and feedback for this PR, I would appreciate it.
@trondhindenes
@petemounce

@arBmind
Copy link

arBmind commented Apr 18, 2015

+1 This seems much more stable than the old version.

@bcomnes
Copy link

bcomnes commented Apr 19, 2015

I'm going to try this out. The current add-on gets chocolatey installed, but it totally hangs on dialog.

@petemounce
Copy link
Contributor

The module does not peg to a particular version of chocolatey. It's
possible chocolatey has changed to include an interactive prompt since I
wrote the module. I'll check that, unless someone does first.

There are also logs written to c:\

Sent from my phone. Please excuse typos and brevity, but never text speak.
On 19 Apr 2015 19:45, "Bret Comnes" [email protected] wrote:

I'm going to try this out. The current add-on gets chocolatey installed,
but it totally hangs on dialog.


Reply to this email directly or view it on GitHub
#398 (comment)
.

@ryanwalls
Copy link

I'll try this out. If I place this module in the root of my project in a library folder, does it take precedence over the built-in "old" version? Of course, I can just check out the full branch if that's what everyone else does...

@petemounce
Copy link
Contributor

@ryanwalls yes, it will take precedence if you do that.

@jhawkesworth
Copy link
Contributor

It seems to depend on where your playbook is, unless you set the library
location specifically in your ansible.cfg

On Mon, Apr 20, 2015 at 3:28 PM, ryanwalls [email protected] wrote:

I'll try this out. If I place this module in the root of my project in a
library folder, does it take precedence over the built-in "old" version? Of
course, I can just check out the full branch if that's what everyone else
does...


Reply to this email directly or view it on GitHub
#398 (comment)
.

@ryanwalls
Copy link

FYI, using this module fixed the hanging issue for me.

@petemounce
Copy link
Contributor

@elventear I've used this successfully to create Windows 2012R2 AMIs (based on ami-5d62ff2a in eu-west-1). I used it to install-latest and install-specific-version, starting from a box that did not have chocolatey.

This has certainly fixed #391 (and made the module nicer to follow too).

Great stuff!

@petemounce petemounce mentioned this pull request Apr 30, 2015
@elventear
Copy link
Contributor Author

Thanks for all the help testing, guys.

else
{
Throw "source is $source - must be one of chocolatey, ruby, webpi or windowsfeatures."
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really should allow custom sources - not everyone is going to want to use the community feed.

@elventear
Copy link
Contributor Author

@ferventcoder thanks for the review. I will try to address those ASAP.

@smadam813
Copy link

@elventear I am very interested in getting this merged and will be using the chocolatey module a great deal. I will make a fork of your repo and try to do some of the leg work on peoples suggestions within this thread. Would that be of help? I don't want to step on something you are already working on.

@elventear
Copy link
Contributor Author

@smadam813 If you provide a nice interface for custom repos, would be nice.

@elventear
Copy link
Contributor Author

Updated with verbose logging, per @ferventcoder comments.

@smadam813
Copy link

@elventear Submitted a pull request to your branch which adds the ability to specify a custom chocolatey repo. #2

@gregdek
Copy link
Contributor

gregdek commented Jun 11, 2015

Thanks @petemounce for the review, and @elventear for the patch!

gregdek added a commit that referenced this pull request Jun 11, 2015
Refactor `win_chocolatey` module
@gregdek gregdek merged commit 78ffe43 into ansible:devel Jun 11, 2015
@elventear
Copy link
Contributor Author

🎉 🎈

@petemounce
Copy link
Contributor

Nice work :-)

Sent from my phone. Please excuse typos and brevity, but never text speak.
On 11 Jun 2015 18:36, "Pepe Barbe" [email protected] wrote:

:party:


Reply to this email directly or view it on GitHub
#398 (comment)
.

@smadam813
Copy link

Just curious, I am not listed as a contributor for this code as my commits do not show up after the merge. Could I be added @gregdek ?

@elventear
Copy link
Contributor Author

@smadam813 you are referenced in the author section of the module doc and in the commit message.

In order to commit I had to rebase and there were too many commits; to make my life easier I had to squash all the commits into a single commit.

@smadam813
Copy link

@elventear No problem! I am new to git so was just wondering. I have many more modules I am working on that I will make pull requests for soon so not worried at all. Thanks!

@elventear
Copy link
Contributor Author

It seems chocolatey is broken again when running from ansible. Can anyone test a freshly installed environment to see if it works for them?

In my tests, the script is fetching version 0.9.9.8 and the error I am getting is:

 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

If I leave the ansible script behind and execute it manually it completes successfully.

@ferventcoder have there been changes in Chocolatey that could trigger this issue?

@ferventcoder
Copy link

@elventear Choco does require and attempts to install .NET Framework 4.0. If it is not successful, you may not get the files unpacked (the chocolateyInstaller.psm1 is unpacked when you call choco unpackself, but requires .net 4.0+ to be functional on the machine).

@ferventcoder
Copy link

@elventear No recent changes with the installer though that would have triggered something that wasn't already possible though.

@ferventcoder
Copy link

It might be better to have choco unpack itself just before it gets packaged up. That way when there are issues, it is more noticeable what it causing the issues (non-functional .NET 4.0 install) versus not finding some file.

@elventear
Copy link
Contributor Author

@ferventcoder is that something that the chocolatey install process should do? Or are you suggesting that ansible do that?

@ferventcoder
Copy link

Something choco should do, filed chocolatey/choco#347

@elventear
Copy link
Contributor Author

Is there a way to get an older version of chocolatey?

@ferventcoder
Copy link

@elventear it's not a new issue. You are likely running into it the first time. Unless I'm missing something. If you have the logs from the install, I would be curious to see.

@ferventcoder
Copy link

@elventear unless I'm mistaken, it is literally the issue of not having a functioning .NET 4+ environment when choco 0.9.9+ attempts to install. Because of the way it unpacks itself, it requires .net 4 to be there to create all of the powershell files.

@smadam813
Copy link

Strange, I was just using this today bootstrapping new servers with no issues. Granted they were WIndows 2012 and come with .NET 4.5 on them.

@Chup4Chups
Copy link

I am using ansible 1.9.2 with win_chocolatey module (v 0.9.9.8) to deploy jdk8 and other programs on a Windows Server 2012 R2 (include .NET 4.5).
I am not installing chocolatey manually, win_chocolatey do it for me the first time.
I am still facing the issue related there: getting fail response, but had successfully executed. #274

Here is my playbook :

---
- hosts: windows-test
  vars:
    java:
  tasks:
    - name: Packages from Chocolatey
      tags: [windows,common,chocolatey,packages]
      win_chocolatey: 
        name: "{{ item.name }} -y"
        state: "present"
      with_items:
        - { name: "7zip.install" }
        - { name: "notepad2-mod" }
        - { name: "jdk8" }

And here is my output with verbose enabled :

TASK: [Packages from Chocolatey] ********************************************** 
<mywindows.domain.com> ESTABLISH WINRM CONNECTION FOR USER: Ansible on PORT 5986 TO mywindows.domain.com
<mywindows.domain.com> REMOTE_MODULE win_chocolatey name='7zip.install -y' state=present
<mywindows.domain.com> EXEC (New-Item -Type Directory -Path $env:temp -Name "ansible-tmp-1435743347.05-121401074477777").FullName | Write-Host -Separator '';
<mywindows.domain.com> PUT /tmp/tmp73cnwp TO C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743347.05-121401074477777\\win_chocolatey
<mywindows.domain.com> PUT /tmp/tmpbt9zbq TO C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743347.05-121401074477777\\arguments
<mywindows.domain.com> EXEC PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743347.05-121401074477777\\win_chocolatey.ps1 C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743347.05-121401074477777\\arguments; Remove-Item "C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743347.05-121401074477777\" -Force -Recurse;
failed: [mywindows.domain.com] => (item={'name': '7zip.install'}) => {"changed": false, "chocolatey command": "choco.exe install 7zip.install -y -source https://chocolatey.org/api/v2/", "failed": true, "item": {"name": "7zip.install"}}
msg: Install error: Chocolatey v0.9.9.8 Installing the following packages: 7zip.install By installing you accept licenses for the packages.  7zip.install v9.38  WARNING: This installer is known to close the explorer process. This means   you may lose current work.   If it doesn't automatically restart explorer, type 'explorer' on the   command shell to restart it.  Installing 7zip.install...  7zip.install has been installed.  The install of 7zip.install was successful.  Chocolatey installed 1/1 package(s). 0 package(s) failed.  See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
<mywindows.domain.com> ESTABLISH WINRM CONNECTION FOR USER: Ansible on PORT 5986 TO mywindows.domain.com
<mywindows.domain.com> REMOTE_MODULE win_chocolatey name='notepad2-mod -y' state=present
<mywindows.domain.com> EXEC (New-Item -Type Directory -Path $env:temp -Name "ansible-tmp-1435743356.3-209680130146115").FullName | Write-Host -Separator '';
<mywindows.domain.com> PUT /tmp/tmpOQ6jrC TO C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743356.3-209680130146115\\win_chocolatey
<mywindows.domain.com> PUT /tmp/tmpsjD7GY TO C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743356.3-209680130146115\\arguments
<mywindows.domain.com> EXEC PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743356.3-209680130146115\\win_chocolatey.ps1 C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743356.3-209680130146115\\arguments; Remove-Item "C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743356.3-209680130146115\" -Force -Recurse;
failed: [mywindows.domain.com] => (item={'name': 'notepad2-mod'}) => {"changed": false, "chocolatey command": "choco.exe install notepad2-mod -y -source https://chocolatey.org/api/v2/", "failed": true, "item": {"name": "notepad2-mod"}}
msg: Install error: Chocolatey v0.9.9.8 Installing the following packages: notepad2-mod By installing you accept licenses for the packages.  notepad2-mod v4.2.25.940  Installing notepad2-mod...  notepad2-mod has been installed.  The install of notepad2-mod was successful.  Chocolatey installed 1/1 package(s). 0 package(s) failed.  See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
<mywindows.domain.com> ESTABLISH WINRM CONNECTION FOR USER: Ansible on PORT 5986 TO mywindows.domain.com
<mywindows.domain.com> REMOTE_MODULE win_chocolatey name='jdk8 -y' state=present
<mywindows.domain.com> EXEC (New-Item -Type Directory -Path $env:temp -Name "ansible-tmp-1435743366.25-64044330299967").FullName | Write-Host -Separator '';
<mywindows.domain.com> PUT /tmp/tmpxFWrK0 TO C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743366.25-64044330299967\\win_chocolatey
<mywindows.domain.com> PUT /tmp/tmpeTh63m TO C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743366.25-64044330299967\\arguments
<mywindows.domain.com> EXEC PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743366.25-64044330299967\\win_chocolatey.ps1 C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743366.25-64044330299967\\arguments; Remove-Item "C:\Users\Ansible\AppData\Local\Temp\ansible-tmp-1435743366.25-64044330299967\" -Force -Recurse;
failed: [mywindows.domain.com] => (item={'name': 'jdk8'}) => {"changed": false, "chocolatey command": "choco.exe install jdk8 -y -source https://chocolatey.org/api/v2/", "failed": true, "item": {"name": "jdk8"}}
msg: Install error: Chocolatey v0.9.9.8 Installing the following packages: jdk8 By installing you accept licenses for the packages.  jdk8 v8.0.45  Downloading JDK from http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-windows-x64.exe  Installing jdk8...  jdk8 has been installed.  PATH environment variable does not have C:\Program Files\Java\jdk1.8.0_45\bin in it. Adding...  WARNING: Write-ChocolateySuccess is deprecated. If you are the maintainer,   please remove it from your package file.  The install of jdk8 was successful.  Chocolatey installed 1/1 package(s). 0 package(s) failed.  See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

FATAL: all hosts have already failed -- aborting

chocolatey logs always indicate me 0 packages failed and Exiting with 0.

I thought it has been fixed, did I miss something ?

@smadam813
Copy link

@Chup4Chups you should no longer need -y when running chocolatey commands as this is the new default. Just out of curiosity, this sounds like the behavior from the old chocolatey module. Try adding branch = devel to each submodule under .gitsubmodule config. Then run "git submodule update --remote --rebase --recursive". See if this fixes your problem.

@Chup4Chups
Copy link

@smadam813 I pulled the devel branch and unfortunately, I have now a weird error I can't solve :

sysadmin@ansibletest:~/ansible-recipes$ ansible -vvvv windows -m win_ping
<mywindows.domain.com> ESTABLISH WINRM CONNECTION FOR USER: Ansible on PORT 22 TO mywindows.domain.com
mywindows.domain.com | FAILED! => {'msg': u'ERROR! 500 WinRMTransport. [Errno 111] Connection refused', 'failed': True}
sysadmin@ansibletest:~/ansible-recipes$ cat group_vars/windows 
---
ansible_ssh_user: Ansible
ansible_ssh_pass: mypass
ansible_ssh_port: 5986
ansible_connection: winrm

Didn't find any issue about this :s
Do you know if group vars name has been modified recently ? it detects I want to use winrm but not the ansible_ssh_port since it's trying 22 instead of 5986.

@trondhindenes
Copy link
Contributor

Probably the same issue as here:
ansible/ansible#11278 (comment)
I'm assuming that if you source the v1 env-setup file everything is working?

@Chup4Chups
Copy link

Yep indeed, I missed this post.
I just temporary solved the issue 10min ago by adding mywindows.domain.com:5986 on top of my ./production file.

@trondhindenes
Copy link
Contributor

It is definetely a bug. Interesting that Ansible figures out that it should
use winrm but misses the correct port since both of those are specified in
the same variable file/group.

On Wed, Jul 1, 2015 at 7:17 PM, Chup4Chups [email protected] wrote:

Yep indeed, I missed this post.
I just temporary solved the issue 10min ago by adding
mywindows.domain.com:5986 on top of my ./production file.


Reply to this email directly or view it on GitHub
#398 (comment)
.

@elventear
Copy link
Contributor Author

So I had to hack things around to try to get a meaningful message of what my problem is and I discovered that I was getting this exception System.OutOfMemoryException.

It seems this is a known issue and is related to this fix:

https://support.microsoft.com/en-us/kb/2842230

Anyone else has run into the same issue? Should the ansible windows docs mention that KB?

@ferventcoder
Copy link

WinRM bump up to 800MB I think fixes it. I know some folks ran into this - offhand - chocolatey/choco#277

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.