首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ansible中修改JSON

在Ansible中修改JSON
EN

Stack Overflow用户
提问于 2018-11-29 15:29:22
回答 2查看 19.3K关注 0票数 9

我有一个管理系统,在这里我们定义了维护数据来控制虚拟环境,其中一个选项是不同团队的VM关闭时间框架。现在,当创建新VM时,用户应该从可用的时限列表中选择VM可以在不中断的情况下关闭以进行工作轮班。我需要能够将这个时间表从我的数据库同步到工作模板调查。我被困在修改JSON调查中了。我尝试过这个帖子在ansible中修改json的最佳方法,但是得到了一个错误:

“异常”:“文件\"/tmp/ansible_1qa8eR/ansible_module_json_modify.py\",第38行,在main\n res =jsonpointer.resolve_pointer(数据、指针)\n文件\"/usr/lib/python2.7/site-packages/jsonpointer.py\",第126行中,在resolve_pointer\n返回pointer.resolve(doc,默认值)\n文件\"/usr/lib/python2.7/site-packages/jsonpointer.py\",第204行中,在解析\n doc = self.walk(doc,part)\n文件JsonPointerException第279行中,在walk\n中提出JsonPointerException(\“成员‘'%s’在%s\”% (part,doc)\n中找不到\n“,"msg":”成员'spec‘在{'stderr_lines':[],'changed':True,'end’中找不到

下面是我试图修改的JSON:

代码语言:javascript
复制
{
  "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": ""
}

我必须更新时间框架(在最后一个之前)选择:

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

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

代码语言:javascript
复制
- 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-29 19:29:17

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

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

如网站所述:

jq类似于对于JSON数据的sed --您可以使用它对结构化数据进行切片、过滤、映射和转换,就像sed、awk、grep和朋友让您玩文本一样容易。

我把你的表演缩小到了最低限度的工作解决方案,结果也是一样。jq可执行文件必须位于正在运行的系统的PATH中。您可以根据您的需要定制它。

代码语言:javascript
复制
---
- 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

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

票数 8
EN

Stack Overflow用户

发布于 2018-11-30 02:57:48

你的剧本里有几个小问题

  1. 您正在将json数据从文件中加载为字符串,而不是json/dict。而不是命令/cat任务,使用它将数据加载到:
  • set_fact: surveySpec:“{查找(‘file’,sharedDataPathFile) \ from_json }”
  1. 您可能遇到的另一个问题是,您试图只更新choices值(即字符串),而不是choices dict项。只需在您的json_modify指针中进行一个小的更改:
  • json_modify: data:"{{ surveySpec }}“指针:"/spec/5”操作:更新更新:"{{ new_choices }}“寄存器:结果

另一件事..。数组索引是5,而不是6。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53542389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档