Conditional run of win_feature with ansible

275 views
Skip to first unread message

daniya...@arctiq.ca

unread,
Aug 30, 2017, 4:02:30 PM8/30/17
to Ansible Project

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

Jordan Borean

unread,
Aug 30, 2017, 4:38:18 PM8/30/17
to Ansible Project
You can create a custom "facts" module to iterate all the features installed and then return what you want for further iteration in Ansible.

J Hawkesworth

unread,
Aug 31, 2017, 5:19:46 AM8/31/17
to Ansible Project
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

- name: Install IIS (Web-Server and Web-Common-Http)
  win_feature:
    name: Web-Server,Web-Common-Http
    state: present
Hope this helps,

Jon
Reply all
Reply to author
Forward
0 new messages