首页 > 解决方案 > 如何从 terraform 模块中提取私有 IP 地址

问题描述

Terraform代码如下

module "centos-vm-author-2" {
  source                      = "terraform.automation.temp.com.au/temp/temp-linux-vm/azurerm"
  version                     = "6.7.0"
  location                    = var.resource_location
  resource_group_name         = var.resource_group_name_2
  vm_count                    = "1"
  tags                        = local.tags
  size                        = var.vm_size
  hostname_prefix             = var.hostname_prefix
  hostname_suffix_start_range = "491"
  image_publisher             = "OpenLogic"
  image_offer                 = "Centos"
  image_sku                   = "7_9"
  subnet_id                   = var.auth_pub_subnet_id
  admin_username              = "azureadmin"
  availability_set_id         = azurerm_availability_set.aemfeature1authoras.id
  patching_tags               = local.patching_tags
  ansible_vault_key           = var.ansible_vault_key
  log_to_loganalytics         = false
  ou_tags                     = local.ou_tags
  os_disk_size                = var.os_size_gb
  os_disk_type                = var.storage_account_type
  server_access_memberships   = ["CN=DSTDEVOPS,OU=DistributionGroups,OU=Groups,OU=Resources,DC=temp,DC=int"]
  sudoers                     = ["%DSTDEVOPS"]
  data_disks = [
    [
      {
        disk_size_gb         = var.disk_size_gb
        storage_account_type = var.storage_account_type
        caching              = "ReadWrite"
        create_option        = "Empty"
        source_resource_id   = ""
        write_accelerator_enabled = false
      }
    ]
  ]
}
resource "null_resource" "centos-vm-author-ansible" {
  provisioner "local-exec" {
  command     = <<EOF
  ansible-playbook -i '${join(",", azurerm_network_interface.centos-vm-author-2.*.private_ip_address)},'-e ansible_user=${var.admin_username} -e "role_name=automate-author" main.yaml
  EOF
  }
  depends_on = [
    module.centos-vm-author-2
  ]
}
}


基本上我想告诉 Ansible 它应该执行角色的私有 IP。

我收到如下错误,

Error: [0m[0m[1mReference to undeclared resource[0m
on main.tf line 236, in resource "null_resource" "centos-vm-author-ansible":
ansible-playbook -i '${join(",", [4mazurerm_network_interface.centos-vm-author-2.*.private_ip_address)},'-e ansible_user=${var.admin_username} -e "role_name=automate-author" main.yaml
A managed resource "azurerm_network_interface" "centos-vm-author-2" has not
been declared in the root module.

衷心感谢任何帮助以了解问题所在以及如何解决问题。PS:TF模块代码如下:

resource "azurerm_network_interface" "main" {
  count                         = var.vm_count
  name                          = "${format("${var.hostname_prefix}%04d", var.hostname_suffix_start_range + count.index, )}-nic"
  location                      = var.location
  resource_group_name           = var.resource_group_name
  enable_accelerated_networking = var.enable_accelerated_networking
  ip_configuration {
    name                          = "${format("${var.hostname_prefix}%04d", var.hostname_suffix_start_range + count.index, )}-ipconfig"
    subnet_id                     = var.subnet_id
    private_ip_address_allocation = var.private_ip_address_allocation
    private_ip_address            = var.private_ip_address
    public_ip_address_id          = var.enable_public_ip_address ? azurerm_public_ip.main[count.index].id : null
  }
  tags = var.tags
}

resource "azurerm_network_interface_backend_address_pool_association" "lbconf" {
  count                   = var.backend_address_pool_id == null ? 0 : var.vm_count
  network_interface_id    = azurerm_network_interface.main[count.index].id
  ip_configuration_name   = azurerm_network_interface.main[count.index].ip_configuration[0].name
  backend_address_pool_id = var.backend_address_pool_id
}

resource "azurerm_linux_virtual_machine" "main" {
  count               = var.vm_count
  name                = format("${var.hostname_prefix}%04d", var.hostname_suffix_start_range + count.index, )
  location            = var.location
  resource_group_name = var.resource_group_name
  admin_username      = var.admin_username
  admin_ssh_key {
    username   = var.admin_username
    public_key = chomp(tls_private_key.bootstrap_private_key.public_key_openssh)
  }
  disable_password_authentication = var.disable_password_authentication
  network_interface_ids           = [azurerm_network_interface.main[count.index].id]
  size                            = var.size
  availability_set_id             = var.availability_set_id

  source_image_reference {
    publisher = var.image_publisher
    offer     = var.image_offer
    sku       = var.image_sku
    version   = var.image_version
  }

  os_disk {
    name                 = "${format("${var.hostname_prefix}%04d", var.hostname_suffix_start_range + count.index, )}-osdisk"
    caching              = "ReadWrite"
    storage_account_type = var.os_disk_type
    disk_size_gb         = var.os_disk_size
  }
  dynamic "identity" {
    for_each = var.identity
    content {
      type         = identity.value["type"]
      identity_ids = identity.value["type"] == "SystemAssigned" ? [] : identity.value["identity_ids"]
    }
  }

  dynamic "plan" {
    for_each = var.marketplace_image ? [1] : []
    content {
      name      = var.image_sku
      product   = var.image_offer
      publisher = var.image_publisher
    }
  }

  #  boot_diagnostics {
  #    storage_account_uri = var.boot_diagnostics_storage_uri
  #  }

  tags = var.ou_tags == null ? merge(var.tags, var.patching_tags) : merge(var.tags, var.ou_tags, var.patching_tags)
}

标签: terraform

解决方案


要引用您的模块,而不是:

azurerm_network_interface.centos-vm-author-2.*.private_ip_address

它应该是:

module.centos-vm-author-2.private_ip_addresses

推荐阅读