Modify JSON file

24 views
Skip to first unread message

bilel....@gmail.com

unread,
Nov 27, 2020, 1:00:36 PM11/27/20
to Ansible Project

Hello

 

I have 2 json files:

 

File Json1

{

    "name": "",

    "description": "",

    "spec": [

        {

            "question_name": "VM Name",

            "question_description": "",

            "required": true,

            "type": "text",

            "variable": "VM_Name",

            "min": 0,

            "max": 1024,

            "default": "",

            "choices": "",

            "new_question": true

        },

        {

            "question_name": "Select a Veeam Backup Job",

            "question_description": "",

            "required": true,

            "type": "multiplechoice",

            "variable": "Job_Name",

            "min": null,

            "max": null,

            "default": "",

            "choices": 

        }

    ]

}

 

 

File Json2

“Jobs_List_Data”:["JOB-1" , "JOB-2","JOB-3", "JOB-4"]

 

 

  I'm stuck in modifying the File Json1 with error:

 

TASK [json_modify] *******************************************************************************************************

<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root

<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'

<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253 && echo ansible-tmp-1606499676.6535082-233822-207184568804253="` echo /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253 `" ) && sleep 0'

Using module file /usr/local/lib/python3.6/site-packages/ansible/modules/json_modify.py

<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-233716vja5qmf5/tmphostnj3p TO /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/AnsiballZ_json_modify.py

<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/ /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/AnsiballZ_json_modify.py && sleep 0'

<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/AnsiballZ_json_modify.py && sleep 0'

<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/ > /dev/null 2>&1 && sleep 0'

The full traceback is:

  File "/tmp/ansible_json_modify_payload_zjd6hujn/ansible_json_modify_payload.zip/ansible/modules/json_modify.py", line 38, in main

  File "/usr/local/lib/python3.6/site-packages/jsonpointer.py", line 126, in resolve_pointer

    return pointer.resolve(doc, default)

  File "/usr/local/lib/python3.6/site-packages/jsonpointer.py", line 204, in resolve

    doc = self.walk(doc, part)

  File "/usr/local/lib/python3.6/site-packages/jsonpointer.py", line 272, in walk

    raise JsonPointerException("index '%s' is out of bounds" % (part, ))

fatal: [localhost]: FAILED! => {

    "changed": false,

    "invocation": {

        "module_args": {

            "action": "update",

            "append": null,

            "data": {

                "description": "",

                "name": "",

                "spec": [

                    {

                        "choices": "",

                        "default": "",

                        "max": 1024,

                        "min": 0,

                        "new_question": true,

                        "question_description": "",

                        "question_name": "VM Name",

                        "required": true,

                        "type": "text",

                        "variable": "VM_Name"

                    },

                    {

                        "choices": null,

                        "default": "",

                        "max": null,

                        "min": null,

                        "question_description": "",

                        "question_name": "Select a Veeam Backup Job",

                        "required": true,

                        "type": "multiplechoice",

                        "variable": "Job_Name"

                    }

                ]

            },

            "extend": null,

            "pointer": "/spec/2/choices/",

            "update": {

                "Backup_Jobs_Lists": [

                                  "JOB-1",

                                  " JOB-2",

                                  "JOB-3",

                                  " JOB-4"

                ]

            }

        }

    },

    "msg": "index '2' is out of bounds"

}

 

 

The final json file should be like this

 

 

Final Json:

{

    "name": "",

    "description": "",

    "spec": [

        {

            "question_name": "VM Name",

            "question_description": "",

            "required": true,

            "type": "text",

            "variable": "VM_Name",

            "min": 0,

            "max": 1024,

            "default": "",

            "choices": "",

            "new_question": true

        },

        {

            "question_name": "Select a Veeam Backup Job",

            "question_description": "",

            "required": true,

            "type": "multiplechoice",

            "variable": "Job_Name",

            "min": null,

            "max": null,

            "default": "",

            "choices": ["JOB-1" , "JOB-2", "JOB-3", "JOB-4"]

        }

    ]

}

 

 

Here is my code:

 

- hosts: localhost

  tasks: 

  - name: load json file

    include_vars:

      file: surveyjobs.json

      name: json1

 

  - debug:

      var: json1

 

  - name: load json file

    include_vars:

      file: Jobs_list.json

      name: json2

 

  - debug:

      var: json2

 

  - json_modify: 

      data: "{{json1}}"

      pointer: "/spec/2/choices/"

      action: update

      update: "{{ json2 }}" 

    register: result     

 

  - debug:

      var: result.result

 

 

Thanks for your help !

BILKAM

unread,
Nov 30, 2020, 2:28:35 AM11/30/20
to ansible...@googlegroups.com
Hello anyone can help me get n this below? 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0a756683-0c7a-45cc-8ac1-3f2fd89eb341n%40googlegroups.com.
--
Envoyé avec Gmail Mobile
Reply all
Reply to author
Forward
0 new messages