# Windows package management via chocolatey

**URL:** <https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986>\
**Category:** Ansible Developer\
**Tags:** windows, aws\
**Created:** [August 19, 2014, 10:07pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986 "2014-08-19T22:07:53Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 19, 2014, 10:07pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/1 "2014-08-19T22:07:53Z")

</div>

Hi there

I’ve been using Ansible fruitfully to provision a windows 2012 server in AWS, so I can use that as the basis for baking an AMI and moving my environment closer towards immutable infrastructure.

In the doing, I’ve been using the chocolatey package manager.

I opened a stub PR containing the documentation for a module that would wrap up the use of chocolatey - [https://github.com/ansible/ansible/pull/8685](https://github.com/ansible/ansible/pull/8685) has that.

Welcome addition / waste of time?

Note that chocolatey will be replaced by oneget once powershell v5 comes out. The only expected difference is that instead of `choco install thing` one runs powershell `install-package thing` - mostly.

I’ve not created a module before - any steers that people can give me in terms of how to automate testing for it (given the windows context, I imagine that might be trickier) would be welcome.

Regards  
Pete

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [August 20, 2014, 3:51pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/2 "2014-08-20T15:51:21Z")

</div>

Look at the existing Windows modules (e.g. [https://github.com/ansible/ansible/blob/devel/library/windows/win\_msi.ps1](https://github.com/ansible/ansible/blob/devel/library/windows/win_msi.ps1)) and tests ([https://github.com/ansible/ansible/blob/devel/test/integration/roles/test\_win\_msi/tasks/main.yml](https://github.com/ansible/ansible/blob/devel/test/integration/roles/test_win_msi/tasks/main.yml)) as a starting point.

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 20, 2014, 4:19pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/3 "2014-08-20T16:19:30Z")

</div>

Thanks for the pointer; will start from there (again, assuming the feature is something that would be welcome).

Thanks also for the powershell module utility - had wondered where Parse-Args etc came from. There’re a couple of things I might add to that.

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [August 20, 2014, 4:32pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/4 "2014-08-20T16:32:21Z")

</div>

You may also want to touch base with Trond. An earlier ansible-devel post indicated he was working on a chocolatey module ([https://groups.google.com/forum/#!topic/ansible-devel/xgyfOdAUIrQ](https://groups.google.com/forum/#!topic/ansible-devel/xgyfOdAUIrQ)), but I haven’t seen any pull requests submitted for it yet.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 20, 2014, 6:33pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/5 "2014-08-20T18:33:09Z")

</div>

Correct, I started working on one. I don’t know if its ready for a pr yet, I’ll have to do some more testing.

Peter, would you be interested in taking a look? You can grab the two files off of my fork at [https://github.com/trondhindenes/ansible/tree/win\_chocolatey](https://github.com/trondhindenes/ansible/tree/win_chocolatey)

The chocolatey commands are a bit tricky because the outout text and not objects, so I’ve made an attempt at some rudimentary text parsing to get results.

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 21, 2014, 10:15am UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/6 "2014-08-21T10:15:42Z")

</div>

Trond - definitely. Yours is further along than my local attempt; happy to test out yours. Want me to PR into yours if necessary?

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 21, 2014, 10:44am UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/7 "2014-08-21T10:44:07Z")

</div>

Yup, that’s fine. I don’t see a point in PR-ing it into source before we’re at least semi-confident that we have working code.

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 26, 2014, 7:59pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/8 "2014-08-26T19:59:16Z")

</div>

Having forked from ansible/ansible - your fork doesn’t appear in my list of possibilities when creating a PR. Any idea how I can solve that? Or, can you add me as a collaborator to yours and I can push directly to a branch of win\_chocolatey there?

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 26, 2014, 8:21pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/9 "2014-08-26T20:21:04Z")

</div>

Hm, it might be better for you to actually own it if you plan on doing some work on it. I was just goofing around a bit and got kinda thrown off track when I realized that there would be so much string parsing involved. I don’t mind if you just take the module and maintain/PR it from your fork.

Or if you’d rather, I can give you push access to my fork, up to you.

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 26, 2014, 8:25pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/10 "2014-08-26T20:25:54Z")

</div>

That sounds ok too.

Am I right to think that I can try this out as a local module in a playbook I’m writing by creating a library directory in my playbook’s root directory, then a directory called win\_chocolatey in that, and place the two win\_chocolatey files in there…?

So,

/  
/group\_vars  
/library  
/win\_chocolatey  
/win\_chocolatey \<\< python doc  
/win\_chocolatey.ps1 \<\< powershell module  
/roles

?

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 26, 2014, 8:31pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/11 "2014-08-26T20:31:41Z")

