0
votes

I am trying to write a playbook which checks for the windows features installed. If they are installed, it should skip them, otherwise install them from my list of vars.

- name: win command
  win_command: 'powershell.exe "Get-WindowsFeature | Where Installed | Select -exp Name | ConvertTo-Json"'
  register: result

- name: Register vars
  set_fact:
    featureinstalled: '{{ result.stdout | from_json }}'

- name: Installing features
  win_feature:
    name: '{{ item }}'
    state: Present
  with_items:
    '{{features_to_install}}'
  when: '{{ item }} != {{ featureinstalled }}'

My features_to_install vars in a separate /vars/ file:

---
features_to_install: [FileAndStorage-Services,File-Services,.....]

I want the playbook to skip installing the feature if the feature is present in the JSON. The error Im getting:

{ "failed": true, "msg": "The conditional check '{{ item }} != {{ featureinstalled }}' failed. The error was: template error while templating string: expected token ',', got 'string'. String: {% if FileAndStorage-Services != [u'FileAndStorage-Services', u'Storage-Services', u'FS-SMB1', u'WoW64-Support'] %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/tmp/worldengine/src/roles/webserver/tasks/windows_features.yml': line 31, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Installing features\n ^

1
How about reading the docs on when before writing the code? Also, != operator is blatantly wrong - "not equal" is not what you intend to check - say the expression aloud in human language and you'll figure it out. - techraf
lol that worked - firebolt

1 Answers

0
votes

There are handy filters available to manipulate sets:

- name: Installing features
  win_feature:
    name: '{{ item }}'
    state: Present
  with_items: '{{ features_to_install | difference(featureinstalled) }}'

This will iterate over features from features_to_install list, but that are not in featureinstalled list.