2017-08-31 64 views
0

我想寫一個檢查安裝的Windows功能的手冊。如果安裝了它們,它應該跳過它們,否則從我的變量列表中安裝它們。Ansible with_items針對json的條件檢查

- 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 }}' 

我features_to_install瓦爾在一個單獨的/瓦爾/文件:

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

我想劇本跳過安裝的功能,如果該功能存在於JSON。錯誤即時得到:

{ 「失敗」:真實, 「味精」:「由於條件檢查 '!{{項目}} = {{featureinstalled}}' 失敗錯誤是:模板出錯字符串:{%if FileAndStorage-Services!= [u'FileAndStorage-Services', u'Storage-Services',u'FS-SMB1',u .FileAndStorage-Services', u'Storage-Services',u'FS-SMB1',u 'WoW64-Support']%}真{%else%} False {%endif%} \ n \ n該錯誤似乎在 '/tmp/worldengine/src/roles/webserver/tasks/windows_features.yml' : 第31行第3列,但可能在文件的其他位置取決於 確切的語法問題。\ n \ nT他得罪行出現在:\ n \ n \正 名稱:安裝功能的\ n^

+0

如何在編寫代碼之前閱讀[docs on'when'](http://docs.ansible.com/ansible/latest/playbooks_conditionals.html#the-when-statement)?另外,'!='操作符顯然是錯誤的 - 「不等於」不是你想要檢查的 - 用人類語言大聲說出表達式,你會發現它。 – techraf

+0

哈哈工作 – firebolt

回答

0

handy filters可操控臺:

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

這將從features_to_install迭代功能列表,但不在featureinstalled列表中。

+0

真棒謝謝你。我有條件檢查何時出錯。我最終使用「in」和「not in」,但是我也會測試你的方法。謝謝! – firebolt