below code creates 3 linux ubuntu azure vm's .At the same time i want to execute shell script in these 3 vm's .For this am using the below code but am getting the following error basically am using filebase64 to execute the code but its not working.Could anyone please check it and let me know the issue
# Resource-1: Azure Resource Group
resource "azurerm_resource_group" "myrg" {
name = "${var.resource_group}"
location = "${var.location}"
}
# Create Virtual Network
resource "azurerm_virtual_network" "myvnet" {
name = "myvnet-1"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
}
# Create Subnet
resource "azurerm_subnet" "mysubnet" {
name = "mysubnet-1"
resource_group_name = azurerm_resource_group.myrg.name
virtual_network_name = azurerm_virtual_network.myvnet.name
address_prefixes = ["10.0.2.0/24"]
}
#Create Bastion host
resource "azurerm_subnet" "Azure_Bastion_Subnet" {
name = "AzureBastionSubnet"
resource_group_name = azurerm_resource_group.myrg.name
virtual_network_name = azurerm_virtual_network.myvnet.name
address_prefixes = ["10.0.3.0/24"]
}
#Create Azure Public IP Address
resource "azurerm_public_ip" "mypublicip" {
count = "${var.instance_count}"
name = "mypublicip-${count.index}"
resource_group_name = azurerm_resource_group.myrg.name
location = azurerm_resource_group.myrg.location
allocation_method = "Static"
sku = "Standard"
domain_name_label = "app1-vm-${count.index}-${random_string.myrandom.id}"
}
resource "azurerm_network_interface" "myvmnic" {
count = "${var.instance_count}"
name = "vmnic-${count.index}"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.mysubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = element(azurerm_public_ip.mypublicip[*].id, count.index)
}
}
resource "azurerm_network_security_group" "linux-nsg"{
name = "acceptanceTestSecurityGroup1"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
security_rule {
name = "Allowssh"
priority = 150
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "Allow"
priority = 100
direction = "Outbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "example" {
subnet_id = azurerm_subnet.mysubnet.id
network_security_group_id = azurerm_network_security_group.linux-nsg.id
}
data "template_file" "config"{
template = file("${path.module}/script.sh")
}
# Resource: Azure Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "mylinuxvm" {
count = "${var.instance_count}"
name = "mylinuxvm-${count.index}"
computer_name = "zookeeper-${count.index}" # Hostname of the VM
resource_group_name = azurerm_resource_group.myrg.name
location = azurerm_resource_group.myrg.location
size = "Standard_DS1_v2"
admin_username = "useradmin"
#admin_password = "Solr@12345"
network_interface_ids = [ element(azurerm_network_interface.myvmnic[*].id, count.index)]
custom_data = filebase64(data.template_file.config.rendered)
disable_password_authentication = true
admin_ssh_key {
username = "useradmin"
public_key = file("${path.module}/ssh-keys/terraform-azure.pub")
}
os_disk {
name = "osdisk${count.index}"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
connection {
type = "ssh"
host = self.public_ip_address
user = self.admin_username
private_key = file("${path.module}/ssh-keys/terraform-azure.pem")
}
}
resource "azurerm_public_ip" "bastion_ip" {
name = "bastion_ip"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_bastion_host" "bastion_test" {
name = "bastion-test"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
ip_configuration {
name = "bastion-configuration"
subnet_id = azurerm_subnet.Azure_Bastion_Subnet.id
public_ip_address_id = azurerm_public_ip.bastion_ip.id
}
}
Error: Invalid function argument
on main.tf line 125, in resource "azurerm_linux_virtual_machine" "mylinuxvm":
125: custom_data = filebase64(data.template_file.config.rendered)
├────────────────
│ while calling filebase64(path)
│ data.template_file.config.rendered is "#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p ~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\"
]\nthen\nmkdir -p ~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p ~/zookeeper/log/zk2\necho \"2\" >
~/zookeeper/data/zk2/myid\n\nelif [ \"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir -p
~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" > ~/zookeeper/data/zk3/myid\nfi\n\n"
Invalid value for "path" parameter: no file exists at
"#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir
-p ~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p
~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p
~/zookeeper/log/zk2\necho \"2\" > ~/zookeeper/data/zk2/myid\n\nelif [
\"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir
-p ~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" >
~/zookeeper/data/zk3/myid\nfi\n\n"; this function works only with files that
are distributed as part of the configuration source code, so if this file
will be created by a resource in this configuration you must instead obtain
this result from an attribute of that resource.
Error: Invalid function argument
on main.tf line 125, in resource "azurerm_linux_virtual_machine" "mylinuxvm":
125: custom_data = filebase64(data.template_file.config.rendered)
├────────────────
│ while calling filebase64(path)
│ data.template_file.config.rendered is "#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p ~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\"
]\nthen\nmkdir -p ~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p ~/zookeeper/log/zk2\necho \"2\" >
~/zookeeper/data/zk2/myid\n\nelif [ \"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir -p
~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" > ~/zookeeper/data/zk3/myid\nfi\n\n"
Invalid value for "path" parameter: no file exists at
"#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir
-p ~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p
~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p
~/zookeeper/log/zk2\necho \"2\" > ~/zookeeper/data/zk2/myid\n\nelif [
\"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir
-p ~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" >
~/zookeeper/data/zk3/myid\nfi\n\n"; this function works only with files that
are distributed as part of the configuration source code, so if this file
will be created by a resource in this configuration you must instead obtain
this result from an attribute of that resource.
Error: Invalid function argument
on main.tf line 125, in resource "azurerm_linux_virtual_machine" "mylinuxvm":
125: custom_data = filebase64(data.template_file.config.rendered)
├────────────────
│ while calling filebase64(path)
│ data.template_file.config.rendered is "#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p ~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\"
]\nthen\nmkdir -p ~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p ~/zookeeper/log/zk2\necho \"2\" >
~/zookeeper/data/zk2/myid\n\nelif [ \"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir -p
~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" > ~/zookeeper/data/zk3/myid\nfi\n\n"
Invalid value for "path" parameter: no file exists at
"#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir
-p ~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p
~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p
~/zookeeper/log/zk2\necho \"2\" > ~/zookeeper/data/zk2/myid\n\nelif [
\"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir
-p ~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" >
~/zookeeper/data/zk3/myid\nfi\n\n"; this function works only with files that
are distributed as part of the configuration source code, so if this file
will be created by a resource in this configuration you must instead obtain
this result from an attribute of that resource.