【问题标题】:How to pass array of arguments to azure vm powershell extension from ansible playbook?如何从ansible playbook将参数数组传递给azure vm powershell扩展?
【发布时间】:2019-11-02 15:17:16
【问题描述】:

我正在尝试将“字符串”类型的数组参数从 ansible playbook 传递到 powershell 脚本,该脚本需要作为 vm 扩展运行在 azure cloud 中运行的 VM。下面是 ansible playbook 代码

  - 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": "argument protected_settings 是 'str' 类型,我们是 无法转换为字典:无法将字符串评估为字典"}

问题是我应该如何将这些参数的数组从 ansible 传递给 powershell 代码?

【问题讨论】:

    标签: azure powershell ansible


    【解决方案1】:

    基于the source code,似乎无论文档显示什么,他们实际上都希望dict 出现在该参数中,因此请尝试提供该数据结构,看看情况是否有所改善

    - name: Create VM extension
      azure_rm_virtualmachineextension:
        protected_settings:
          commandToExecute: "powershell -ExecutionPolicy Unrestricted -File {{PowershellUserScriptFileName }} -arg1 {{ Arrayofstring1 }} -arg2 {{ Arrayofstring2 }} -arg3 {{ Arrayofstring3 }}"
    

    【讨论】:

    • 嗨,丹尼尔,您能否从您的答案中删除 '[ ' 和 ']' ......代码 commandToExecute: "powershell -ExecutionPolicy Bypass -File somefile.ps1" 对我来说很好,而不是使用 '[' 和 ']'...感谢您的帮助
    • 我用它们是因为你用了它们;在发布代码之前,我没有一个 azure 帐户来实际测试该代码
    猜你喜欢
    • 2017-12-25
    • 2010-10-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2021-03-21
    • 1970-01-01
    相关资源
    最近更新 更多