0

Im using ansible 2.9.2 and i have this playbook :

  register: output

- debug: msg={{ output.instance }}

which gives this output:

TASK [debug] ***************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "annotation": "",
        "current_snapshot": null,
        "customvalues": {},
        "guest_consolidation_needed": false,
        "guest_question": null,
        "guest_tools_version": "0",
        "hw_cluster": null,
        "hw_datastores": [
            "V1",
        ],
        "hw_esxi_host": "10.10.101.10",
        "hw_eth0": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "",
            "macaddress": "00:00:00:00:00:51",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "Vlan1"

How can i get the output to give me onlythe "ipaddresses": null? I tried this :

  • debug: msg={{ output.instance | json_query('hw_eth0{}.ipaddresses') }}

but got an error

FAILED! => {"msg": "JMESPathError in json_query filter plugin:\\ninvalid token: Parse error at column 7, token \\"{\\" (LBRACE), for expression:\\n\\"hw_eth0{}.ipaddresses
Saeid Babaei
  • 481
  • 2
  • 16
Batchen Regev
  • 685
  • 1
  • 7
  • 28

1 Answers1

0

Your jmespath expression is wrong. You can check the doc for more details

The following should work

- debug:
    msg: "{{ output.instance | json_query('hw_eth0.ipaddresses') }}"

Meanwhile, you really don't need json_query in this situation where you just have to read the value in the hash:

- debug:
    var: output.instance.hw_eth0.ipaddresses

Note that in the output, ansible will automatically transform the json null value to an empty string.

From the name, I guess this parameter is supposed to return a list when not empty. If you ever need to check on that in your playbook, the best practice is to verify the parameter length, e.g:

- name: Do something only when there are configured IPs
  debug:
    msg: There is at least one IP configured
  when: output.instance.hw_eth0.ipaddresses | length > 0
Zeitounator
  • 38,476
  • 7
  • 53
  • 66