Ansible Loops

I am trying to update several values under a particular Registry Key name for example:

win_regedit:
key: HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
value: DetectionFrequency
data: 00000001
datatype: dword

win_regedit:
key: HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
value: NoAUShutdownOption
data: 00000000
datatype: dword

I know this is an inefficient way of doing it (besides it’s not working due to this error → found a duplicate dict key (win_regedit). Using
last defined value only) and same task can be achieved with Loops, and updated inside just one ‘win_regedit’ instance. Can someone help me with how I can do this via loops?. Thanks.

Ok, got some help from the docs and doing this now, better but probably still not the best:

  • name: Insert Updates needed for WSUS
    win_regedit:
    key: HKLM:\SOFTWARE\Policies
    Microsoft\Windows{{item.subkey}}
    value: “{{item.value}}”
    data: “{{item.data}}”
    datatype: “{{item.datatype}}”
    with_items:
  • {subkey: ‘WindowsUpdate’, value: ‘WUServer’, data: ‘http://x.x.x.x:8530’, item.datatype: ‘string’}

but I am getting this error:
“fatal: [172.x.x.x]: FAILED! => {“failed”: true, “msg”: “‘dict object’ has no attribute ‘datatype’”}”

No error if I comment out " datatype: “{{item.datatype}}”" but the values are being created as default datatype (string).

I am on Ansible 2.0.1.0.

Any help appreciated.

I think you are on the right track.

I suspect you don’t want ‘item.datatype’ in your with_items list - you only need item. when you are dereferencing the variable.

Not tested, but I think maybe something like this is what you are after. I changed subkey to location as it seems subkey has a special meaning in yaml.

Incidentally I use yamllint.com or a yaml-aware editor such as notepad++ to help get my syntax straight, that might help here.

Hi J,

Thanks, your syntax for listing the items under the "with_items’ section did the trick.
I wonder why listing as:
{location: ‘WindowsUpdate’, value: ‘WUServer’, data: ‘http://x.x.x.x:8530’, datatype: ‘string’}
was throwing an error.

Thanks again.
a.

So what can be done about the “found a duplicate dict key” message? Surely modifying multiple values under one key is a legitimate use case. How do you do it? I tried using 2 separate task and also a loop like below, but I always get the same message. What am I doing wrong?

  • name: Miscellaneous Security Settings
    win_regedit:
    key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\policies{{item.subkey}}
    value: “{{item.value}}”
    data: “{{item.data}}”
    datatype: “{{item.datatype}}”
    with_items:
  • subkey: system
    value : dontdisplaylastusername
    data: 1
    datatype: dword
  • subkyey: system
    vlaue: legalnoticecaption
    data: Warning
    datatype: string