`az vm create`: when `--patch-mode` is set but not `--enable-agent`, vm creation fails with `KeyError: 'linuxConfiguration'`.
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 3.5k
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 60
Description
### Describe the bug
When creating a VM with `az vm create`, if `--patch-mode` is set but not `--enable-agent`, vm creation fails with message `'linuxConfiguration'`.
`--debug` option shows it fails with `KeyError` at the following line:
https://github.com/Azure/azure-cli/blob/aea0ce91218f13960232f76f458796555f68eb1b/src/azure-cli/azure/cli/command_modules/vm/_template_builder.py#L381
`os_profile['linuxConfiguration']` is set only if `enable_agent` is not `None` or `ssh_key_values` and `ssh_key_path` are given. We must initialize `os_profile['linuxConfiguration']` (and `os_profile['windowsConfiguration']` for Windows VM) before setting `patchSettings`.
We must fix `build_vm_resource` and `build_vmss_resource`.
### Related command
az vm create
### Errors
The command fails with just `'linuxConfiguration'`. See "Issue script & Debug output" for details.
### Issue script & Debug output
Command:
```
RESOURCE_GROUP=example-resource-group
az vm create \
--validate \
--name "test" \
--resource-group "$RESOURCE_GROUP" \
--image Ubuntu2204 \
--admin-password 123456789aA_ \
--patch-mode ImageDefault \
--debug
```
Debug output (only errors):
```
cli.azure.cli.core.azclierror: Traceback (most recent call last):
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 701, in _run_job
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 334, in __call__
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py", line 121, in handler
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/vm/custom.py", line 1097, in create_vm
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/vm/_template_builder.py", line 589, in build_vm_resource
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/vm/_template_builder.py", line 381, in _build_os_profile
KeyError: 'linuxConfiguration'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/arm.py", line 109, in handle_template_based_exception
AttributeError: 'KeyError' object has no attribute 'inner_exception'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 664, in execute
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 731, in _run_jobs_serially
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 723, in _run_job
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/arm.py", line 114, in handle_template_based_exception
knack.util.CLIError: 'linuxConfiguration'
cli.azure.cli.core.azclierror: 'linuxConfiguration'
az_command_data_logger: 'linuxConfiguration'
```
### Expected behavior
The command finishes without errors.
### Environment Summary
```
azure-cli 2.58.0 *
core 2.58.0 *
telemetry 1.1.0
Extensions:
aks-preview 1.0.0b3
azure-devops 0.26.0
azure-firewall 1.0.1
bastion 0.3.0
log-analytics 0.2.2
logic 1.0.1
ssh 2.0.2
Dependencies:
msal 1.26.0
azure-mgmt-resource 23.1.0b2
Python location 'C:\Users\yonezawa\scoop\apps\azure-cli\current\python.exe'
Extensions directory 'C:\Users\yonezawa\.azure\cliextensions'
Python (Windows) 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
Legal docs and information: aka.ms/AzureCliLegal
You have 2 update(s) available. Consider updating your CLI installation with 'az upgrade'
```
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.