我尝试使用regex_replace从包含接口其他信息的字符串列表中提取接口名称。
我试过这个剧本:
---
- name: Ansible regex mapping
hosts: localhost
vars:
interfaces:
- "1/1/18 19 8,19,108,142,160,204,208,220,228,240,254"
- "1/1/20 19 8,19,108,142,160,204,208,220,228,240,254"
- "1/1/21 19 8,19,108,142,160,204,208,220,228,240,254"
- "1/1/32 19 8,19,108,142,160,204,208,220,228,240,254"
tasks:
- name: Extract interface in list
set_fact:
trunk_interfaces: "{{ interfaces | map('regex_replace', '^(\\S+)', '\\1') | list }}"
- debug:
msg: "{{ trunk_interfaces }}"
我期待着:
msg:
- 1/1/18
- 1/1/20
- 1/1/21
- 1/1/32
但我得到了:
msg:
- 1/1/18 19 8,19,108,142,160,204,208,220,228,240,254
- 1/1/20 19 8,19,108,142,160,204,208,220,228,240,254
- 1/1/21 19 8,19,108,142,160,204,208,220,228,240,254
- 1/1/32 19 8,19,108,142,160,204,208,220,228,240,254
2条答案
按热度按时间hm2xizp91#
你不需要 regex_replace。相反,拆分项目(默认情况下为空格),并获取第一个字段
给你想要的
给予
请参阅:使用Ansible解析半结构化文本
e3bfsja22#