jhaals / ansible-vault

ansible lookup plugin for secrets stored in Vault(by HashiCorp)
BSD 3-Clause "New" or "Revised" License
347 stars 65 forks source link

missing url key, plugin fails, even with ' | default('') ' #59

Closed danielmotaleite closed 6 years ago

danielmotaleite commented 6 years ago

Hi

i'm trying to find htpasswd for several users, but not all of then do have a htpasswd access, no entry in vault, so when they do not have, i get:

failed: [nginx-01] (item=user1) => {"failed": true, "msg": "AnsibleError: An unhandled exception occurred while running the lookup plugin 'vault'. Error was a <class 'ansible.errors.AnsibleError'>, original message: Unable to read secret/nginx/live/htpasswd/user1 from vault: HTTP Error 404: Not Found", "user": "user1"} and stop the file template.

My template have this: {% for user,pass in ( lookup('vault', 'secret/nginx/'+ env +'/htpasswd/' + office ) | default('') ).iteritems() %} {{ user }}:{{ pass }} {% endfor %} I would assume that with the | default('') , the plugin would continue, but it just fails

So is any way for the | default('') to work? if not, maybe adding a extra parameter, or starting the url with + , to flag that the query can fail silently

Thanks for the help and for the plugin

jhaals commented 6 years ago

Hi, Lookup plugins like file has the same behaviour and will fail when trying to lookup a file that does not exist. I want this plugin to work in a similar way as the built-in lookup plugins

  - hosts: localhost
    connection: local
    gather_facts: False
    tasks:
    - name: Get a secret from Vault
~_    debug: msg="{{ lookup('file', 'user_data_file') | default('')}}"
~/src/ansible-vault/tests(master ✗) ansible-playbook test-playbook.yaml
 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost] **********************************************************************************************************************************************************

TASK [Get a secret from Vault] ********************************************************************************************************************************************
 [WARNING]: Unable to find 'user_data_file' in expected paths.

fatal: [localhost]: FAILED! => {"failed": true, "msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: user_data_file"}
    to retry, use: --limit @/Users/jhaals/src/ansible-vault/tests/test-playbook.retry

PLAY RECAP ****************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1

~/src/ansible-vault/tests(master ✗) ansible --version
ansible 2.3.2.0
  config file = /Users/jhaals/src/ansible-vault/tests/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Jul 31 2017, 13:30:25) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
danielmotaleite commented 6 years ago

Ok, so if its the same behaviour, i guess its OK :)

thanks for the help!

danielmotaleite commented 6 years ago

while other lookup also have the problem, i just found by accident that not all agree that simply fail is the best option:

https://github.com/ansible/ansible/pull/28908

The File lookup may be logic to fail if the file is missing, but like passwordstore lookup, in vault lookup may be more friendly to have a option to ignore the missing key as they are possibly mode dynamic lookups

danielmotaleite commented 6 years ago

@jhaals please reopen this as this limits a lot the ansible<->vault usage

We want to store some private keys in the vault, but if they are missing, ansible would create then and upload to vault. We can not do this because right now, if the key is missing, the ansible will abort, forcing the manual creation of keys

Please add the "ok_if_missing" option, so we can test if a value exists in vault, even if File lookup aborts

It does not look as a hard change, If we do this and submit a pull request, would you accept it?

strangeman commented 4 years ago

@danielmotaleite sorry for necroposting, but {{ lookup('vault', 'secret/ansible/missing_variable', 'value', errors='ignore') or "some_default_value" }} works for me