<p>I am trying to collect IP addresses of the pods which has 3 network interfaces attached to. From  the output of <code>kubectl get pod .. -o yaml</code>, I would like to collect the IPs of 2nd and 3rd interfaces including pod-name.</p>
<p><code>kubectl describe pod</code> data:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      k8s.v1.cni.cncf.io/network-status: |-
        [{
            &quot;name&quot;: &quot;default-cni-network&quot;,
            &quot;ips&quot;: [
                &quot;10.244.160.191&quot;
            ],
            &quot;default&quot;: true,
            &quot;dns&quot;: {}
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov1&quot;,
            &quot;interface&quot;: &quot;net1&quot;,
            &quot;ips&quot;: [
                &quot;10.56.217.100&quot;
            ],
            &quot;mac&quot;: &quot;6a:58:82:8a:93:7b&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:02.7&quot;
                }
            }
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov2&quot;,
            &quot;interface&quot;: &quot;net2&quot;,
            &quot;ips&quot;: [
                &quot;10.56.218.100&quot;
            ],
            &quot;mac&quot;: &quot;66:a2:70:eb:5e:04&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:14.0&quot;
                }
            }
        }]
      k8s.v1.cni.cncf.io/networks: minio-sriov1,minio-sriov2
      k8s.v1.cni.cncf.io/networks-status: |-
        [{
            &quot;name&quot;: &quot;default-cni-network&quot;,
            &quot;ips&quot;: [
                &quot;10.244.160.191&quot;
            ],
            &quot;default&quot;: true,
            &quot;dns&quot;: {}
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov1&quot;,
            &quot;interface&quot;: &quot;net1&quot;,
            &quot;ips&quot;: [
                &quot;10.56.217.100&quot;
            ],
            &quot;mac&quot;: &quot;6a:58:82:8a:93:7b&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:02.7&quot;
                }
            }
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov2&quot;,
            &quot;interface&quot;: &quot;net2&quot;,
            &quot;ips&quot;: [
                &quot;10.56.218.100&quot;
            ],
            &quot;mac&quot;: &quot;66:a2:70:eb:5e:04&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:14.0&quot;
                }
            }
        }]
      kubernetes.io/psp: node-exporter
      meta.helm.sh/release-name: minio-tenant
      meta.helm.sh/release-namespace: minio-tenant
      min.io/revision: &quot;0&quot;
    creationTimestamp: &quot;2022-04-27T08:09:45Z&quot;
    generateName: minio-tenant-ss-0-
    labels:
      app: minio
      app.kubernetes.io/managed-by: Helm
      controller-revision-hash: minio-tenant-ss-0-7976bbfbc6
      statefulset.kubernetes.io/pod-name: minio-tenant-ss-0-0
  spec:
    affinity:
    containers:
    - args:
      - :9090
      env:
      - name: MINIO_UPDATE
</code></pre>
<p>Using this regular expression:</p>
<pre class="lang-regex prettyprint-override"><code>networks-status:[\s\S]*?
(?:&quot;)(name)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s]*)
(?:&quot;)(interface)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s\S]*?)
(?:&quot;)(ips)(?:&quot;:\s*\[\s*&quot;)([\S]*)(?:&quot;)
[\s\S]*?(pod-name)(?::\s)([\S]*)
</code></pre>
<p>I would like to get the following output:</p>
<pre class="lang-none prettyprint-override"><code>name
minio-tenant/minio-sriov1
interface
net1
ips
10.56.217.100
name
minio-tenant/minio-sriov2
interface
net2
ips
10.56.218.100
pod-name
minio-tenant-ss-0-0
</code></pre>
<p>It gets me:</p>
<pre class="lang-none prettyprint-override"><code>name
minio-tenant/minio-sriov1
interface
net1
ips
10.56.217.100
pod-name
minio-tenant-ss-0-0
</code></pre>
<p>There is missing portion of IP:</p>
<pre class="lang-yaml prettyprint-override"><code>name
minio-tenant/minio-sriov2
interface
net2
ips
10.56.218.100
</code></pre>
<p>If I use <code>*</code> or <code>+</code> to repeat on the data section, I am getting unexpected result.</p>
<pre class="lang-regex prettyprint-override"><code>networks-status:[\s\S]*?
(
(?:&quot;)(name)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s]*)
(?:&quot;)(interface)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s\S]*?)
(?:&quot;)(ips)(?:&quot;:\s*\[\s*&quot;)([\S]*)(?:&quot;)
)*
[\s\S]*?(pod-name)(?::\s)([\S]*)
</code></pre>
<p>Can you give some hints how I can get the expected results?</p>

spark
<p>Don't use regular expression for this, it is a little bit like <a href="https://stackoverflow.com/a/1732454/2123530">trying to use them to parse HTML</a>, it is a non sense, especially because Ansible is very JSON capable. Your issue here lies in the fact that those JSON snippets are in string in your YAML dictionary, but you can use the <code>from_json</code> filter in order to query them.</p>
<p>Here, I am using a <code>json_query</code> and JMESPath to retrieve the objects as you where expecting them:</p>
<pre class="lang-yaml prettyprint-override"><code>- debug:
    var: &gt;-
      {
        'networks': _metadata.annotations['k8s.v1.cni.cncf.io/network-status']
          | from_json
          | json_query('[?default != `true`].{
            name: name,
            interface: interface,
            ips: ips
          }'),
        'pod-name': _metadata.labels['statefulset.kubernetes.io/pod-name']
      }
  vars:
    _metadata: &quot;{{ k8s.stdout['items'].0.metadata }}&quot;

</code></pre>
<p>This would yield:</p>
<pre class="lang-yaml prettyprint-override"><code>networks:
- interface: net1
  ips:
  - 10.56.217.100
  name: minio-tenant/minio-sriov1
- interface: net2
  ips:
  - 10.56.218.100
  name: minio-tenant/minio-sriov2
pod-name: minio-tenant-ss-0-0
</code></pre>
