创建Azure VM时cloud-init未运行的问题排查求助
Let’s walk through the most common reasons your cloud-init-web-server.txt isn’t executing, and how to diagnose and fix them— I’ve debugged this exact scenario multiple times for Azure VMs.
1. First, Validate Your cloud-init File Format & Content
Cloud-init is picky about syntax and structure— small mistakes here are the #1 culprit:
- Must start with the right header: If you’re using cloud-config YAML, the file must begin with
#cloud-config(no extra spaces before it). For example, a valid web server config looks like this:#cloud-config packages: - apache2 runcmd: - systemctl start apache2 - systemctl enable apache2 - Check for YAML errors: YAML relies on consistent spacing (use 2 spaces per indent, never tabs). A misplaced tab or missing indent will break the entire config.
- Avoid Windows-style encoding: If you created the file on Windows, make sure it’s saved as UTF-8 without BOM (Notepad defaults to adding a BOM, which cloud-init can’t parse).
2. Confirm the Custom Data Is Being Passed Correctly
When you run az vm create --custom-data cloud-init-web-server.txt, Azure needs to access that file from your local working directory:
- Verify the file exists in the directory where you ran the
azcommand (runls cloud-init-web-server.txtto check). If it’s in another folder, use the full absolute path (e.g.,--custom-data /home/you/scripts/cloud-init-web-server.txt). - Ensure there are no permission issues preventing the
azCLI from reading the file (runcat cloud-init-web-server.txtto confirm you can access it).
3. Check Cloud-Init Logs (The Most Critical Step)
SSH into your VM and inspect the cloud-init logs— they’ll tell you exactly what went wrong:
# Detailed cloud-init startup/execution logs cat /var/log/cloud-init.log # Output from your config's commands (e.g., package installs) cat /var/log/cloud-init-output.log
Look for errors like:
- "Invalid YAML syntax" (points to a formatting issue in your file)
- "Failed to fetch user data" (means Azure didn’t pass the custom data to the VM)
- "Package install failed" (indicates a problem with the package repository or command in your config)
4. Verify the Custom Data Reached the VM
Check if your cloud-init file was correctly delivered to the VM:
cat /var/lib/cloud/instance/user-data.txt
If this file is empty or doesn’t match your cloud-init-web-server.txt content, the issue is with how you’re passing the --custom-data parameter. Double-check the file path, and ensure the --secrets "$vm_secret" part of your command isn’t causing syntax conflicts (e.g., unclosed quotes).
5. Rule Out Compatibility or Environment Issues
- Check cloud-init version: Ubuntu LTS images come with a compatible cloud-init version, but you can confirm with:
If you’re on an older version (pre-21.0), updating it might resolve bugs, but this is rarely necessary for recent Ubuntu LTS releases.cloud-init --version - Password auth vs. SSH keys: While using
--admin-passwordshouldn’t break cloud-init, if all other checks pass, try creating the VM with an SSH key instead (using--ssh-key-value), just to rule out any edge cases with password-based authentication.
Start with the logs— they’ll give you the clearest insight into why your config isn’t running. Once you spot the error, fixing it is usually straightforward!
内容的提问来源于stack exchange,提问作者kumar

