2
votes

I have three resources resp. VM types I want to add. For each resource I want to run a remote-exec and place some files using the corresponding provisioner. As I don't want to have the provisioners repeated in each resource, I thought about writing a (resp. another) module. I created a module which looks like that:

variable "cicd-user" {
  default = [
    "useradd cicd-user",
    "echo cicd-user: | /usr/sbin/chpasswd",
    "su - cicd-user -c \"mkdir /home/cicd-user/.ssh/\"",
    "su - cicd-user -c \"touch /home/cicd-user/.ssh/authorized_keys\"",
    "su - cicd-user -c \"chmod 700 /home/cicd-user/.ssh/\"",
    "su - cicd-user -c \"chmod 600 /home/cicd-user/.ssh/authorized_keys\"",
  ]
}
variable "sudoers_content" {
  default = "cicd-user ALL=(ALL)       NOPASSWD: ALL"
}
variable "sudoers_destination" {
  default = "/etc/sudoers.d/cicd"
}

The main.tf has the following:

module "provisioner_info" {
  source = "./provisioner_info"
}
...
resource "vsphere_virtual_machine" "web" {
...
  provisioner "remote-exec" {
    inline = module.provisioner_info.cicd-user
  }
  provisioner "file" {
    content     = module.provisioner_info.sudoers_content
    destination = module.provisioner_info.sudoers_destination
  }
...

That doesn't work as I would have expected that, I get the following error:

terraform plan

Error: Unsupported attribute

  on main.tf line 81, in resource "vsphere_virtual_machine" "web":
  81:     inline = module.provisioner_info.cicd-user

This object does not have an attribute named "cicd-user".

Can someone tell me, what I'm doing wrong? Do I misuse the module functionality?

1
Not user if that's relevant but I actually trigger another main.tf which loads then the main.tf I'm showing above.lulukas86

1 Answers

2
votes

You need to provide an output for the provisioner_info module:

output "cicd-user" {
  value       = var.cicd-user
  description = "The cicd-user."
}

and then it will be accessible to a config declaring the module as provisioner_info in the namespace module.provisioner_info.cicd-user as you have done above.