简体   繁体   English

如何捕获未找到密钥的Ansible credstash查找插件的异常?

[英]How to catch exceptions for ansible credstash lookup plugin with key not found?

I have the following code 我有以下代码

 app_key:   "{{ lookup('credstash', 'aws/project/'+app_name+'/'+app_env+'/app_key') | default('not-set') }}"

And was expecting to be able set a default value based on lookup failing with a key not found, and then generate and store a key later in my playbook. 并期望能够基于查找失败并没有找到键来设置默认值,然后稍后在我的剧本中生成并存储一个键。

However, I found that the plugin raises an Exception which makes the entire playbook run fail. 但是,我发现该插件引发Exception,这会使整个剧本运行失败。 Obviously this is not what I was looking for (pre-storing app-keys for non production branches) 显然,这不是我想要的(为非生产分支预存储应用程序密钥)

(see credstash code: You can find the credstash plugidn code here https://github.com/ansible/ansible/blob/ec701c4b82e570371af7c3999ffb587d870a5b37/lib/ansible/plugins/lookup/credstash.py ) (请参阅credstash代码:您可以在此处找到credstash插件代码https://github.com/ansible/ansible/blob/ec701c4b82e570371af7c3999ffb587d870a5b37/lib/ansible/plugins/lookup/credstash.py

What are my options? 我有什么选择?

What are my options? 我有什么选择?

For example (I don't know what task you define app_key in, so I use set_fact in the second task below): 例如(我不知道您在其中定义app_key任务,因此我在下面的第二个任务中使用set_fact ):

- set_fact:
    app_key_candidate: "{{ lookup('credstash', 'aws/project/'+app_name+'/'+app_env+'/app_key')"
  ignore_errors: true

- set_fact:
    app_key: "{{ app_key_candidate | default('not-set') }}" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM