使用Ansible自动化创建AWS Workspaces时遭遇CLI输入JSON解析错误的问题求助
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:
Incorrect JSON Structure for AWS CLI:
Theaws workspaces create-workspacescommand expects the--cli-input-jsonto contain a top-levelWorkspacesarray (even for a single workspace). Your current template outputs a standalone workspace object, which the CLI can't parse as the required map structure.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:
Approach 1: Render Template to File (Recommended)
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

