Home > Back-end >  How to find a string in json output in Ansible/yaml
How to find a string in json output in Ansible/yaml

Time:06-29

I have an API call that puts out the following JSON data:

"data": [
      {
        "vrfId": "1",
        "name": "test",
        "rd": null,
        "description": null,
        "sections": "3",
        "editDate": null,
        "customer_id": "1"
      },
      {
        "vrfId": "2",
        "name": "vrf-XYZ-01",
        "rd": null,
        "description": "test vrf for XYZ",
        "sections": "3;4",
        "editDate": "2022-06-28 13:03:47",
        "customer_id": null
      },
      {
        "vrfId": "3",
        "name": "vrf-ABC-01",
        "rd": null,
        "description": "test vrf for ABC ",
        "sections": "3;4",
        "editDate": "2022-06-28 13:04:03",
        "customer_id": null
      },
      {
        "vrfId": "4",
        "name": "vrf-KLM-01",
        "rd": null,
        "description": "test vrf for KLIM ",
        "sections": "3;4",
        "editDate": null,
        "customer_id": null
      }
    ]

I want to loop through it with an Ansible task, and then search if a 3 letter code (ABC, KLM, XYZ..) is in the name value, and save that corresponding vrfId

To try it out with a debug task I made the following:

- name: debug vrfs with tenant name
  debug:
    msg: "{{ item }}"
  with_items: "{{ vrf.json.data }}"
  when: "{{ tenant }}" in "{{ item.name }}" 

Where vrf.json.data is the JSON data above. Variable tenant is the search string, XYZ in this case. This gives me the following error:

Syntax Error while loading YAML.
  did not find expected key
The error appears to be in '/runner/project/playbooks/nsx-t/roles/ipam_modify_subnet/tasks/main.yml': line 27, column 24, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
  with_items: "{{ vrf.json.data }}"
  when: "{{ tenant }}" in "{{ item.name }}"
                       ^ here

I

CodePudding user response:

First, you have a syntax error in your when statement: a when statement is a jinja expression, which means you never use {{...}} template markers. You want your task to look like this:

- name: debug vrfs with tenant name
  debug:
    msg: "{{ item }}"
  with_items: "{{ vrf.json.data }}"
  when: tenant in item.name

We can test that change with the following playbook, assuming that your sample data is available in the vrf variable: sample data:

- hosts: localhost
  gather_facts: false
  vars:
    tenant: XYZ
  tasks:
    - debug:
        msg: "{{ item }}"
      loop: "{{ vrf.json.data }}"
      when: tenant in item.name

Running this outputs:

TASK [debug] ******************************************************************************************************************************************************************************************************
skipping: [localhost] => (item={'vrfId': '1', 'name': 'test', 'rd': None, 'description': None, 'sections': '3', 'editDate': None, 'customer_id': '1'})
ok: [localhost] => (item={'vrfId': '2', 'name': 'vrf-XYZ-01', 'rd': None, 'description': 'test vrf for XYZ', 'sections': '3;4', 'editDate': '2022-06-28 13:03:47', 'customer_id': None}) => {
    "msg": {
        "customer_id": null,
        "description": "test vrf for XYZ",
        "editDate": "2022-06-28 13:03:47",
        "name": "vrf-XYZ-01",
        "rd": null,
        "sections": "3;4",
        "vrfId": "2"
    }
}
skipping: [localhost] => (item={'vrfId': '3', 'name': 'vrf-ABC-01', 'rd': None, 'description': 'test vrf for ABC ', 'sections': '3;4', 'editDate': '2022-06-28 13:04:03', 'customer_id': None})
skipping: [localhost] => (item={'vrfId': '4', 'name': 'vrf-KLM-01', 'rd': None, 'description': 'test vrf for KLIM ', 'sections': '3;4', 'editDate': None, 'customer_id': None})

Note that you can also use Ansible filters to extract the desired records from your data. For example, using the json_query filter, which performs jmespath queries against your data:

- debug:
    msg: "{{ vrf.json.data|json_query('[?contains(name, `XYZ`)]') }}"

This outputs:

TASK [debug] ********************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "customer_id": null,
            "description": "test vrf for XYZ",
            "editDate": "2022-06-28 13:03:47",
            "name": "vrf-XYZ-01",
            "rd": null,
            "sections": "3;4",
            "vrfId": "2"
        }
    ]
}
  • Related