When writing roles, I try and keep them re-usable. This means avoiding making them dependent on any arrangement of vars in my inventory and playbooks.
The problem I keep bumping into then is that ansible / jinja2 have limited means of transforming, for example, a list of server description hashes, into a hash of server name to ssh public keys.
I find myself pining for list comprehensions, so I could do this sort of thing:
```vars:# an example of a typical datastructure in the inventory:# details aren’t important, what matters is that it is not# 1:1 with the role’s requirementsservers:- name: server1ssh:keys:rsa:pub: ‘xsdfjhdkfuhwef …’prv: ‘…’# …- name: server2ssh:# … and so on`
# elsewhere... roles: # This role just wants a dictionary mapping server name -> rsa public key
```- role: fooserver_keys: `
…Where the list comprehension creates a map of item.name → item.ssh.keys.rsa.pub extracted from the ‘servers’ list.
How do other people approach this? Is there a good solution?
Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia
"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
Thanks, I did spot that in the documentation, but sadly, no I don't have the
right version of jinja2 (on Ubuntu 12.04). I could try and upgrade, but I
suspect that wouldn't be sensible.
Besides:
a) jinja2's map filter looks like it may only handle top-level attributes of
list items
b) the result would be a string and presumably would not be treated as a data
structure by the consuming role
Had a quick look at the source code for the map filter, and I believe
it will descend to lower levels and the result is an arbitrary list,
so I think this would work.
However, this is of no use if your control node is running a stable OS
The only other approach I can think of is to split out keys from the
'servers' the data structure. Yes ugly
Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia
"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
Considered laziness. I’d like to avoid adding in dependencies which I then need to ensure are always present, when they aren’t proven to be worth the effort. Thanks, despite what I said above, that may come in useful at some point. N