我正试图将类型为"string"的数组参数从ansible剧本传递到powershell脚本,该脚本需要作为虚拟机扩展在azure云中运行。下面是易翻译的剧本代码
- name: Create VM extension
azure_rm_virtualmachine_extension:
name: "Extension"
location: "{{ var_location }}"
resource_group: "{{ var_ResourceGroup }}"
virtual_machine_name: "{{ var_vmname }}"
publisher: Microsoft.Compute
state: present
virtual_machine_extension_type: CustomScriptExtension
type_handler_version: "1.9"
auto_upgrade_minor_version: true
settings: '{"fileUris": ["{{ var_powershellscriptUrl }}"]}'
protected_settings: '{"commandToExecute": "[powershell -ExecutionPolicy Unrestricted -File {{PowershellUserScriptFileName }} -arg1 {{ Arrayofstring1 }} -arg2 {{ Arrayofstring2 }} -arg3 {{ Arrayofstring3 }} ]"}'
ignore_errors: true
以下代码的输出
- debug:
msg: {{ Arrayofstring1 }}
is "VM1", "VM2", "VM3"
执行剧本时出现的错误是
"msg":"参数protected_settings的类型为'str',而我们无法转换为dict:无法将字符串计算为dictionary"}
问题是我应该如何将这些参数的数组从ansible传递到powershell代码?
根据源代码,无论文档显示什么,他们实际上都希望dict
出现在该参数中,所以尝试提供该数据结构,看看是否有所改进
- name: Create VM extension
azure_rm_virtualmachineextension:
protected_settings:
commandToExecute: "powershell -ExecutionPolicy Unrestricted -File {{PowershellUserScriptFileName }} -arg1 {{ Arrayofstring1 }} -arg2 {{ Arrayofstring2 }} -arg3 {{ Arrayofstring3 }}"