You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible中解析AWX查询JSON输出的URL格式数据并提取指定内容

Hey there! Since you’ve already cracked this in Python, translating it to Ansible is straightforward—we can use either Jinja2’s string splitting filters or regex matching to pull out those dynamic xxxx segments from your dn string. Let’s walk through both approaches with examples.

Using Split Filters (For Fixed String Structure)

If your dn format is consistent, splitting the string step-by-step with Jinja2’s split filter is super intuitive—just like you’d do in Python. Let’s assume your dn value is stored in a variable called dn_string.

Here’s how to extract each target segment:

  1. Extract xxxxxxxxxx from accportgrp-xxxxxxxxxx:
    Split the string by /, grab the 4th segment (index 3), then split that by - and take the second part:
    {{ dn_string.split('/')[3].split('-')[1] }}
    
  2. Extract xxxxxxxxxxxx from accportprof-xxxxxxxxxxxx:
    First, isolate the inner path inside the square brackets, then split similarly:
    {{ (dn_string.split('/')[-1].split('[')[1].split(']')[0]).split('/')[3].split('-')[1] }}
    
  3. Extract xxxxxx from hports-xxxxxx-typ-range:
    Use the same inner path isolation, then split the 5th segment (index 4) of that path:
    {{ (dn_string.split('/')[-1].split('[')[1].split(']')[0]).split('/')[4].split('-')[1] }}
    

Using Regex Search (For Flexible String Structure)

If your dn might have minor format variations, regex is more robust. Ansible’s regex_search filter lets you target exactly the patterns you need. We’ll use capture groups to pull out the dynamic parts:

  1. Extract xxxxxxxxxx:
    Match the pattern accportgrp- followed by word characters, then grab the captured group:
    {{ dn_string | regex_search('accportgrp-(\\w+)', '\\1') | first }}
    
  2. Extract xxxxxxxxxxxx:
    Target the accportprof- pattern similarly:
    {{ dn_string | regex_search('accportprof-(\\w+)', '\\1') | first }}
    
  3. Extract xxxxxx:
    Target the hports- pattern:
    {{ dn_string | regex_search('hports-(\\w+)', '\\1') | first }}
    
    Note: We use \\w instead of \w because Jinja2 requires escaping backslashes.

Full Example Playbook

Here’s a complete playbook to test both methods:

- name: Extract dynamic segments from dn string
  hosts: localhost
  vars:
    dn_string: "uni/infra/funcprof/accportgrp-xxxxxxxxxx/rtaccBaseGrp-[uni/infra/accportprof-xxxxxxxxxxxx/hports-xxxxxx-typ-range]"
  tasks:
    - name: Extract using split filters
      ansible.builtin.debug:
        msg:
          - "accportgrp ID: {{ dn_string.split('/')[3].split('-')[1] }}"
          - "accportprof ID: {{ (dn_string.split('/')[-1].split('[')[1].split(']')[0]).split('/')[3].split('-')[1] }}"
          - "hports ID: {{ (dn_string.split('/')[-1].split('[')[1].split(']')[0]).split('/')[4].split('-')[1] }}"

    - name: Extract using regex search
      ansible.builtin.debug:
        msg:
          - "accportgrp ID: {{ dn_string | regex_search('accportgrp-(\\w+)', '\\1') | first }}"
          - "accportprof ID: {{ dn_string | regex_search('accportprof-(\\w+)', '\\1') | first }}"
          - "hports ID: {{ dn_string | regex_search('hports-(\\w+)', '\\1') | first }}"

Run this playbook, and you’ll see all three dynamic segments extracted in the debug output.

内容的提问来源于stack exchange,提问作者Satwik Shalom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:27:27