linux 即使在更改custom_data后,Terraform Azure VM也不会重新创建示例

y4ekin9u  于 2022-12-18  发布在  Linux
关注(0)|答案(1)|浏览(127)

我正在尝试配置azure虚拟机,并且需要在其上安装kubectl。因此,我正在使用bash脚本并将其传递到VM的custom_data部分。
一切正常-它配置了VM并安装了kubectl。
但问题是,如果我对bash脚本做了一些修改,然后执行terraform apply,它不会显示任何更改,因为它不会检测到我对bash脚本做了更改。
这是我的代码片段。有人能帮我理解一下吗?

locals {
  admin_username = "myjumphost"
  admin_password = "mypassword!610542"
}

resource "azurerm_virtual_machine" "vm" {
  name                  = var.vm_name
  location              = var.location
  resource_group_name   = var.rg_name
  network_interface_ids = var.nic_id
  vm_size               = var.vm_size

  delete_os_disk_on_termination = true

  delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = var.storage_image_reference.publisher
    offer     = var.storage_image_reference.offer
    sku       = var.storage_image_reference.sku
    version   = var.storage_image_reference.version
  }
  storage_os_disk {
    name              = var.storage_os_disk.name
    caching           = var.storage_os_disk.caching
    create_option     = var.storage_os_disk.create_option
    managed_disk_type = var.storage_os_disk.managed_disk_type
  }
  os_profile {
    computer_name  = var.vm_name
    admin_username = local.admin_username
    admin_password = local.admin_password
    custom_data    = file("${path.module}/${var.custom_data_script}")
  }

  os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
      key_data = file("${path.module}/${var.ssh_public_key}")
      path     = "/home/${local.admin_username}/.ssh/authorized_keys"
    }
  }

  tags = merge(var.common_tags)
}

任何我的脚本install.sh

#!/bin/bash

# install Kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client --output=yaml > /tmp/kubectl_version.yaml

# Install Azure CLI 
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
laximzn5

laximzn51#

请尝试刷新命令;它是向后兼容的。通常,它不是必需的,因为计划将执行相同的刷新。大多数混淆将由bash或PowerShell脚本引起,这些脚本作为cloud-init脚本在backed上运行并实现功能;我们可以在活动日志里交叉比对。

terraform apply -refresh-only -auto-approve

我已经尝试复制相同的与下面提到的代码库
主tf文件如下

resource "azurerm_resource_group" "example" {
  name     = "v-swarna-mindtree"
  location = "Germany West Central"
}
data "azuread_client_config" "current" {}

resource "azurerm_virtual_network" "puvnet" {
    name                = "Public_VNET"
   resource_group_name = azurerm_resource_group.example.name
  location            = "Germany West Central"
  address_space       = ["10.19.0.0/16"]
  dns_servers         = ["10.19.0.4", "10.19.0.5"]
}

resource "azurerm_subnet" "osubnet" {
  name                 = "Outer_Subnet"
  resource_group_name = azurerm_resource_group.example.name
  address_prefixes     = ["10.19.1.0/24"]
  virtual_network_name = azurerm_virtual_network.puvnet.name
}

resource "azurerm_network_interface" "main" {
  name                = "testdemo"
  location                = "Germany West Central"
  resource_group_name     = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.osubnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "main" {
   name                  = "vmjumphost"
  location              = "Germany West Central"
  resource_group_name =  azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.main.id]
  //vm_size               = "Standard_A1_v2"
  vm_size ="Standard_DS2_v2"
   storage_image_reference {
    offer                 = "0001-com-ubuntu-server-focal"
    publisher             = "Canonical"
    sku                   = "20_04-lts-gen2"
    version   = "latest"
  }
  storage_os_disk {
    name              = "myosdisk2"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "vm-swarnademo"
     admin_username = "testadmin"
    admin_password = "Password1234!"
  //  custom_data    = file("${path.module}/${var.custom_data_script}")
    custom_data = file("install.sh")
  }

  os_profile_linux_config {
    disable_password_authentication = false
    # ssh_keys {
    #   key_data = file("${path.module}/${var.ssh_public_key}")
    #   path     = "/home/${local.admin_username}/.ssh/authorized_keys"
    # }
  }
  tags = {
    environment = "staging"
  }
}

安装sh文件如下

#!/bin/bash

# install Kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client --output=yaml > /tmp/kubectl_version.yaml
#testing by adding command -start
#sudo apt-get -y update
#testing by adding command -End
#Install Azure CLI 
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

尝试使用以下命令行更新脚本

#testing by adding command -start
#sudo apt-get -y update
#testing by adding command -End

**步骤1:**在实施计划和应用时,已在门户上创建所有资源。x1c 0d1x

**步骤2:**通过启用代码库更新脚本

#testing by adding command -start
    sudo apt-get -y update
    #testing by adding command -End

运行plan and apply后,它将刷新虚拟机的状态和所有更改

从活动日志验证:

相关问题