The traditional programmer might need a few online searches to get going with Terraform’s syntax. Because a well-written script is key to managing a cloud environment, this blog attempts to break the inertia by providing two short comprehensive scripts to jump-start a project in Oracle Cloud Infrastructure (OCI). Both scripts represent standard virtual machine (VM) images in OCI and run a set of commands using remote-exec.

The first script enables you to scale VM instances up and down by numeric value, and all VMs receive the same commands through remote-exec. The second script scales unique VMs with the ability to run different sets of commands based on VM’s display name.

The two scripts correspond with the two iteration mechanisms available in Terraform: count and for-each (Terraform 0.12). The second script implements if-then-else to filter the selection of different command sets, while both scripts use remote-exec for command execution. The scripts are divided into two sections: Administration and execution. This divide separates the roles and facilitates maintenance and operations. All remote-exec commands direct the terminal output to a log file. Modularity and extensibility are also key considerations of the overall script’s coding structure.

The following short descriptions show the tricks and the source-codes of the two scripts. This blog showcases the two scripts that you can use for scaled deployments on OCI. For more technical details, see this blog.

Script 1: Same VM, scaled with numeric index

Iterations with count

Using count, this script creates, scales up and down, and runs the same set of commands on each VM. The administrator only needs to change the numeric value of the count.

A screenshot of the script with a blue arrow pointing to "Set the number of VMs here."

A screenshot comparing the original VMs to the VMs after scaling.

remote-exec

With touch and echo, the utility tee enables the remote-exec to run sudo commands while maintaining logs. The redirect fails for commands requiring sudo and using 'sudo tee' with pipe is a nice trick to resolve this permissions issue.

A screenshot of the command to enable running sudo commands.

Script 2: Different VMs, scaled with display-name

Iteration with for-each

With for-each looping, this script matches the display-name with a set of commands. Only the commands in the specified set are run on the VM. Adding and removing VM names from the list scales up and down.

A screenshot of the script with a blue arrow pointing to "Set the VMs here."

A screenshot comparing the original VMs and the VMs after scaling.

if-then-else

This script uses regex to match the display-name to filter for the command set to run in the VM. Since the regex returns an error instead of a false for a non-match, a try block helps with the else condition. This script shows one way that if-then-else can be implemented with RegEx.

A screenshot of the code showing the if, then, and else parameters.

Conclusion

You have many choices of utilities and various ways to write cloud automation code. This blog provides a Terraform way to achieve a basic implementation in two scripts. The scripts are written with modularity in mind with the intent of providing a good set of tricks for anyone who wants to start experimenting. The source code for both scripts is attached.

Script 1: Same VM, scaled with numeric index

#----------- Administration Start -----------

# VM Instances

locals {

  num_of_VMs = 5  #SET THE NUMBER OF VMs HERE

  tenancy_ocid             = "ocid1.tenancy.oc1..aaaaaaaaubrkzed3mzqxtsxx4qnfgmcmoh5mm7r6r33e3joefrayjnrmf7oa"

  user_ocid                = "ocid1.user.oc1..aaaaaaaalv4mjzp3her5qpjmwy4l3d2ddppqcnuzbi4seiilzzt4xvimscva"

  private_key_path         = "C:\\Users\\adnan\\.oci\\oracleidentitycloudservice_adnan.umar-11-15-18-50.pem"

  private_key_password     = ""

  fingerprint              = "7e:f4:6b:83:64:cd:5f:9b:73:d7:fa:e2:42:d9:e5:ab"

  region                   = "us-ashburn-1"

  compartment_id           = "ocid1.compartment.oc1..aaaaaaaacmao4vvaukxffbdwv4mj57s5mocfmpxrggsrgaronncqf2a77c4a"

  availability_domain      = "kZAQ:US-ASHBURN-AD-1"

  ssh_authorized_keys_file = "C:\\Users\\adnan\\pubkey.txt"

  private_key_file         = "C:\\Users\\adnan\\pvtkey"

}

#------------ Administration End ------------

#---- Do not make changes below this line----

provider "oci" {

  tenancy_ocid         = local.tenancy_ocid

  user_ocid            = local.user_ocid

  private_key_path     = local.private_key_path

  private_key_password = local.private_key_password

  fingerprint          = local.fingerprint

  region               = local.region

}

# Create IGW

resource "oci_core_internet_gateway" "My_count_igw" {

  #Required

  compartment_id = local.compartment_id

  vcn_id         = oci_core_vcn.My_count_vcn.id

}

# Add route to IGW

