By this time most of the readers on this blog know I am an automation junkie and Ansible is my dealer. I started using Chocolatey about 2 years ago as it is an efficient open-source package manager for Windows operating systems similar to what brew is for Mac OS systems. In today's tutorial, I am going to cover how to use Chocolatey and Ansible together to manage desktop apps on Windows operating systems.
Prerequisite Initial Steps
The initial steps would be to have Ansible set up already and Chocolatey installed on your Windows machine. You can follow the tutorials below to set up Ansible and install Chocolatey:
Script To Set Ansible Connection To Windows Host Locally Via WSL
WSL also known as Windows Subsystem For Linux is a lightweight virtual machine that you can enable to run Linux on your Windows computer. Ensure that you set up WSL by following these steps:
Once WSL is installed and Ansible and Chocolatey are set up. Copy the Powershell script below and name it windows-host-setup.ps1
. Navigate to the directory and run this command to execute the setup that will allow you to access your Windows localhost through WSL - powershell.exe -ExecutionPolicy ByPass -File "windows-host-setup.ps1"
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
Set Up Inventory And Playbooks
Once the above script is executed successfully the next steps are to add your inventory
file and playbook
to manage your desktop apps on your Windows system. Create a directory called choco
and add localhost_inventory.yml
and desktop_apps.yml
and copy the scripts below:
#localhost_inventory.yml
all:
hosts: localhost
vars:
ansible_user: user #add your windows username here
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore
#desktop_apps.yml
---
- hosts: localhost
connection: local
tasks:
- name: Install/Uninstall/Update Chromium
win_chocolatey:
name:
- chromium
state: absent
- name: Install/Uninstall/Update Slack
win_chocolatey:
name:
- slack
state: present
- name: Install/Uninstall/Update Telegram
win_chocolatey:
name:
- telegram
state: latest
The desktop_apps.yml
script can be updated by looking at the chocolatey package website to see which packages are available to install. In this tutorial we are going to uninstall Chromium, ensure that Slack is installed else it will be installed, update Telegram to the latest version if installed, and if not install the latest version. This ansible module states work as follows:
- absent - uninstall package/software
- present - ensure the software is installed and if not install the latest version
- latest - update software to the latest version and if it does not exist install the latest version
Once all the above steps are completed run the following command to execute this Ansible playbook - ansible-playbook choco/desktop_apps.yml -i choco/localhost_inventory.yml --ask-pass --verbose
and you will be prompted to input your Windows password.
Once successful you can search for the apps and should see that you have installed or updated your desktop apps to the latest version like in this example below.
Conclusion
In conclusion, Chocolatey is an awesome open-source package manager for Windows operating systems and has a large community behind it. If you enjoyed this article consider signing up for our newsletter and don't forget to share it with people that would find it useful. Leave a comment below with a tutorial you would like us to cover.