How to use a variable inside of a regex string? (search filter)

16 views
Skip to first unread message

coo...@gmail.com

unread,
Nov 3, 2017, 1:14:34 PM11/3/17
to Ansible Project
It looks like this is working, but I get an error.  Does anyone know the proper way to do this?  Thanks!


- name: configure hostname
ios_config:
commands:
- ip domain-name {{ domain_name }}
when: not ansible_net_config | search("(ip domain-name {{ domain_name }}|ip domain name {{ domain_name }})")


 [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: not ansible_net_config | search("(ip domain-name {{ domain_name }}|ip domain name {{
domain_name }})")

Kai Stian Olstad

unread,
Nov 3, 2017, 2:32:36 PM11/3/17
to ansible...@googlegroups.com
On 03. nov. 2017 18:14, coo...@gmail.com wrote:
> It looks like this is working, but I get an error. Does anyone know the
> proper way to do this? Thanks!
>
>
> - name: configure hostname
> ios_config:
> commands:
> - ip domain-name {{ domain_name }}
> when: not ansible_net_config | search("(ip domain-name {{ domain_name }}|ip domain name {{ domain_name }})"


According to the template documentation[1] ~ (tilde) is used for concatenation of strings, so is just a matter of concatenate the elements.

when: not ansible_net_config | search("(ip domain-name " ~ domain_name ~ "|ip domain name " ~ domain_name ~ ")")


[1] http://jinja.pocoo.org/docs/dev/templates/#other-operators

--
Kai Stian Olstad

coo...@gmail.com

unread,
Nov 3, 2017, 9:23:35 PM11/3/17
to Ansible Project
Works, thank you!
Reply all
Reply to author
Forward
0 new messages