# Ansible Loops

**URL:** <https://forum.ansible.com/t/ansible-loops/20738>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [March 1, 2016, 10:18pm UTC](https://forum.ansible.com/t/ansible-loops/20738 "2016-03-01T22:18:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [March 1, 2016, 10:18pm UTC](https://forum.ansible.com/t/ansible-loops/20738/1 "2016-03-01T22:18:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [March 2, 2016, 2:21pm UTC](https://forum.ansible.com/t/ansible-loops/20738/2 "2016-03-02T14:21:30Z")

</div>

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](http://172.30.1.21: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.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [March 2, 2016, 3:52pm UTC](https://forum.ansible.com/t/ansible-loops/20738/3 "2016-03-02T15:52:18Z")

</div>

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](http://yamllint.com) or a yaml-aware editor such as notepad++ to help get my syntax straight, that might help here.

---

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [March 2, 2016, 5:59pm UTC](https://forum.ansible.com/t/ansible-loops/20738/4 "2016-03-02T17:59:20Z")

</div>

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](http://172.30.1.21:8530)’, datatype: ‘string’}  
was throwing an error.

Thanks again.  
a.

---

<div class="post-metadata">

**Author:** ![Todd\_Schaal](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Todd\_Schaal](https://forum.ansible.com/u/Todd_Schaal)\
**Post date:** [November 7, 2016, 5:40pm UTC](https://forum.ansible.com/t/ansible-loops/20738/5 "2016-11-07T17:40:41Z")

</div>

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
