@ -38,6 +38,12 @@ options:
description :
- Name of the virtual machine .
required : true
custom_data :
description :
- Data which is made available to the virtual machine and used by e . g . , cloud - init .
default : null
required : false
version_added : " 2.5 "
state :
description :
- Assert the state of the virtual machine .
@ -591,6 +597,7 @@ azure_vm:
}
''' # NOQA
import base64
import random
import re
@ -601,6 +608,7 @@ except ImportError:
# This is handled in azure_rm_common
pass
from ansible . module_utils . basic import to_native , to_bytes
from ansible . module_utils . azure_rm_common import AzureRMModuleBase , azure_id_to_dict
@ -626,6 +634,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
self . module_arg_spec = dict (
resource_group = dict ( type = ' str ' , required = True ) ,
name = dict ( type = ' str ' , required = True ) ,
custom_data = dict ( type = ' str ' ) ,
state = dict ( choices = [ ' present ' , ' absent ' ] , default = ' present ' , type = ' str ' ) ,
location = dict ( type = ' str ' ) ,
short_hostname = dict ( type = ' str ' ) ,
@ -660,6 +669,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
self . resource_group = None
self . name = None
self . custom_data = None
self . state = None
self . location = None
self . short_hostname = None
@ -970,6 +980,10 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
if self . admin_password :
vm_resource . os_profile . admin_password = self . admin_password
if self . custom_data :
# Azure SDK (erroneously?) wants native string type for this
vm_resource . os_profile . custom_data = to_native ( base64 . b64encode ( to_bytes ( self . custom_data ) ) )
if self . os_type == ' Linux ' :
vm_resource . os_profile . linux_configuration = self . compute_models . LinuxConfiguration (
disable_password_authentication = disable_ssh_password
@ -1101,6 +1115,12 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
if vm_dict . get ( ' tags ' ) :
vm_resource . tags = vm_dict [ ' tags ' ]
# Add custom_data, if provided
if vm_dict [ ' properties ' ] [ ' osProfile ' ] . get ( ' customData ' ) :
custom_data = vm_dict [ ' properties ' ] [ ' osProfile ' ] [ ' customData ' ]
# Azure SDK (erroneously?) wants native string type for this
vm_resource . os_profile . custom_data = to_native ( base64 . b64encode ( to_bytes ( custom_data ) ) )
# Add admin password, if one provided
if vm_dict [ ' properties ' ] [ ' osProfile ' ] . get ( ' adminPassword ' ) :
vm_resource . os_profile . admin_password = vm_dict [ ' properties ' ] [ ' osProfile ' ] [ ' adminPassword ' ]
@ -1667,5 +1687,6 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
def main ( ) :
AzureRMVirtualMachine ( )
if __name__ == ' __main__ ' :
main ( )