在 Ansible 中修改 JSON

lar*_*ybg 7 python json ansible

我有一个管理系统,我们在其中定义维护数据来控制虚拟环境,其中一个选项是不同团队的 VM 关闭时间范围。现在,当创建新的 VM 时,用户应该从可用的时间范围列表中选择他/她的 VM 可以在不中断工作班次的情况下关闭的时间。我需要能够将这个时间范围列表从我的数据库同步到工作模板调查。我一直在修改 JSON 调查。我已经尝试了这篇文章在ansible中修改json的最佳方法,但出现错误:

“异常”:“文件\”/tmp/ansible_1qa8eR/ansible_module_json_modify.py\”,第38行,在main\n res = jsonpointer.resolve_pointer(data, pointer)\n File \”/usr/lib/python2.7/ site-packages/jsonpointer.py\", line 126, in resolve_pointer\n return pointer.resolve(doc, default)\n File \"/usr/lib/python2.7/site-packages/jsonpointer.py\",第 204 行,在解析中\n doc = self.walk(doc, part)\n 文件 \"/usr/lib/python2.7/site-packages/jsonpointer.py\",第 279 行,在 walk 中\n 引发 JsonPointerException (\"在 %s 中找不到成员 '%s'\" % (part, doc))\n", "msg": "在 {'stderr_lines': [], 'changed' 中找不到成员 'spec':真的,'结束'

这是我要修改的 JSON:

{
  "spec": [
    {
      "question_description": "", 
      "min": 0, 
      "default": "Test text", 
      "max": 4096, 
      "required": true, 
      "choices": "", 
      "variable": "_t", 
      "question_name": "Note", 
      "type": "textarea"
    }, 
    {
      "required": true, 
      "min": null, 
      "default": "", 
      "max": null, 
      "question_description": "appliance id", 
      "choices": "Unconfigured\n600,qvmProcessor/applianceexemptions,all", 
      "new_question": true, 
      "variable": "appid", 
      "question_name": "Appliance ID", 
      "type": "multiplechoice"
    }, 
    {
      "required": true, 
      "min": null, 
      "default": "", 
      "max": null, 
      "question_description": "Select version", 
      "choices": "1.2.3\n1.2.4\n1.2.5", 
      "new_question": true, 
      "variable": "version", 
      "question_name": "App Version", 
      "type": "multiplechoice"
    }, 
    {
      "required": true, 
      "min": 0, 
      "default": "", 
      "max": 1024, 
      "question_description": "", 
      "choices": "", 
      "new_question": true, 
      "variable": "newVMIP", 
      "question_name": "IP for new VM", 
      "type": "text"
    }, 
    {
      "required": true, 
      "min": 0, 
      "default": "", 
      "max": 1024, 
      "question_description": "", 
      "choices": "", 
      "new_question": true, 
      "variable": "requesterEmail", 
      "question_name": "Requester's email", 
      "type": "text"
    }, 
    {
      "required": true, 
      "min": null, 
      "default": "", 
      "max": null, 
      "question_description": "Select the timeframe for automatic VM shutdown. ***NOTE*** EST Time is in 24 hour format", 
      "choices": "23:00-02:00\n02:00-04:00\n04:00-06:00\n00:00-02:00", 
      "new_question": true, 
      "variable": "powerOFF_TimeFrame", 
      "question_name": "Power OFF window", 
      "type": "multiplechoice"
    }, 
    {
      "required": true, 
      "min": 0, 
      "default": 5, 
      "max": 30, 
      "question_description": "The VM will be deleted after # of days specified (default=5).", 
      "choices": "", 
      "new_question": true, 
      "variable": "vmNumReservedDays", 
      "question_name": "Keep VM for # of days", 
      "type": "integer"
    }
  ], 
  "description": "", 
  "name": ""
}
Run Code Online (Sandbox Code Playgroud)

我必须更新时间范围(前一个)选择:

"选择": "23:00-02:00\n02:00-04:00\n04:00-06:00\n00:00-02:00",

这是我的代码。我可以直接读取变量,但现在我只是将 JSON 保存到文件中:

- name: Sync Power Schedules From Database to Survey Spec
  hosts: awxroot
  gather_facts: no

  vars:
    new_choices: {}

  tasks:

    - name: Set shared directory name
      set_fact: 
        sharedDataPath: /var/tmp/survey

    - name: Set shared file path name
      set_fact: 
        sharedDataPathFile: "{{sharedDataPath}}/s.json"

    - name: Create directory to share data
      file:
        path: "{{ sharedDataPath }}"
        state: directory

    - name: Load Survey Spec to file
      shell: 'tower-cli job_template survey 70 > "{{ sharedDataPathFile }}"'



    - name: Make sure the survey spec file exists
      stat:
        path: "{{ sharedDataPathFile }}"
      register: isFileExists

    - name: Fail if file is not there
      fail:
        msg: "Cannot find survey spec exported file"
      when: isFileExists == False

    - name: Read exception file to a variable
      command: cat "{{ sharedDataPathFile }}"
      register: surveySpec
      when: isFileExists.stat.exists == True


    - name: Setting key
      set_fact:
        choices_key: "choices"

    - name: Setting new values
      set_fact:
        choices_value: "23:00-02:00\n02:00-04:00\n04:00-06:00\n00:00-04:00"

    - name: Create dictionary
      set_fact:
        new_choices: "{{ new_choices | combine({choices_key: choices_value}) }}"


    - json_modify:
        data: "{{ surveySpec }}"
        pointer: "/spec/6/choices"
        action: update
        update: "{{new_choices}}"
      register: result

    - debug:
        var: result.result
Run Code Online (Sandbox Code Playgroud)

JGK*_*JGK 8

不是直接回答您关于错误的问题,modify_json而是一个有效的解决方案。

我会和jq一起去。jq是一个轻量级且灵活的命令行 JSON 处理器,几乎适用于所有 Linux 发行版。如果没有,请使用没有依赖项的预构建二进制文件

正如网站所说:

jq 就像用于 JSON 数据的 sed - 您可以使用它来切片、过滤、映射和转换结构化数据,就像 sed、awk、grep 和朋友让您处理文本一样轻松。

我将您的游戏缩小到最小的工作解决方案,结果相同。该jq可执行文件必须在PATH其上运行的系统。随意根据您的需要定制它。

---
- name: Sync Power Schedules From Database to Survey Spec
  hosts: localhost
  gather_facts: no

  vars:
    choices_key: ".spec[6].choices"
    choices_value: "23:00-02:00\n02:00-04:00\n04:00-06:00\n00:00-04:00"
    json_file: "{{playbook_dir}}/s.json"

  tasks:

  - name: "modify json"
    command: >
      jq "{{choices_key}}=\"{{choices_value}}\"" "{{json_file}}"
    register: json

  - debug:
      var: json.stdout
Run Code Online (Sandbox Code Playgroud)

我认为这作为带有额外json_modify.py模块的解决方案更优雅。有关更多信息,jq请参阅手册页