# Check existence of a value from dictionary with list

**URL:** <https://forum.ansible.com/t/check-existence-of-a-value-from-dictionary-with-list/35676>\
**Category:** Ansible Developer\
**Tags:** kubernetes\
**Created:** [August 25, 2021, 10:57am UTC](https://forum.ansible.com/t/check-existence-of-a-value-from-dictionary-with-list/35676 "2021-08-25T10:57:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rijesh\_Puthiya\_Puray](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@Rijesh\_Puthiya\_Puray](https://forum.ansible.com/u/Rijesh_Puthiya_Puray)\
**Post date:** [August 25, 2021, 10:57am UTC](https://forum.ansible.com/t/check-existence-of-a-value-from-dictionary-with-list/35676/1 "2021-08-25T10:57:30Z")

</div>

Hello all,

I have to implement a logic where I need to create a few secrets in kubernetes cluster using ansible. The login I am trying to implement is to get registered all the secrets in a namepsapce first, then create the secrets if that not present. I specifically want to know how I can validate the secret already present.

- name: Get all insights secrets in namespace ‘{{ ansible\_operator\_meta.namespace }}’  
community.kubernetes.k8s\_info:  
api\_version: “v1”  
kind: “secrets”  
namespace: “{{ ansible\_operator\_meta.namespace }}”  
label\_selectors:
- project=myproject  
register: secrets\_register  
when: debug is not defined

This will register all the secrets with specific label from the namespace. This will be dictionary with lists as given below.

secrets\_register:  
resources:

- apiVersion: v1  
data:  
\_DATA\_ENCRYPTION\_PASSWORD:VTJGc2RHVmtYMS82b2EyOW9CZXNBY3lWTmI4QzBEclZKaW40U2haQ3hTMGl6bUxXMmFmamVVMXNPRC9kQ05kcA==  
kind: Secret  
metadata:  
labels:  
name: data-encryption-password  
namespace: test
- apiVersion: v1  
data:  
\_DATA\_ADMIN:VTJGc2RHVmtYMS82b2EyOW9CZXNBY3lWTmI4QzBEclZKaW40U2haQ3hTMGl6bUxXMmFmamVVMXNPRC9kQ05kcA==  
kind: Secret  
metadata:  
labels:  
name: mongodb-authsecret  
namespace: test
- apiVersion: v1  
data:  
\_DATA\_TEST:VTJGc2RHVmtYMS82b2EyOW9CZXNBY3lWTmI4QzBEclZKaW40U2haQ3hTMGl6bUxXMmFmamVVMXNPRC9kQ05kcA==  
kind: Secret  
metadata:  
labels:  
name: ingressca  
namespace: test

And then I need to create secrets with a logic when secret\_name not in secrets\_register.resources[\*].metadata.name is in secrets\_register something like below.

- name: Create secret  
k8s:  
state: present  
definition:  
apiVersion: v1  
kind: Secret  
type: Opaque  
metadata:  
name: “{{ secret\_name }}”  
namespace: “{{ project\_name | lower }}”  
data:  
config\_data.json: “{{ lookup(‘template’, mongo\_conn\_templates\_path + ‘/config\_data.json’ ) | tojson | b64encode }}”  
when: \>  
debug is not defined  
and secret\_name not in secrets\_register.resources[\*].metadata.name is in secrets\_register

But unfortunately I can’t get this statement “secret\_name not in secrets\_register.resources[\*].metadata.name is in” working as I am not able to figure out how to check existence of a value in a dictionary of lists in ansible. Can someone help?

Thanks in advance,  
Rijesh.
