Create an azure VM via python with init script

Viewed 26

I am trying to create a virtual machine on azure via python and having it execute a script on launching time (init script). For this purpose, I am using the custom_data as follows:

entercompute_client = ComputeManagementClient(managed_identity, subscription_id)
poller = compute_client.virtual_machines.begin_create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
    {
        "location": LOCATION,
        "storage_profile": {
            "image_reference": {
                "publisher": 'Canonical',
                "offer": "0001-com-ubuntu-server-focal",
                "sku": "20_04-lts-gen2",
                "version": "latest"
            }
        },
        "hardware_profile": {
            "vm_size": "Standard_DS2_v2"
        },
        "os_profile": {
            "computer_name": VM_NAME,
            "admin_username": USERNAME,
            "admin_password": PASSWORD,
            "custom_data": custom_data,
        },
        "network_profile": {
            "network_interfaces": [{
                "id": nic_result.id,
            }]
        }
    }
)

vm_result = poller.result() code here

the custom_data is created this way:

init_script = f'''#!/bin/bash
    nohup {WORKER_SCRIPT_PATH} -c "{config}" -j "{job}" -m "{message_base64}" -h "{handle}" > /root/log 2> /root/error &
'''

custom_data = base64.b64encode(init_script.encode('utf-8')).decode('latin-1')
create_vm(custom_data)

For the variables inside the init_script: WORKER_SCRIPT_PATH : is the string path to my shell script (an sh file) and the other variables are simple strings.

Now after I execute the script, everything seems to work fine and the VM is created but when I get inside the machine, in the /root/error I find this:

nohup: failed to run command './simple_bash.sh': No such file or directory

As if the sh file was not read by the VM.

0 Answers
Related