Run command in linux vm in azure using python sdk

我怕爱的太早我们不能终老 提交于 2020-08-02 20:47:29

问题


I have found that azure python sdk has provided following method for running command in linux vm.

from azure.mgmt.compute import compute_management_client
from azure.common.credentials import ServicePrincipalCredentials

credentials = ServicePrincipalCrendentials(client_id, secret, tenant)
client = compute_management_client(credentials, subscription_id)

client.virtual_machines.run_command(resource_group_name,
     vm_name, parameters, customheaders=None, raw=False,
     **operation_config)

But how do I pass my command here? I couldn't find any sample for parameters and operation_config. Please Help


回答1:


Basic example:

  run_command_parameters = {
      'command_id': 'RunShellScript', # For linux, don't change it
      'script': [
          'ls /tmp'
      ]
  }
  poller = client.virtual_machines.run_command(
        resource_group_name,
        vm_name,
        run_command_parameters
  )
  result = poller.result()  # Blocking till executed
  print(result.value[0].message)  # stdout/stderr

If you wish to inject parameters, you can do this:

    run_command_parameters = {
        'command_id': 'RunShellScript',
        'script': [
            'echo $arg1'
        ],
        'parameters':[
            {'name':"arg1", 'value':"hello world"}
        ]
    }

If using Windows, you can use RunPowerShellScript command id

You might want to test your command using the CLI: az vm run-command invoke --help

Since the CLI is using this SDK, you'll get the same behavior.



来源:https://stackoverflow.com/questions/51478227/run-command-in-linux-vm-in-azure-using-python-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!