# Calling a variable within a variable within a jinja pipe filter

**URL:** <https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565>\
**Category:** Ansible Project\
**Created:** [January 29, 2019, 10:34pm UTC](https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565 "2019-01-29T22:34:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 29, 2019, 10:34pm UTC](https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565/1 "2019-01-29T22:34:12Z")

</div>

I am trying to do two things:

1. Call a variable while trying to set\_fact within a jinja pipe filter
2. Prevent fails when the user isn’t found

`

- name: “Find the user\_id for {{ user }}”  
set\_fact:  
uid: “{{ (userinfo.json.users|selectattr(‘username’,‘match’,‘MYVAR’)|list|first).user\_id }}”

`

If this is possible, what is the proper syntax for the variable MYVAR? Everything I am trying fails.

In addition, I can’t use “failed\_when: false” with the above and get it to work (ie passing in a non-existent user into MYVAR)–it seems to get ignored and ansible exits with a failure. What would be the proper way to prevent this from failing Ansible when the user isn’t found?

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 29, 2019, 10:44pm UTC](https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565/2 "2019-01-29T22:44:50Z")

</div>

Figured it out. I just call the variable with nothing else:

`

`

- name: “Find the user\_id for {{ user }}”  
set\_fact:  
uid: “{{ (userinfo.json.users|selectattr(‘username’,‘match’,MYVAR)|list|first).user\_id }}”

`

`

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 29, 2019, 10:51pm UTC](https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565/3 "2019-01-29T22:51:07Z")

</div>

Still trying to figure out how to keep the set\_fact from failing when passing in a non-existent user. If you have ideas for that, please let me know.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 29, 2019, 10:52pm UTC](https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565/4 "2019-01-29T22:52:25Z")

</div>

ignore\_errors: yes works with set\_fact

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 2, 2019, 12:22pm UTC](https://forum.ansible.com/t/calling-a-variable-within-a-variable-within-a-jinja-pipe-filter/29565/5 "2019-02-02T12:22:59Z")

</div>

> Figured it out. I just call the variable with nothing else:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: "Find the user\_id for {{ user }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uid: "{{  
> (userinfo.json.users|selectattr('username','match',MYVAR)|list|first).user\_id  
> }}"

Using ignore\_errors is a bad practice IMHO.

If you get no hits you "|list" will be empty and you "|first" filter will fail so you need to run it through default filter.

{{ (userinfo.json.users|selectattr('username','match',MYVAR)|list|first|default()).user\_id }}"

But this will also fail since user\_id doesn't exist in a empty list do you need to run that through the default filter as well.

So this should work without ignore\_errors

{{ (userinfo.json.users|selectattr('username','match',MYVAR)|list|first|default()).user\_id
