在Ansible中使用regex_replace提取特定字符串

2eafrhcq  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(122)

我尝试使用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
hm2xizp9

hm2xizp91#

你不需要 regex_replace。相反,拆分项目(默认情况下为空格),并获取第一个字段

trunk_interfaces: "{{ interfaces|map('split')|map('first')|list }}"

给你想要的

trunk_interfaces:
    - 1/1/18
    - 1/1/20
    - 1/1/21
    - 1/1/32
  • 完整的测试剧本示例
- 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"

    trunk_interfaces: "{{ interfaces|map('split')|map('first')|list }}"
      
  tasks:

    - debug:
        var: trunk_interfaces
  • 您可以进一步拆分项目
arr: "{{ interfaces|map('split')|list }}"
  f1: "{{ arr|map(attribute='1')|list }}"
  f2: "{{ arr|map(attribute='2')|list }}"
  f2_arr: "{{ f2|map('split', ',')|map('map', 'int')|list }}"

给予

arr:
    - [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']

  f1:
  - '19'
  - '19'
  - '19'
  - '19'

  f2:
  - 8,19,108,142,160,204,208,220,228,240,254
  - 8,19,108,142,160,204,208,220,228,240,254
  - 8,19,108,142,160,204,208,220,228,240,254
  - 8,19,108,142,160,204,208,220,228,240,254

  f2_arr:
    - [8, 19, 108, 142, 160, 204, 208, 220, 228, 240, 254]
    - [8, 19, 108, 142, 160, 204, 208, 220, 228, 240, 254]
    - [8, 19, 108, 142, 160, 204, 208, 220, 228, 240, 254]
    - [8, 19, 108, 142, 160, 204, 208, 220, 228, 240, 254]

请参阅:使用Ansible解析半结构化文本

e3bfsja2

e3bfsja22#

---
- 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 }}"

相关问题