Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
danielmotaleite opened this issue Sep 19, 2017 · 5 comments
Closed

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

danielmotaleite opened this issue Sep 19, 2017 · 5 comments

Comments

@danielmotaleite
Copy link

danielmotaleite commented Sep 19, 2017

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
Copy link
Owner

jhaals commented Sep 27, 2017

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)]

@jhaals jhaals closed this as completed Sep 27, 2017
@danielmotaleite
Copy link
Author

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

thanks for the help!

@danielmotaleite
Copy link
Author

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

ansible/ansible#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
Copy link
Author

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants