# Conditional run of win\_feature with ansible

**URL:** <https://forum.ansible.com/t/conditional-run-of-win-feature-with-ansible/25375>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [August 30, 2017, 8:02pm UTC](https://forum.ansible.com/t/conditional-run-of-win-feature-with-ansible/25375 "2017-08-30T20:02:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniyal.javed](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@daniyal.javed](https://forum.ansible.com/u/daniyal.javed)\
**Post date:** [August 30, 2017, 8:02pm UTC](https://forum.ansible.com/t/conditional-run-of-win-feature-with-ansible/25375/1 "2017-08-30T20:02:30Z")

</div>

> up vote  
> down vote[favorite](https://stackoverflow.com/questions/45968747/conditional-run-of-win-feature-with-ansible#)  
>   
>   
> |   
>   
>   
>   
> I am installing (about 30) and removing (about 40) required windows features on a 2012 R2 box. My playbook looks like this:  
>   
>   
>   
> `--- - name: Installing features win_feature: name: '{{ item }}' state: Present with_items: '{{ features_to_install }}'`  
>   
>   
>   
> `- name: Removing features win_feature: name: '{{ item }}' state: Absent with_items: '{{ features_to_remove }}'`  
>   
>   
>   
> I then have my vars in a separate file which look like this:  
>   
>   
>   
> `--- features_to_install: [FileAndStorage-Services,File-Services,.....] features_to_remove: [AD-Certificate,ADCS-Cert-Authority.....]`  
>   
>   
>   
> All in all, everything works well. But it is too slow as I have to run it over and over again as I make changes. I want to introduce conditionals where it “skips” if a feature is already installed or is not present.  
>   
>   
>   
> I am trying to register the output from a powershell script converted to Json and then compare with `features_to_install` but cannot seem to get it right. The script that Im using:  
>   
>   
>   
> `Get-WindowsFeature | ? {$_.Installed -match "True"} | Select -exp Name | ConvertTo-Json`  
>   
>   
>   
> Here’s what Im working with so far. Any direction/help would be greatly appreciated: `- name: Copy powershell script to list windows features win_copy: src: '{{ Directory }}/tools/ListWindowsFeatures.ps1' dest: '{{ Powershell }}'`  
>   
>   
>   
> `- name: run powershell win_shell: '{{ Powershell }}/ListWindowsFeatures.ps1' register: presentfeatures`  
>   
>   
>   
> `- name: Installing features win_feature: name: File-Services state: Present with_items: '{{ features_to_install }}' when: presentfeatures != features_to_install`  
>   
>   
>   
> |

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [August 30, 2017, 8:38pm UTC](https://forum.ansible.com/t/conditional-run-of-win-feature-with-ansible/25375/2 "2017-08-30T20:38:18Z")

</div>

You can create a custom “facts” module to iterate all the features installed and then return what you want for further iteration in Ansible.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [August 31, 2017, 9:19am UTC](https://forum.ansible.com/t/conditional-run-of-win-feature-with-ansible/25375/3 "2017-08-31T09:19:46Z")

</div>

win\_feature also can take a comma-separated list of features to install. This is going to be MUCH faster than using with\_items as the features can all be tackled in a single batch.

There’s an example of a comma-separated list in the documentation here: [http://docs.ansible.com/ansible/latest/win\_feature\_module.html](http://docs.ansible.com/ansible/latest/win_feature_module.html)

```auto
- name: Install IIS (Web-Server and Web-Common-Http)
  win_feature:
    name: Web-Server,Web-Common-Http
    state: present

```

Hope this helps,

Jon
