Anisble output specific key pair value from local json file

60 views
Skip to first unread message

JJ

unread,
Mar 27, 2023, 1:45:03 PM3/27/23
to Ansible Project

I am new to Anisble and I have been trying to get Anisble to take the output of a local json file and read it, store the information and then output the data. I think I was successful because it does output the data in the file. What I am having trouble with is getting just a specific piece of data to display so I know Anisble is parsing the right section of the json file so I can pass it to a variable in the playbook. I feel I have tried almost everything but with no success.

JSON EXAMPLE

}, "receiving_dns_records": [ { "cached": [], "priority": "10", "record_type": "MX", "valid": "unknown", "value": "mxa.domain.org" }, 

Playbook:

---
- name: Ansible connection
  hosts: localhost        
  connection: local

- name: Read local JSON file
  hosts: localhost
  vars:
    jsondata: "{{ lookup('file', '/home/name/appdir/output.json') | from_json }}"
 
  tasks:
    - name: Create dictionary
      set_fact:
        jsondata: "{{ jsondata }}"

   
    - name: Print variable JSON data
      debug:
        var: jsondata
               


    - name: Output specific JSON data
      debug:
        msg: "this is where I get lost I have tried JMES Query and Debug MSG but every example I see fails"

JJ

unread,
Mar 27, 2023, 3:50:56 PM3/27/23
to Ansible Project
Hope this helps

server]$ ansible-playbook test-json-output.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible connection] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [localhost]

PLAY [Read local Mailgun JSON file] **************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Create dictionary] *************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print variable JSON data] ******************************************************************************************************************************************************************************
ok: [localhost] => {
    "jsondata": {

       
       },
        "receiving_dns_records": [
            {
                "cached": [],
                "priority": "10",
                "record_type": "MX",
                "valid": "unknown",
                "value": "mxa.mailgun.org"
            },
           

TASK [Output specific JSON data] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Error in jmespath.search in json_query filter plugin:\n'dict object' has no attribute '

JJ

unread,
Mar 27, 2023, 5:08:40 PM3/27/23
to Ansible Project
I am able to get the output.json to display but looking only to display the record type and priority from the json file then take that output to a variable in the playbook.
Reply all
Reply to author
Forward
0 new messages