ansible parsing text file and copy section

Viewed 74

I would like to parse some text section. Here is example text.

;; Description: 
define condition __CondList1TOMB2RAD_dst
    url.address="46.249.255.139/32"
end condition __CondList1TOMB2RAD_dst

;; Description: 
define subnet TOMB2RAD_src
    100.72.59.65/32
    100.72.59.66/32
    20.253.139.54/32
    20.253.19.56/32
    
end subnet TOMB2RAD_src

define condition TOMB2RAD_dst
    condition=__CondList1TOMB2RAD_dst   
end condition TOMB2RAD_dst

I would like to parse text which is between define subnet TOMB2RAD_src and end subnet TOMB2RAD_src so the result should be all IPs between.

100.72.59.65/32
100.72.59.66/32
20.253.139.54/32
20.253.19.56/32
1 Answers

For example, given the text in the variable text_section,

lines: "{{ text_section.split('\n')|map('trim')|list }}"
tag_begin: define subnet TOMB2RAD_src
tag_end: end subnet TOMB2RAD_src
line_begin: "{{ lines.index(tag_begin) }}"
line_end: "{{ lines.index(tag_end) }}"
TOMB2RAD_src: "{{ lines[line_begin|int + 1 : line_end|int]|select() }}"

will create the list

TOMB2RAD_src:
  - 100.72.59.65/32
  - 100.72.59.66/32
  - 20.253.139.54/32
  - 20.253.19.56/32
    - assert:
        that: TOMB2RAD_src|map('ansible.netcommon.ipaddr') is all
        fail_msg: IP address(es) not valid.
        success_msg: IP address(es) valid.
Related