Remove Windows Registry path or key on Windows-like systems - Ansible module win_regedit
How to automate the deletion of the Windows Registry “Test” path with one key “hello” under a specific hive Windows-like systems with Ansible Playbook using win_regedit module.


How to Remove Windows Registry key on Windows-like systems with Ansible?
Changing registry values manually can be time-consuming and error-prone. Ansible includes built-in capabilities for managing individual key-value pairs in an idempotent way. I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible remove Windows Registry on Windows-like systems
ansible.windows.win_regedit
- Get information about Windows registry keys
Let’s talk about the Ansible module win_regedit
.
The full name is ansible.windows.win_regedit
, which means that is part of the collection of modules specialized to interact with Windows target host.
It’s a module pretty stable and out for years and it works in Windows and Windows Server operating systems.
It adds, changes, or removes registry keys and values.
Parameters
- path string - The full registry key path including the hive to search for
- name string - Name of the registry entry in the path parameters.
- state string - present/absent
The only mandatory parameter is “path” which is the full registry key path including the hive to search for. You might like to specify a single “name” of the name of the registry entry. If you want to delete all the paths, basically all the Windows Registry tree but if you want only the single entry you need also to specify the “name” of it. Please be careful! The essential parameter for our use-case is “state” to specify if we would like to add or modify (“present” option) or remove (“absent” option).
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to Remove Windows Registry key on Windows-like systems accessing the Windows Registry with Ansible Playbook. Specifically, I’m going to delete a Windows Registry key located in “HKEY_LOCAL_MACHINE\Software\Test”.
code
---
- name: windows registry remove
hosts: all
vars:
mypath: 'HKLM:\Software\Test'
tasks:
- name: registry remove path
ansible.windows.win_regedit:
path: "{{ mypath }}"
state: absent
execution
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory windows/registry_remove.yml
PLAY [windows registry remove] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [registry remove path] ***********************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory windows/registry_remove.yml
PLAY [windows registry remove] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [registry remove path] ***********************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
after execution
Recap
Now you know how to remove the Windows Registry key on Windows-like systems with Ansible.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate