approle auth not working with vault_kv2_get lookup #444
-
Hey there, Hope you're well! I'm seeking help with the lookup plugin When I use the $ VAULT_TOKEN=$(vault write -field=token auth/approle/login role_id="408b1d9d-5b67-458a-8bc2-REDACTED" secret_id="4901a2fc-ced3-5cda-eb8d-REDACTED")
$ vault kv get -namespace=my_vault_namespace -field=my_field -mount=kv "my/kv-path/secret"
-----BEGIN RSA PRIVATE KEY----- using - hosts: localhost
gather_facts: no
vars:
ssh_dev: "{{ lookup('community.hashi_vault.vault_kv2_get', 'my/kv-path/secret', engine_mount_point='kv', url='https://REDACTED:443', namespace='my_vault_namespace', auth_method='approle', role_id='408b1d9d-5b67-458a-8bc2-REDACTED', secret_id='4901a2fc-ced3-5cda-eb8d-REDACTED')['secret']['my_field'] }}"
- name: Print ssh_dev
ansible.builtin.debug:
var: ssh_dev output: TASK [Print ssh_dev] ****************************************************************************************************************************************************************************************************************************
task path: //path/to/mytask.yml:49
Loading collection community.hashi_vault from /Users/carlos/.ansible/collections/ansible_collections/community/hashi_vault
[WARNING]: Collection community.hashi_vault does not support Ansible version 2.11.4
fatal: [localhost]: FAILED! => {
"msg": "An unhandled exception occurred while templating '{{ lookup('community.hashi_vault.vault_kv2_get', 'my/kv-path/secret', engine_mount_point='kv', url='https://REDACTED:443', namespace='my_vault_namespace', auth_method='approle', role_id='408b1d9d-5b67-458a-8bc2-REDACTED', secret_id='4901a2fc-ced3-5cda-eb8d-REDACTED' )['secret']['my-field'] }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'community.hashi_vault.vault_kv2_get'. Error was a <class 'hvac.exceptions.Forbidden'>, original message: permission denied, on post https://REDACTED:443/v1/auth/approle/login"
} using the generated vault token - hosts: localhost
gather_facts: no
vars:
ssh_dev: "{{ lookup('community.hashi_vault.vault_kv2_get', 'my/kv-path/secret', engine_mount_point='kv', url='https://REDACTED:443', namespace='my_vault_namespace', token='hvs.CAESIIlgbqZ_eHD1SV5kE6EhutQ_TSBqpkwDtssxlz0EeuYmGiUKHGh2cyREDACTED' )['secret']['my_field'] }}" output: TASK [Print ssh_dev] ****************************************************************************************************************************************************************************************************************************
task path: /path/to/mytask.yml:49
Loading collection community.hashi_vault from /Users/REDACTED/.ansible/collections/ansible_collections/community/hashi_vault
[WARNING]: Collection community.hashi_vault does not support Ansible version 2.11.4
ok: [localhost] => {
"ssh_dev": "-----BEGIN RSA PRIVATE KEY----- gpt is telling me that the plugin does authentication for me, I couldn't find the code snippet so any help appreciated if I have missed something obvious. Cheers, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
i dont get this to work either. i really tried to get this to work for almost 2 days now. - hosts: host1
gather_facts: no
tasks:
- name: damn
vars:
ansible_hashi_vault_auth_method: token
ansible_hashi_vault_token: <my token>
ansible_hashi_vault_engine_mount_point: "data/ansible/proxmox/dc-workshop1"
ansible_hashi_vault_url: http://127.0.0.1:8200
ansible.builtin.debug:
msg: '{{ item.values() | list }}'
with_community.hashi_vault.vault_kv2_get:
- user this is the error this method is producing: so i just use the curl version now:
|
Beta Was this translation helpful? Give feedback.
-
I've tried with env variables instead: further proof when I do a curl to vault with approle curl --request POST --data '{"role_id": "408b1d9d-5b67-458a-8bc2-REDACTED", "secret_id": "4901a2fc-ced3-5cda-eb8d-REDACTED"}' https://REDACTED:443/v1/auth/approle/login I get:
So generally speaking I can use the approle Any further help to find the issue would be much appreciated! |
Beta Was this translation helpful? Give feedback.
-
Some progress on my side... the problem is that authentication happens on the root namespace not on the one I give for the kv secret. Because of that either the login works with
|
Beta Was this translation helpful? Give feedback.
@carlos-lehmann thank you so much for elaborating! this makes a lot of sense and you went through all the permutations I would have suggested.
There is one other possible option which provides you with more control over the path, and that would be to use the
vault_read
plugin instead:Since that plugin is not kv-aware, it isn't trying to do that level of path manipulation, and so you might be able to set the path to what it needs to be (by inserting
/data/
in the right place), but you won't get thesecret
convenience return value from that plugin, so you'll have to index it as['data']['data…