ansible解析文本文件和复制部分

ansible parsing text file and copy section

我想解析一些文本部分。这是示例文本。

;; 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

我想解析定义子网 TOMB2RAD_src 和结束子网 TOMB2RAD_src 之间的文本 所以结果应该是所有 IP 之间。

100.72.59.65/32
100.72.59.66/32
20.253.139.54/32
20.253.19.56/32

例如,给定变量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() }}"

将创建列表

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.