Transform dictionary into lists for module consumption

83 views
Skip to first unread message

Geoff Webster

unread,
Dec 6, 2016, 2:36:23 PM12/6/16
to Ansible Project
I am trying to simplify some variable files for our developers and I am wondering if I can do this transform with existing filter/lookup plugins Ansible. 

I'll have this dictionary (and the fields are not static, there could be more settings):

OptionSettings:
      aws:elasticbeanstalk:command:
        BatchSize: '30'
        BatchSizeType: Percentage
      aws:elasticbeanstalk:sns:topics:
        Notification Endpoint: m...@example.com

And I want to transform the elements under OptionSettings to match what the module/API is expecting:

OptionSettingsTransform:
  - Namespace: aws:elasticbeanstalk:command
    OptionName: Batch Size
    Value: "30"
  - Namepsace: aws:elasticbeanstalk:command
    OptionName: BatchSizeType
    Value: Percentage
  - Namespace: aws:elasticbeanstalk:sns:topics
    OptionName: Notification Endpoint
    Value: m...@example.com

So it involves iterating over each subelement of OptionSettings and then each subelement of each namespace while retaining the namespace it is working within. I have tried looking at with_items, together, subelements, but I can't seem to wrap my head around it. Any ideas? Is this too much and would I need to implement a custom lookup plugin in python to do this?

Any help/suggestions would be appreciated :)

Stephen Granger

unread,
Dec 8, 2016, 12:33:45 AM12/8/16
to ansible...@googlegroups.com
Use the jinja map attribute function to extract the items into a list.

I've put together an example here

There is some more documentation here with links here

Sorry I don't have the time to go through a full example with your use case, hope this helps.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/298f5f03-54c6-4941-9ab0-f42613730fc9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve
Reply all
Reply to author
Forward
0 new messages