# variable inside regex\_search inside a variable :)

**URL:** <https://forum.ansible.com/t/variable-inside-regex-search-inside-a-variable/23411>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 9, 2017, 5:21pm UTC](https://forum.ansible.com/t/variable-inside-regex-search-inside-a-variable/23411 "2017-01-09T17:21:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![az1](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@az1](https://forum.ansible.com/u/az1)\
**Post date:** [January 9, 2017, 5:21pm UTC](https://forum.ansible.com/t/variable-inside-regex-search-inside-a-variable/23411/1 "2017-01-09T17:21:45Z")

</div>

Hello,

after hours of struggle with Ansible I’ve decided to ask for help smarter people. What I’m trying to do is to remove (lets say just do ls -la for now all files and directories within certain locations which names are not containing any part of certain list of variables. Let me give you an example:

`

- name: List currently existing letsencrypt certificates  
find:  
paths={{ item.paths }}  
file\_type={{ item.file\_type }}  
register: haproxy\_le\_existing  
with\_items:

- { paths: /etc/letsencrypt/live/, file\_type: directory}

- { paths: /etc/haproxy/ssl/le/, file\_type: file}

- debug: var=‘haproxy\_le\_ssl|map(attribute=“domain”)|list|join(“|”)’

- debug: var=‘haproxy\_le\_existing.results|sum(attribute=‘files’, start=)|map(attribute=‘path’)|map(‘regex\_search’,’^((?!{{haproxy\_le\_ssl|map(attribute=‘domain’)|list|join(‘|’)}}).)\*$‘)| select(‘string’) | list’

- name: Remove SSLs that are not in the config  
shell: “ls -la {{ item }}”  
with\_items: “{{ haproxy\_le\_existing.results|sum(attribute=‘files’, start=)|map(attribute=‘path’)|map(‘regex\_search’,‘^((?!{{haproxy\_le\_ssl|map(attribute=‘domain’)|list|join(’|‘)}}).)\*$’)| select(‘string’) | list }}”  
`

variables are here:

`  
haproxy\_le\_ssl:

- { domain: let2.example.is}
- { domain: let3.example.is}
- { domain: let4.example.is}  
`

Debug works perfectly:

`  
TASK [common/haproxy : List currently existing letsencrypt certificates] \*\*\*\*\*\*\*  
ok: [haproxy-test.aws.example] =\> (item={u’file\_type’: u’directory’, u’paths’: u’/etc/letsencrypt/live/‘})  
ok: [haproxy-test.aws.example] =\> (item={u’file\_type’: u’file’, u’paths’: u’/etc/haproxy/ssl/le/'})

msg: All items completed

msg: All items completed

TASK [common/haproxy : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [haproxy-test.aws.example] =\> {  
“haproxy\_le\_ssl|map(attribute="domain")|list|join("|")”: “let2.example.is|let3.example.is|let4.example.is”  
}

TASK [common/haproxy : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [haproxy-test.aws.example] =\> {  
“haproxy\_le\_existing.results|sum(attribute=‘files’, start=)|map(attribute=‘path’)|map(‘regex\_search’,‘^((?!let2.example.is|let3.example.is|let4.example.is).)\*$’)| select(‘string’) | list”: [  
“/etc/letsencrypt/live/let1.example.is”,  
“/etc/haproxy/ssl/le/dupa.pem”  
]  
}  
`

most probably because it doesn’t have “{{ }}”, but when it comes to running a task I’ve got following error:

`TASK [common/haproxy : Remove SSLs that are not in the config] ***************** fatal: [haproxy-test.aws.example]: FAILED! => {"failed": true, "msg": "template error while templating string: expected token ',', got 'domain'. String: {{ haproxy_le_existing.results|sum(attribute='files', start=[])|map(attribute='path')|map('regex_search','^((?!{{haproxy_le_ssl|map(attribute='domain')|list|join('|')}}).)*$')| select('string') | list }}"}`

For me creating oneliner like this is more convenient than creating multiple tasks (most probably I could create something using join from multiple tasks, but I’d like to understand what is happening here as using variable inside regex seems to be quite handy and I might use it in the future), even it is not the easiest to read way of doing the job. Please advice what could I do in this situation.

---

<div class="post-metadata">

**Author:** ![Dani\_C](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dani\_C](https://forum.ansible.com/u/Dani_C)\
**Post date:** [December 10, 2017, 8:50pm UTC](https://forum.ansible.com/t/variable-inside-regex-search-inside-a-variable/23411/2 "2017-12-10T20:50:06Z")

</div>

Hi,

Any chance you managed to work out as i’m exactly in the same point and boy it drives me crazy…

Cheers

---

<div class="post-metadata">

**Author:** ![Marian\_Vasile\_Caraim](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Marian\_Vasile\_Caraim](https://forum.ansible.com/u/Marian_Vasile_Caraim)\
**Post date:** [January 16, 2018, 12:28pm UTC](https://forum.ansible.com/t/variable-inside-regex-search-inside-a-variable/23411/3 "2018-01-16T12:28:36Z")

</div>

I’m working in a similar task and what worked for me was this trick :

1. create a variable :  
vars:  
prefix=“192”

- name: Make the regexp variable

set\_fact: term=“^{{prefix}}.\*”

1. use the “prepared variable”

- name: Get ip4 address of the server  
set\_fact: matches=“{{ ansible\_all\_ipv4\_addresses|map(‘regex\_search’, (term) )|list|difference(empty)}}”  
failed\_when: matches | count \> 1
