Efficiently Polling Windows Features in Ansible

Efficiently Polling Windows Features in Ansible

June 24, 2025·Tyler Rasmussen
Tyler Rasmussen

I recently ran into an issue with slow playbook runtimes. I found when querying Windows hosts — specifically when determining if a Windows Feature is installed, the task would take substantially longer than any other playbooks without this query.

Here’s the troublesome area:

- name: Collect state of ADDS feature.
  ansible.windows.win_powershell:
    script: Get-WindowsFeature -Name AD-Domain-Services
  register: adds_results
  changed_when: false

- name: Determine if ADDS is installed.
  set_fact:
    adds_installed: "{{ adds_results.output[0].Installed }}"

It’s a pretty simple task. It pulls Windows using the Get-WindowsFeature PowerShell command to determine if a specific Windows Feature, in this case Active Directory Domain Services (ADDS) is installed. The state of this feature (installed/not installed) is then stored in a variable: adds_installed as a boolean (true/false). Useful when wanting to determine if a task should run based off of the state of a Windows Feature.

The bottleneck in this task is the Get-WindowsFeature command as when run it takes upwards of 30-60 seconds to come back with a result. I needed to find an alternative solution…

Here’s what I came up with. Instead of checking each Windows Feature individually, it queries the host and collects the installation state on all of the Windows Features in one fell swoop, saving them into a dictionary.

- name: 'Determine Windows Features.'
  ansible.windows.win_feature_info:
  register: feature_state
  tags: always

- name: 'Store results.'
  set_fact:
    windows_features: >-
      {{ dict( feature_state.features | map(attribute='name') | zip( feature_state.features | map(attribute='installed')))}}      
  tags: always

Now, anytime I need to determine if a task should run based off of the state of a Windows Feature, I can just query the dictionary as it holds the state of all Windows Features for that particular host.

...
when: not windows_features['AD-Domain-Services']

So long as I don’t need to refresh the dictionary, I now have a very quick method of determining a Windows Feature state. Handy!