Looping a nested data structure

Hi!

I’m converting our configuration and deployment over to Ansible, but I hit a snag with the latter.

We use a multi-application-per-host structure, each running as its own Linux user, and each containing its own set of modules that must be updated.

The structure is essentially this:

HostA:
UserA:

  • module1
  • module2
    UserB:
  • module2
  • module6
  • module7
    HostB:

Now, I’ve created per-host var files, where I’ve used the following structure:

(host_a.yaml)

What are these modules? May help me offer up the best way to model this.

In the worst case you could write a custom lookup plugin, but rearranging the data structure is probably best, depending on the use case.

Thanks for your reply. The modules are Bazaar repositories, cloned from a central server, which contain Python code with a certain structure. Each has its own repository.

The idea is using the bzr module to pull the latest version available, or clone it if doesn’t exist on the directory yet.

Got it.

Looping over this kind of construct where you have a hash with subkeys is not something that is really there now, and there may be a better way, but I tend to think a loopup plugin is the right way to do this, but it would be a little wierd to be general purpose. Lookup plugins are just ways of writing iterators in Ansible.

Take a look at “nested.py” and “together.py” in the source code checkout for some more advanced ones, “items.py” is the absolute most basic.

It is easy to loop over two lists for permutations, to zip lists, or to walk one list.

I think you want something like:

with_list_members_inside_hash:

  • variable_name
  • ‘modules’ # the name of what list member to iterate over.

And have it return {{item.0}} as the name of the hash each time and then the module name as {{item.1}}, for each item in each hash until you get to the end.

I’ll look into those plugins. Thanks for your help!