Adding userdata on create VM operation with Python SDK for Azure

2024/10/10 0:30:06

I am using Python sdk for azure creation virtual machine operation. I want some script to be executed whenever the VM starts. So, I have tried adding the custom-data while creating VM with Python.

My directory:

  • user-data.sh
  • create_VM.py

user-data.sh file will be similar to this:

#!/bin/bash
sudo apt install apache2 -y
sudo apt install git -y

I have used the base64 package on create_VM.py file, which looks like:

import base64...file = open("user-data.sh", "r")
a = file.read().encode()
encoded_string = base64.b64encode(a)...poller = compute_client.virtual_machines.create_or_update(RESOURCE_GROUP_NAME, VM_NAME,{"location": LOCATION,"storage_profile": {"image_reference": {"publisher": 'Canonical',"offer": "UbuntuServer","sku": "16.04.0-LTS","version": "latest"}},"hardware_profile": {"vm_size": "Standard_DS1_v2"},"os_profile": {"computer_name": VM_NAME,"admin_username": USERNAME,"admin_password": PASSWORD,"custom_data": encoded_string},"network_profile": {"network_interfaces": [{"id": nic_result.id,}]}}
)
...

The error I am getting is:

Azure Error: InvalidParameter\nMessage: Custom data in OSProfile must be in Base64 encoding and with a maximum length of 87380 characters.\nTarget: customData

How can I fix the issue?

Answer

please try this one, it's working on mine.

import base64...file = open("user-data.sh", "r")
a = file.read().encode()...CUSTOM_DATA = base64.b64encode(a.encode('utf-8')).decode('latin-1')
https://en.xdnf.cn/q/118523.html

Related Q&A

python obtain the self variable in another class which already has a self function

I want to use the self variables in one class and use them in another class which already has its own self variables how to do I do this. Some code here to help.class A():self.health = 5 class B(): # T…

Cannot pip install package in virtualenv on EC2

Im seeing this weird issue on ec2. Im trying to install lsm-db package inside my virtualenv, it says its successfully installed but when trying to import the package or do pip list its not there.I crea…

Python: string to integer as a key

Im trying to convert a string column in a dataframe to int. The strings should be replaced with an integer as a key value.Data:user_id site_id 100 url1.com 100 url2.com 100 url1.com 101…

Data manipulation, kind of downsampling

I have a large csv file, example of the data below. I will use an example of eight teams to illustrate.home_team away_team home_score away_score year belgium france 2…

Chrome Native Messaging throwing error when sending a base64 string to client

Using Chrome Native Messaging sample app as a template am able make a system call to bashos.system("<bash command>")The requirement is to return a base64 string from the python scriptos…

Exporting DataFrame to Excel using pandas without subscribe

How can I export DataFrame to excel without subscribe? For exemple: Im doing webscraping and there is a table with pagination, so I take the page 1 save it in DataFrame, export to excel e do it again …

Fraction of a real number in python giving complicated answer

Importing Fraction from fractions to give a fractional representation of a real number, but giving responses quite complicated which seems very simple by the paper-pen method. Fractions(.2) giving answ…

Scrape latitude and longitude (Google Maps) inside Script type=text/javascript

Im beginner in Web Scrapping. Im trying to get latitude and longitude from this web: https://urbania.pe/inmueble/proyecto/ememhvin-proyecto-mariscal-castilla-lima-santiago-de-surco-tale-inmobiliaria-65…

How to delete a button that is made by a loop

from tkinter import *class Main:def __init__(self, root):for i in range(0, 9):for k in range(0, 9):Button(root, text=" ").grid(row=i, column=k)root.mainloop()root = Tk()x = Main(root)How do I…

Invalid array shape with neural network using Keras?

Currently studying the Deep Learning with Python book by Francios Chollet. I am very new to this and I am getting this error code despite following his code verbatim. Can anyone interpret the error mes…