resource "oci_core_default_route_table" "default_route_table_id" {

  #Required

  manage_default_resource_id = oci_core_vcn.My_count_vcn.default_route_table_id

  route_rules {

    network_entity_id = oci_core_internet_gateway.My_count_igw.id

    #cidr_block = "0.0.0.0/0"

    destination = "0.0.0.0/0"

  }

}

#

resource "oci_core_default_security_list" "default_security_list_id" {

  #Required

  manage_default_resource_id = oci_core_vcn.My_count_vcn.default_security_list_id

  # allow SSH

  ingress_security_rules {

    protocol  = 6 # tcp

    source    = "0.0.0.0/0"

    stateless = false

    tcp_options {

      min = 22

      max = 22

    }

  }

  # allow ICMP

  ingress_security_rules {

    protocol  = 1 #icmp

    source    = "0.0.0.0/0"

    stateless = false

  }

  # allow HTTP

  ingress_security_rules {

    protocol  = 6 # tcp

    source    = "0.0.0.0/0"

    stateless = false

    tcp_options {

      min = 80

      max = 80

    }

  }

  # allow egress traffic

  egress_security_rules {

    destination = "0.0.0.0/0"

    protocol    = "all"

  }

}

# Create one VCN

resource "oci_core_vcn" "My_count_vcn" {

  compartment_id = local.compartment_id

  cidr_blocks    = ["10.0.0.0/16"]

  display_name   = "My_VCN"

}

# Create one subnet inside the VCN

resource "oci_core_subnet" "My_count_subnet" {

  cidr_block     = "10.0.1.0/24"

  compartment_id = local.compartment_id

  vcn_id         = oci_core_vcn.My_count_vcn.id

  display_name   = "My_subnet"

}

# Create one Linux Instance

resource "oci_core_instance" "My_count_VMs" {

  availability_domain = local.availability_domain

  compartment_id      = local.compartment_id

  shape               = "VM.Standard2.1"

  display_name        = "My_VM_${count.index}"

  count               = local.num_of_VMs

  create_vnic_details {

    subnet_id = oci_core_subnet.My_count_subnet.id

  }

  source_details {

    source_id   = "ocid1.image.oc1.iad.aaaaaaaakjvsts7rf7umrlqtw5hbhc3gjotadu7thfn5cfathwdn3awht7ca"

    source_type = "image"

  }

  metadata = {

    ssh_authorized_keys = file(local.ssh_authorized_keys_file)

  }

  preserve_boot_volume = false

}

# Execute commands in Linux Instance

resource "null_resource" "remote-exec" {

  depends_on = [oci_core_instance.My_count_VMs]

  count = local.num_of_VMs

  provisioner "remote-exec" {

    connection {

      agent       = false

      timeout     = "30m"

      host        = oci_core_instance.My_count_VMs[count.index].public_ip

      user        = "opc"

      private_key = file(local.private_key_file)

    }

    inline = [

      "touch ~/logs",

      join(" ", ["echo $(echo | date)\"",oci_core_instance.My_count_VMs[count.index].display_name,oci_core_instance.My_count_VMs[count.index].public_ip,"\">> logs"]),

      "sudo firewall-cmd --zone=public --add-service=http --permanent",

      "sudo firewall-cmd --zone=public --add-service=https --permanent",

      "sudo firewall-cmd --reload >> /home/opc/logs",

      "sudo touch /etc/yum.repos.d/nginx.repo",

      "echo '[nginx]' | sudo tee -a /etc/yum.repos.d/nginx.repo",

      "echo 'name=nginx repo' | sudo tee -a /etc/yum.repos.d/nginx.repo",

      "echo 'baseurl=https://nginx.org/packages/centos/$releasever/$basearch/' | sudo tee -a /etc/yum.repos.d/nginx.repo",

      "echo 'gpgcheck=0' | sudo tee -a /etc/yum.repos.d/nginx.repo",

      "echo 'enabled=1' | sudo tee -a /etc/yum.repos.d/nginx.repo",

      "sudo yum -y install nginx >> /home/opc/logs; sudo systemctl start nginx"

    ]

  }

}

# Print Public IP of Linux Instances

output "my_output" {

  value = concat([oci_core_instance.My_count_VMs[*].display_name,oci_core_instance.My_count_VMs[*].public_ip])

  description = "Info about my instances"

}

Script 2: Different VMs, scaled with display-name

#----------- Administration Start -----------

# VM Instances

variable "my_vm" {

  description = "My VMs"

  type        = set(string)

  default     = ["My_VM_nginx_Alpha","My_VM_httpd_Beta","My_VM_grafana_Charlie","My_VM_httpd_webpage_Delta"] #SET THE VMs HERE

}

