You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ansible自动化创建AWS Workspaces时遭遇CLI输入JSON解析错误的问题求助

Fixing AWS Workspaces Creation Error with Ansible

Let's break down the issues causing your error and walk through the fixes step by step.

Root Causes of the Error

The error Error parsing parameter '--cli-input-json': Invalid type: expecting map, received <class 'str'> comes from two key mistakes:

  1. Incorrect JSON Structure for AWS CLI:
    The aws workspaces create-workspaces command expects the --cli-input-json to contain a top-level Workspaces array (even for a single workspace). Your current template outputs a standalone workspace object, which the CLI can't parse as the required map structure.

  2. Quoting Conflict in Ansible:
    You wrapped the lookup output in single quotes, but the lookup itself uses single quotes for the template path. This causes Ansible to truncate the string incorrectly, passing a malformed string to the AWS CLI instead of valid JSON.

Step 1: Fix the JSON Template

Rename your template to workspaces.json.j2 (to make the Jinja2 template explicit) and update its content to include the required Workspaces array:

{
  "Workspaces": [
    {
      "DirectoryId": "{{ DirectoryId }}",
      "UserName": "{{ UserName }}",
      "BundleId": "{{ BundleId }}",
      "UserVolumeEncryptionEnabled": false,
      "RootVolumeEncryptionEnabled": false,
      "WorkspaceProperties": {
        "RunningMode": "ALWAYS_ON",
        "RootVolumeSizeGib": 80,
        "UserVolumeSizeGib": 10,
        "ComputeTypeName": "PERFORMANCE"
      },
      "Tags": [
        {
          "Key": "",
          "Value": ""
        }
      ]
    }
  ]
}

Note: I removed RunningModeAutoStopTimeoutInMinutes because it's only valid when RunningMode is set to AUTO_STOP—it will be ignored (or cause warnings) for ALWAYS_ON.

Step 2: Fix the Ansible Playbook

We have two reliable approaches to fix the command execution:

This avoids quoting issues entirely by first rendering the template to a temporary file, then passing that file to the AWS CLI:

- hosts: windows
  gather_facts: false
  vars:
    BundleId: "wpc-30g6060"
    DirectoryId: "d-8039964"
    UserName: "testing"
  tasks:
    - name: Render Workspaces config template
      template:
        src: workspaces.json.j2
        dest: /tmp/workspaces_config.json
        mode: 0644
      delegate_to: localhost

    - name: Deploy Workspaces
      command: aws workspaces create-workspaces --cli-input-json file:///tmp/workspaces_config.json
      delegate_to: localhost
      register: create_result
      changed_when: create_result.rc == 0

    - name: Clean up temporary config file
      file:
        path: /tmp/workspaces_config.json
        state: absent
      delegate_to: localhost

Approach 2: Fix Quoting in the Command

If you prefer to keep everything inline, use double quotes for the outer wrapper and ensure the JSON is properly serialized:

- hosts: windows
  gather_facts: false
  vars:
    BundleId: "wpc-30g6060"
    DirectoryId: "d-8039964"
    UserName: "testing"
  tasks:
    - name: Deploy Workspaces
      command: >
        aws workspaces create-workspaces --cli-input-json "{{ lookup('template', 'workspaces.json.j2') | to_json }}"
      delegate_to: localhost

Step 3: Verify the Fix

Run your playbook again. If you still encounter issues, add -v to the ansible-playbook command to see the exact JSON being passed to the AWS CLI—this will help catch any remaining formatting errors.

内容的提问来源于stack exchange,提问作者Niko Belly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 13:47:48