1

I'm writing an ansible playbook to look in the Windows registry, the "uninstall" command for a given software. That command is saved in a registry key named "UninstallString".

I'm having some problems to extract that particular value. The idea is to later use an exec from the very same ansible playbook and uninstall that software.

In this code, I'm trying to do that for the software "7-Zip". Later in the playbook I'll use a variable, but for testing purposes I'm hardcoding "Zip".

tasks:

- win_reg_stat:
    path: HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
  register: lista64

- win_reg_stat:
    path: HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{{ item }}
    name: UninstallString
  with_items: "{{ lista64.sub_keys | select('match','.*Zip.*') | list }}"
  register: desinstala

- debug:
    msg: "{{ desinstala.results }}"

This code returns this output:

ok: [172.16.1.25] => {
"msg": [
    {
        "_ansible_ignore_errors": null, 
        "_ansible_item_result": true, 
        "_ansible_no_log": false, 
        "_ansible_parsed": true, 
        "changed": false, 
        "exists": true, 
        "failed": false, 
        "item": "7-Zip", 
        "raw_value": "C:\\Program Files\\7-Zip\\Uninstall.exe", 
        "type": "REG_SZ", 
        "value": "C:\\Program Files\\7-Zip\\Uninstall.exe"
    }
]

}

How could I save "C:\Program Files\7-Zip\Uninstall.exe" into a variable that I could use later on the playbook? I tried with things like "{{ desinstala.results.value }}" or "{{ desinstala.results | select... }}" but I just can't find the way to do it.

Any help is very welcome :)

carrotcakeslayer
  • 809
  • 2
  • 9
  • 33
  • 2
    Apply the method from the answer under the first dup-target: `{{ desinstala.results[0].value }}` or `{{ (desinstala.results | first).value }}`. – techraf Mar 23 '18 at 12:27
  • ohh lord. It works perfectly. I was trying (and failing) with: {{ desinstala.results[0] }} and got the full list, and when I tried to specify the position like {{ desinstala.results[6] }} it was tellining me that there was no such position. I did not know that the position was 0 and I could then use the "." to invoke the value. I see now what I was not understanding before. Thank you very much for your help and patience! – carrotcakeslayer Mar 23 '18 at 12:32
  • "*`{{ desinstala.results[0] }}` and got the full list*" ー you get a dictionary, not a list. `desinstala.results` is a single-element list of dictionaries. – techraf Mar 23 '18 at 12:33

0 Answers0