locals {

  tenancy_ocid             = "ocid1.tenancy.oc1..aaaaaaaaubrkzed3mzqxtsxx4qnfgmcmoh5mm7r6r33e3joefrayjnrmf7oa"

  user_ocid                = "ocid1.user.oc1..aaaaaaaalv4mjzp3her5qpjmwy4l3d2ddppqcnuzbi4seiilzzt4xvimscva"

  private_key_path         = "C:\\Users\\adnan\\.oci\\oracleidentitycloudservice_adnan.umar-11-15-18-50.pem"

  private_key_password     = ""

  fingerprint              = "7e:f4:6b:83:64:cd:5f:9b:73:d7:fa:e2:42:d9:e5:ab"

  region                   = "us-ashburn-1"

  compartment_id           = "ocid1.compartment.oc1..aaaaaaaacmao4vvaukxffbdwv4mj57s5mocfmpxrggsrgaronncqf2a77c4a"

  availability_domain      = "kZAQ:US-ASHBURN-AD-1"

  ssh_authorized_keys_file = "C:\\Users\\adnan\\pubkey.txt"

  private_key_file         = "C:\\Users\\adnan\\pvtkey"

}

#------------ Administration End ------------

#---- Do not make changes below this line----

provider "oci" {

  tenancy_ocid         = local.tenancy_ocid

  user_ocid            = local.user_ocid

  private_key_path     = local.private_key_path

  private_key_password = local.private_key_password

  fingerprint          = local.fingerprint

  region               = local.region

}

# Create IGW

resource "oci_core_internet_gateway" "My_foreach_igw" {

  #Required

  compartment_id = local.compartment_id

  vcn_id         = oci_core_vcn.My_foreach_vcn.id

}

# Add route to IGW

resource "oci_core_default_route_table" "default_route_table_id" {

  #Required

  manage_default_resource_id = oci_core_vcn.My_foreach_vcn.default_route_table_id

  route_rules {

    network_entity_id = oci_core_internet_gateway.My_foreach_igw.id

    #cidr_block = "0.0.0.0/0"

    destination = "0.0.0.0/0"

  }

}

#

resource "oci_core_default_security_list" "default_security_list_id" {

  #Required

  manage_default_resource_id = oci_core_vcn.My_foreach_vcn.default_security_list_id

  # allow SSH

  ingress_security_rules {

    protocol  = 6 # tcp

    source    = "0.0.0.0/0"

    stateless = false

    tcp_options {

      min = 22

      max = 22

    }

  }

  # allow ICMP

  ingress_security_rules {

    protocol  = 1 #icmp

    source    = "0.0.0.0/0"

    stateless = false

  }

  # allow HTTP

  ingress_security_rules {

    protocol  = 6 # tcp

    source    = "0.0.0.0/0"

    stateless = false

    tcp_options {

      min = 80

      max = 80

    }

  }

  # allow HTTP for Graphana

  ingress_security_rules {

    protocol  = 6 # tcp

    source    = "0.0.0.0/0"

    stateless = false

    tcp_options {

      min = 3000

      max = 3000

    }

  }

  # allow egress traffic

  egress_security_rules {

    destination = "0.0.0.0/0"

    protocol    = "all"

  }

}

# Create one VCN

resource "oci_core_vcn" "My_foreach_vcn" {

  compartment_id = local.compartment_id

  cidr_blocks    = ["10.0.0.0/16"]

  display_name   = "My_VCN"

}

# Create one subnet inside the VCN

resource "oci_core_subnet" "My_foreach_subnet" {

  cidr_block     = "10.0.1.0/24"

  compartment_id = local.compartment_id

  vcn_id         = oci_core_vcn.My_foreach_vcn.id

  display_name   = "My_subnet"

}

# Create one Linux Instance

resource "oci_core_instance" "My_foreach_VMs" {

  availability_domain = local.availability_domain

  compartment_id      = local.compartment_id

  shape               = "VM.Standard2.1"

  display_name        = each.value

  for_each             = var.my_vm

  create_vnic_details {

    subnet_id = oci_core_subnet.My_foreach_subnet.id

  }

  source_details {

    source_id   = "ocid1.image.oc1.iad.aaaaaaaakjvsts7rf7umrlqtw5hbhc3gjotadu7thfn5cfathwdn3awht7ca"

    source_type = "image"

  }

  metadata = {

    ssh_authorized_keys = file(local.ssh_authorized_keys_file)

  }

  preserve_boot_volume = false

}

# Execute commands in Linux Instance

