variable inside regex_search inside a variable :)

3,346 views
Skip to first unread message

az

unread,
Jan 9, 2017, 12:50:34 PM1/9/17
to Ansible Project
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.



Dani C

unread,
Dec 10, 2017, 3:50:07 PM12/10/17
to Ansible Project
Hi,

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

Cheers

Marian Vasile Caraiman

unread,
Jan 16, 2018, 7:28:36 AM1/16/18
to Ansible Project
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}}.*"

2. 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
Reply all
Reply to author
Forward
0 new messages