2

How can I use the Python SDK and execute a script stored in Blob inside a VM?

Is it possible to do it at the time of creation?

I've looked at this, and I'm not able to make it work. This is my existing template. Where am I going wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Varun Vembar
  • 318
  • 5
  • 18
  • you are probably better off just using ARM Template – 4c74356b41 Jan 22 '18 at 08:39
  • Please provide more details about what you have done and check this topic before asking a question in SO : https://stackoverflow.com/help/how-to-ask – Fourat Jan 22 '18 at 08:40
  • @fourat. I've provided the exact details. What I saw, and what I did, as links. Could you please tell me how I could use the improve the question? – Varun Vembar Jan 22 '18 at 08:48
  • @VarunVembar I find this [example](https://github.com/Azure-Samples/compute-python-msi-vm) could create VM custom script extension, I modify it. You could check it, it works for me. – Shui shengbao Jan 22 '18 at 09:08

1 Answers1

4

You should create VM firstly, then using Azure Custom Script Extension to execute scripts inside VM.

compute_client = ComputeManagementClient(credentials, subscription_id)
###Your python code to create VM
.......
compute_client.virtual_machines.create_or_update( 'DEV-Central', computer_name, param_dict )

##Using Azure Custom Script to execute script inside VM
GROUP_NAME = 'shuicli'
vmname = 'shui'
ext_type_name = 'CustomScriptForLinux'
ext_name = 'shuitest'
params_create = {
    'location': 'eastus',
    'publisher': 'Microsoft.OSTCExtensions',
    'virtual_machine_extension_type': ext_type_name,
    'type_handler_version': '1.5',
    'auto_upgrade_minor_version': True,
    'settings': {
        'fileUris': ["https://shuilinuxdiag336.blob.core.windows.net/customscriptfiles/test.sh"],
        'commandToExecute': "sh test.sh"
    }, 
    'protected_settings' : {
        'storageAccountName': 'shuilinuxdiag336',
        'storageAccountKey': '<your storage account key>'
    },
}
ext_poller = compute_client.virtual_machine_extensions.create_or_update(
    GROUP_NAME,
    vmname,
    ext_name,
    params_create,
)
ext = ext_poller.result()
Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
  • Thank you for all your time, but I'm still not able to make it work! I'm getting an error with the 'ext_type_name' parameter. I'm trying to do all this inside a Python runbook. – Varun Vembar Jan 22 '18 at 11:44
  • Hi, I use your file test in my lab, it work for me. This is the [code](https://gist.github.com/Walter-Shui/ab5f72e33aac22f2dae93fbafcd67c6c). You could see the test [result](https://imgur.com/a/Il4jV) – Shui shengbao Jan 23 '18 at 02:21
  • Hi, if you get same error log, you could post your code here, I will test in my lab and give you the result. – Shui shengbao Jan 23 '18 at 07:39
  • This is the code https://pastebin.com/AWpyYmYN. It's pretty much a copy paste of what you've shared. It's erroring out at the ext = ext_poller.result()' line – Varun Vembar Jan 23 '18 at 07:44
  • You lose some parameter in `params_create`, the parameters are all required. – Shui shengbao Jan 23 '18 at 07:46
  • Hi, check this example https://gist.github.com/Walter-Shui/ab5f72e33aac22f2dae93fbafcd67c6c. Only replace `location`, `vm name`, `rg name`, don't change other parameters. – Shui shengbao Jan 23 '18 at 07:47
  • Hi, this question https://stackoverflow.com/questions/48376092/add-vm-to-azure-app-gateway-python/48378190#48378190 Do you solve it? If it helps, you also could accept it. Thanks. – Shui shengbao Jan 23 '18 at 09:40
  • How to get stdout?! – Jack Jul 02 '21 at 22:16