TerryHowe / ansible-modules-hashivault

Ansible module for Hashicorp Vault.
https://pypi.python.org/pypi/ansible-modules-hashivault
MIT License
454 stars 155 forks source link

Question: why does the lookup module convert the result value to string? #18

Closed moorglade closed 7 years ago

moorglade commented 7 years ago

A quick question about the behavior of the lookup module:

I've observed that the data received from Vault is always converted to a string, regardless of its real type.

For example, if I have a list stored in Vault:

Key            Value
---            -----
my_secrets     [my_secret1 my_secret2 my_secret3]

then the value returned by the lookup plugin is a string instead of a proper list, i.e.:

"[u'my_secret1', u'my_secret2', u'my_secret3']"

If I assign this result directly to an Ansible variable, it gets automatically converted to a list by the Ansible's templating engine. It is not possible, however, to use the result directly in a more complex Jinja2 expression, such as:

"{{ lookup('hashivault', '/secret/example', 'my_secrets') | first }}"

The above expression does not return the first element from the list (as I would expect), but the first character of the string ([).

Is there any reason why the lookup plugin cannot return a proper list in such a case?

TerryHowe commented 7 years ago

I don't have a good reason for you off the top of my head.

moorglade commented 7 years ago

In that case, would it be OK if I submitted a PR with an optional parameter stringify_data=True added to the lookup plugin?

I did some testing and this seems be easy to implement, and would simplify my use case.

TerryHowe commented 7 years ago

I was thinking just release it on a new major release.

moorglade commented 7 years ago

You mean without the str() call on the lookup result? If that doesn't break anything for you, that'd be fine for me.

TerryHowe commented 7 years ago

Hopefully this will cover your use case https://github.com/TerryHowe/ansible-modules-hashivault/commit/170b41f6bb0389474abc57330a629cca8d7c8ab0