resource "null_resource" "remote-exec" {

  depends_on = [oci_core_instance.My_foreach_VMs]

  for_each = oci_core_instance.My_foreach_VMs

  provisioner "remote-exec" {

    connection {

      agent       = false

      timeout     = "30m"

      host        = oci_core_instance.My_foreach_VMs[each.key].public_ip

      user        = "opc"

      private_key = file(local.private_key_file)

    }

    inline = [

      try(regex("My_VM", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM" ? "touch ~/logs" : "", "echo \"Error 1\" >> /home/opc/logs"),

      try(regex("My_VM", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM" ? join(" ", ["echo $(echo | date)\"", oci_core_instance.My_foreach_VMs[each.key].display_name, oci_core_instance.My_foreach_VMs[each.key].public_ip, "\">> logs"]) : "", "echo \"Error 2\" >> /home/opc/logs"),

      try(regex("My_VM", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM" ? "sudo firewall-cmd --zone=public --add-service=http --permanent" : "", "echo \"Error 3\" >> /home/opc/logs"),

      try(regex("My_VM", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM" ? "sudo firewall-cmd --zone=public --add-service=https --permanent" : "", "echo \"Error 4\" >> /home/opc/logs"),

      try(regex("My_VM", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM" ? "sudo firewall-cmd --reload >> /home/opc/logs" : "", "echo \"Error 5\" >> /home/opc/logs"),

      try(regex("My_VM_httpd", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd" ? "sudo yum -y install httpd >> /home/opc/logs; sudo systemctl start httpd >> /home/opc/logs" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo touch /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<!DOCTYPE html>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<html>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<head>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<title>My customer Apache webpage</title>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<style>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '    body {' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '        width: 35em;' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '        margin: 0 auto;' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '        font-family: Tahoma, Verdana, Arial, sans-serif;' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '    }' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '</style>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '</head>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<body>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<h1>My customer Apache webpage</h1>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<p>Line one ....</p>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo ''| sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '<p>Line two ....</p>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '</body>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_httpd_webpage", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_httpd_webpage" ? "sudo echo '</html>' | sudo tee -a /var/www/html/index.html" : "", "echo \"httpd not selected\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "sudo touch /etc/yum.repos.d/nginx.repo" : "", "echo \"nginx not selected ... 1\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "echo '[nginx]' | sudo tee -a /etc/yum.repos.d/nginx.repo" : "", "echo \"nginx not selected ... 2\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "echo 'name=nginx repo' | sudo tee -a /etc/yum.repos.d/nginx.repo" : "", "echo \"nginx not selected ... 3\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "echo 'baseurl=https://nginx.org/packages/centos/$releasever/$basearch/' | sudo tee -a /etc/yum.repos.d/nginx.repo" : "", "echo \"nginx not selected ... 4\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "echo 'gpgcheck=0' | sudo tee -a /etc/yum.repos.d/nginx.repo" : "", "echo \"nginx not selected ... 5\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "echo 'enabled=1' | sudo tee -a /etc/yum.repos.d/nginx.repo" : "", "echo \"nginx not selected ... 6\" >> /home/opc/logs"),

      try(regex("My_VM_nginx", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_nginx" ? "sudo yum -y install nginx >> /home/opc/logs; sudo systemctl start nginx" : "", "echo \"nginx not selected ... 7\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent" : "", "echo \"Error G1\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "sudo firewall-cmd --reload >> /home/opc/logs" : "", "echo \"Error G2\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "sudo touch /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 1\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo '[grafana]' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 2\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'name=grafana' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 3\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'baseurl=https://packages.grafana.com/oss/rpm' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 4\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'repo_gpgcheck=1' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 5\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'enabled=1' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 6\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'gpgcheck=1' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 7\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'gpgkey=https://packages.grafana.com/gpg.key' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 8\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'sslverify=1' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 9\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "echo 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt' | sudo tee -a /etc/yum.repos.d/grafana.repo" : "", "echo \"grafana not selected ... 10\" >> /home/opc/logs"),

      try(regex("My_VM_grafana", oci_core_instance.My_foreach_VMs[each.key].display_name) == "My_VM_grafana" ? "sudo yum -y install grafana >> /home/opc/logs; sudo systemctl start grafana-server.service" : "", "echo \"grafana not selected ... 11\" >> /home/opc/logs")      

    ]

  }

}

# Print Public IP of Linux Instances

output "my_output" {

  value = concat([values(oci_core_instance.My_foreach_VMs)[*].display_name,values(oci_core_instance.My_foreach_VMs)[*].public_ip])

  description = "Info about my instances"

}