Ansible Loops

551 views
Skip to first unread message

a akin

unread,
Mar 2, 2016, 8:39:58 AM3/2/16
to Ansible Project
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.
Message has been deleted

a akin

unread,
Mar 2, 2016, 9:21:30 AM3/2/16
to Ansible Project
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.

J Hawkesworth

unread,
Mar 2, 2016, 10:52:18 AM3/2/16
to Ansible Project
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.

--- 
  name: "Insert Updates needed for WSUS"
  win_regedit: 
    data: "{{item.data}}"
    datatype: "{{item.datatype}}"
    key: "HKLM:\\SOFTWARE\\Policies\\Microsoft\\Windows\\{{item.location}}"
    value: "{{item.value}}"
  with_items: 
    - location: WindowsUpdate 
      data: "http://x.x.x.x:8530"
      datatype: string
      value: WUServer
    - location: SomeOtherLocation 
      data: "http://x.x.x.x:8530"
      datatype: string
      value: WUServer

Hope this helps,

Jon

a akin

unread,
Mar 2, 2016, 12:59:21 PM3/2/16
to Ansible Project
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.


Todd Schaal

unread,
Nov 7, 2016, 2:55:10 PM11/7/16
to Ansible Project

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
Reply all
Reply to author
Forward
0 new messages