Last active
September 28, 2020 11:46
-
-
Save scross01/6a8914365e4bd1e8ab82cab372e8bbf5 to your computer and use it in GitHub Desktop.
Oracle Cloud Infrastructure ISCSI Block Volume Attachment Example for Windows Server 2016
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "tenancy_ocid" {} | |
variable "user_ocid" {} | |
variable "fingerprint" {} | |
variable "private_key_path" {} | |
variable "compartment_ocid" {} | |
variable "region" {} | |
variable "subnet" {} | |
variable "ad" { | |
default = 0 | |
description = "availability domain index [0..2]" | |
} | |
locals { | |
AD = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.ad],"name")}" | |
} | |
provider "oci" { | |
tenancy_ocid = "${var.tenancy_ocid}" | |
user_ocid = "${var.user_ocid}" | |
fingerprint = "${var.fingerprint}" | |
private_key_path = "${var.private_key_path}" | |
region = "${var.region}" | |
} | |
data "oci_identity_availability_domains" "ADs" { | |
compartment_id = "${var.tenancy_ocid}" | |
} | |
data "oci_core_images" "windows-server-2016-standard-VM" { | |
compartment_id = "${var.tenancy_ocid}" | |
operating_system = "Windows" | |
operating_system_version = "Server 2016 Standard" | |
filter { | |
name = "display_name" | |
values = ["^Windows-Server-2016-Standard-Edition-VM-Gen2-([\\.0-9-]+)$"] | |
regex = true | |
} | |
} | |
data "template_file" "cloud-config" { | |
template = <<POWERSHELL | |
#ps1_sysnative | |
# disable password expiry to allow winrm remote-exec using opc account | |
cmd /C 'wmic UserAccount where Name="opc" set PasswordExpires=False' | |
# enable the iSCSI service | |
Set-Service -Name msiscsi -StartupType Automatic | |
Start-Service msiscsi | |
POWERSHELL | |
} | |
resource "oci_core_instance" "instance1" { | |
count = 1 | |
availability_domain = "${local.AD}" | |
compartment_id = "${var.compartment_ocid}" | |
subnet_id = "${var.subnet}" | |
display_name = "instance1" | |
hostname_label = "instance1" | |
source_details { | |
source_type = "image" | |
source_id = "${data.oci_core_images.windows-server-2016-standard-VM.images.0.id}" | |
} | |
shape = "VM.Standard2.1" | |
metadata = { | |
user_data = "${base64encode(data.template_file.cloud-config.rendered)}" | |
} | |
} | |
data "oci_core_instance_credentials" "instance1" { | |
instance_id = "${oci_core_instance.instance1.id}" | |
} | |
output "username" { | |
value = "${data.oci_core_instance_credentials.instance1.username}" | |
} | |
output "password" { | |
value = "${data.oci_core_instance_credentials.instance1.password}" | |
} | |
resource "oci_core_volume" "volume1" { | |
availability_domain = "${local.AD}" | |
compartment_id = "${var.compartment_ocid}" | |
display_name = "volume1" | |
size_in_gbs = 50 | |
} | |
resource "oci_core_volume_attachment" "attachment1" { | |
attachment_type = "iscsi" | |
instance_id = "${oci_core_instance.instance1.id}" | |
volume_id = "${oci_core_volume.volume1.id}" | |
connection { | |
type = "winrm" | |
insecure = true | |
https = true | |
port = 5986 | |
host = "${oci_core_instance.instance1.public_ip}" | |
user = "${data.oci_core_instance_credentials.instance1.username}" | |
password = "${data.oci_core_instance_credentials.instance1.password}" | |
} | |
# register and connect the iSCSI block volume | |
provisioner "remote-exec" { | |
inline = [ | |
" Powershell New-IscsiTargetPortal –TargetPortalAddress ${self.ipv4}", | |
" Powershell Connect-IscsiTarget -NodeAddress ${self.iqn} -TargetPortalAddress ${self.ipv4} -IsPersistent $True", | |
] | |
} | |
# unmount and disconnect on destroy | |
provisioner "remote-exec" { | |
when = "destroy" | |
on_failure = "continue" | |
inline = [ | |
" Powershell \"Get-IscsiTargetPortal -TargetPortalAddress ${self.ipv4} | ForEach-Object {Get-IscsiSession -IscsiTargetPortal $_} | ForEach-Object {Unregister-IscsiSession -SessionIdentifier $_.SessionIdentifier}\"", | |
" Powershell Disconnect-IscsiTarget -NodeAddress ${self.iqn} -Confirm:$false", | |
" Powershell Remove-IscsiTargetPortal -TargetPortalAddress ${self.ipv4} -Confirm:$false", | |
] | |
} | |
} | |
output "public_ip" { | |
value = "${oci_core_instance.instance1.public_ip}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the post. it helped me :)