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?

4
  • you are probably better off just using ARM Template Commented Jan 22, 2018 at 8:39
  • Please provide more details about what you have done and check this topic before asking a question in SO : stackoverflow.com/help/how-to-ask Commented Jan 22, 2018 at 8: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? Commented Jan 22, 2018 at 8:48
  • @VarunVembar I find this example could create VM custom script extension, I modify it. You could check it, it works for me. Commented Jan 22, 2018 at 9:08

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

8 Comments

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.
Hi, I use your file test in my lab, it work for me. This is the code. You could see the test result
Hi, if you get same error log, you could post your code here, I will test in my lab and give you the result.
This is the code 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
You lose some parameter in params_create, the parameters are all required.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.