</div>

Good question. I was meaning to ask Michael if we could have a “debug” option to ease the testing on PowerShell modules.

I usually just copy the module I’m testing to ansible/library/windows and create some arbitrary test playbook when I’m goofing around. There might be better ways. And you only need the .ps1 file, the other one is just for documentation.

Let me know if you need anything more detailed, and I’d be happy to help you get setup.

-Trond

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 26, 2014, 8:37pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/12 "2014-08-26T20:37:07Z")

</div>

That sounds ok too; I’ll try both and see which works for me. It’d be great to know how to iterate on windows modules effectively; perhaps I’ll drop a README.md into that directory with some guidance as I go.

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [August 29, 2014, 3:19am UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/13 "2014-08-29T03:19:08Z")

</div>

I try to write integration tests as my example playbooks while I'm creating  
or updating a module. My usual steps go something like:

- Copy test/integration/inventory.winrm.example to inventory.winrm and add  
my test hosts  
- Create a test role with tasks in  
test/integration/roles/test\_win\_foo/tasks/default.yml  
- Edit test/integration/test\_winrm.yml to add my test\_win\_foo role  
- Create/update module code in library/windows/win\_foo.ps1, docs in  
library/windows/win\_foo  
- Run tests for my role: cd test/integration; TEST\_FLAGS="-vvvv -t  
test\_win\_foo" make test\_winrm  
- Add --limit host to TEST\_FLAGS to target only a single host  
- Temporarily add tags to selected tasks and use -t my\_tag while I'm  
debugging/testing specific tasks  
- Use --start-at-task='task name' to skip tasks I don't need to run each  
time  
- Repeat until the cows come home

There's no requirement to include any tests when you submit a module, but  
it can help in getting a pull request reviewed and merged sooner.

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [August 29, 2014, 8:15am UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/14 "2014-08-29T08:15:30Z")

</div>

Gotcha; thanks.

As regards running tests when I add those - I’m going to assume that you guys have Windows boxes to run tests on in a cloud or similar? I’ve been iterating on this via the latest Windows 2012 R2 AWS AMI (2014.08.13, ami-6abd621d in eu-west-1) as part of some at-work work.

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [September 12, 2014, 8:41am UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/15 "2014-09-12T08:41:08Z")

</div>

Hey - made some progress while baking AMIs using ansible. PR opened at [https://github.com/ansible/ansible/pull/8981](https://github.com/ansible/ansible/pull/8981) includes functional module and a doc file.

In terms of developing and iterating it, I have my playbook and I was able to successfully write the module inline to my playbook (exactly as the docs indicate, yay!) by creating a structure like this

/  
/group\_vars  
/library  
/win\_chocolatey  
/win\_chocolatey.ps1

/roles  
/site.yml

It would be really handy, by the way, if ansible-playbook had a mode where it would ship the scripts and modules to the windows node, execute them, but then _not_ immediately delete them after execution, so that one can manually look at what arrived. Does that exist?

Cheers  
Pete

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 12, 2014, 11:52am UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/16 "2014-09-12T11:52:11Z")

</div>

That does exist. See the tip in [http://docs.ansible.com/developing\_modules.html#building-testing](http://docs.ansible.com/developing_modules.html#building-testing)

---

<div class="post-metadata">

**Author:** ![Peter\_Mounce](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Peter\_Mounce](https://forum.ansible.com/u/Peter_Mounce)\
**Post date:** [September 12, 2014, 2:31pm UTC](https://forum.ansible.com/t/windows-package-management-via-chocolatey/15986/17 "2014-09-12T14:31:53Z")

</div>

Thank you!
