Ansible jinja2 regex search fails to properly find the beginning of string

Viewed 41

I'm using

ansible 2.9.27
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, May 27 2022, 11:27:32) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Pursuant to my question here: Ansible set_fact list using jinja , I am trying to create a list which is derived from a list, but with an element removed: if it contains "default" at the beginning of the list element string, remove it. But why doesn't the regex work properly? The sticking point is the beginning of string operator.

Here is my list:

[ u'default via 10.13.99.1 dev bond0.99',
  u'10.13.101.0/23 dev p1p1.101',
  u'128.1.66.227 via 10.13.201.1 dev p1p1.201' ]

It's called found_routes. When I try to filter out the first entry, the default route, I attempt:

- name: Populate appended route list
  set_fact:
    found_added_routes: "{{ found_routes | select('search', ' via ') | reject('regex', '^default') | list }}"

That doesn't work. But this does:

- name: Populate appended route list
  set_fact:
    found_added_routes: "{{ found_routes | select('search', ' via ') | reject('regex', '^..default') | list }}"

...what are those two invisible characters that my regex is finding?

Here is how I create output:

- name: Debug found_routes
  debug:
    msg: "Found route list: {{ item }}"
  loop: "{{ found_routes }}"

- name: Debug found_added_routes
  debug:
    msg: "Found: {{ item }}"
  loop: "{{ found_added_routes }}"

And here's the output from the successful set_fact:

TASK [networks : Debug found_route_list] **********************************************************************************************************************
ok: [server01.example.com] => (item=default via 10.13.99.1 dev bond0.99) => {
    "msg": "Found route list: default via 10.13.99.1 dev bond0.99"
}
ok: [server01.example.com] => (item=10.13.101.0/24 dev p1p1.101) => {
    "msg": "Found route list: 10.13.101.0/24 dev p1p1.101"
}
ok: [server01.example.com] => (item=128.1.66.227 via 10.13.101.1 dev p1p1.101) => {
    "msg": "Found route list: 128.1.66.227 via 10.13.101.1 dev p1p1.101"
}

TASK [networks : Debug found_added_routes] **********************************************************************************************************************
ok: [server01.example.com] => (item=10.13.101.0/24 dev p1p1.101) => {
    "msg": "Found: 10.13.101.0/24 dev p1p1.101"
}
ok: [server01.example.com] => (item=128.1.66.227 via 10.13.101.1 dev p1p1.101) => {
    "msg": "Found: 128.1.66.227 via 10.13.101.1 dev p1p1.101"
}

But as you can see, in order to get rid of that first "default" route, I needed to use ^..default instead of ^default and I'm at a loss to understand why.

1 Answers

OK, because of the jinja2 magic that I did in my original question, which was to do this:

- name: Populate appended route list
  set_fact:
    found_added_routes: |- 
      {%- set blah = [] %}
        {%- for line in found_route_list %}
          {%- set _ = blah.append(line) %}
        {%- endfor %} 
      {{ blah }}

...the output was a string that had elements literally like this: u'default via 10.13.99.1 dev bond0.99'

So the string begins not with default... but rather with a literal 'u' and tick mark, sa: u'default...

The first two characters of the string are u and '. Not d and e as I was expecting.

My mistake!

Related