C
Coder.com•2mo ago
Andrej

Bug with local exec in a module

Local exec works fine in terraform script in root, but once it is in any module, it is throwing weird errors. To reproduce this issue create following template: |main.tf |/module/main.tf
# main.tf
module "test_exec" {
source = "./module"
}

resource "null_resource" "main_test_exec" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = <<EOT
whoami
ls ~/.ssh/
EOT
}
}
# main.tf
module "test_exec" {
source = "./module"
}

resource "null_resource" "main_test_exec" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = <<EOT
whoami
ls ~/.ssh/
EOT
}
}
# module/main.tf
resource "null_resource" "module_test_exec" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = <<EOT
whoami
ls ~/.ssh/
EOT
}
}
# module/main.tf
resource "null_resource" "module_test_exec" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = <<EOT
whoami
ls ~/.ssh/
EOT
}
}
With this template, first local exec finishes successfully, but the second one fails with:
No description
35 Replies
Codercord
Codercord•2mo ago
<#1304458286367637535>
Category
Bug report
Product
Coder OSS (v2)
Platform
Linux
Logs
Please post any relevant logs/error messages.
Andrej
AndrejOP•2mo ago
when using this directory on both my local machine and INSIDE docker coder container, terraform apply finishes with no errors and both list content of ssh dir.
Phorcys
Phorcys•2mo ago
hey @Andrej, what goal are you trying to achieve by using local-exec?
Andrej
AndrejOP•2mo ago
we have this project to setup kubernetes using openstack and ansible. what im trying to do is run all of this in coder, running ansible with some other scripts to set it up. The code i sent above is just to reproduce the issue. I wanted to move all of the code to a module, so that coder template consists only coder parameters, module resouce and coder metadata i managed to do this kubernetes setup when i ran the local-exec in the root, but once moved to a module, it stopped working
Phorcys
Phorcys•2mo ago
so if I understand correctly, you're running the ansible CLI and/or other scripts using local-exec within Coder? just to make sure, you are aware that this will run the command within the Coder host, and not within the workspace right?
Andrej
AndrejOP•2mo ago
yes, im aware @Phorcys do you think i should use startup_script for this instead?
Phorcys
Phorcys•2mo ago
startup_script will run within the workspace so if that's what you want yes the issue is that i'm having trouble understanding what you're really doing the idea is to run an ansible playbook within the context of a Coder template to provision a kubernetes instance?
Andrej
AndrejOP•2mo ago
yeah basically
Phorcys
Phorcys•2mo ago
is it to provision a kubernetes test cluster per workspace or to actually manage production clusters ?
Andrej
AndrejOP•2mo ago
first one
Phorcys
Phorcys•2mo ago
and why not have terraform do that provisioning instead of the ansible playbook?
Andrej
AndrejOP•2mo ago
because we are creating the kubernetes clusters on the openstack cloud
Phorcys
Phorcys•2mo ago
Terraform has a provider for OpenStack
Andrej
AndrejOP•2mo ago
i know, i am using it to first setup openstack VMs and then ansible to setup kubernetes
Phorcys
Phorcys•2mo ago
ah okay i get it now I think you might want to run and apply the playbook on the same machine i know it's not ideal but it's probably the best solution because using local-exec providers isn't really something you want to do
Andrej
AndrejOP•2mo ago
yeah, those were the 2 options 😄 do you think its possible to do it in the startup script though?
Phorcys
Phorcys•2mo ago
yeah sorry it took me a bit to understand as it's pretty unusual
Andrej
AndrejOP•2mo ago
dont worry, i really appriciate your effort
Phorcys
Phorcys•2mo ago
well all you should have to do is install the ansible CLI in the workspace and run the playbook
Andrej
AndrejOP•2mo ago
yes
Phorcys
Phorcys•2mo ago
so you can either install the Ansible CLI within your base image or do it inside the startup_script
Andrej
AndrejOP•2mo ago
i have ansible-playbook inside the base image now
Phorcys
Phorcys•2mo ago
okay do you host the playbook in some kind of git repository or something? that would be the easiest way to get it into the workspace, and then you just have to run it
Andrej
AndrejOP•2mo ago
yes but im struggling to understand how the startup script works. I used it to do basic command like ls or sleep , but its creation finished in 0 seconds
Phorcys
Phorcys•2mo ago
yes that makes sense basically the startup script runs as soon as the agent gets executed on a machine so you can't wait for it in the Terraform side of things, otherwise it would just stall forever, so Coder is the one doing the waiting after the Terraform stage has finished
Andrej
AndrejOP•2mo ago
and the agent is executed on a machine by the init_script?
Phorcys
Phorcys•2mo ago
yes basically you would provision a VM, then run the agent's init script in the VM, which would in turn run your startup_script and associated coder_script resources
Andrej
AndrejOP•2mo ago
damn, okay i didnt get this from the docs really 😄 let me absorb all this, try it and ill post the result here. thank you sir
Phorcys
Phorcys•2mo ago
oh, is there any parts of the docs that you found unclear or that you think should be edited to make it more understandeable?
Andrej
AndrejOP•2mo ago
now that i understand what is going on i will have a look again and report back to you
Phorcys
Phorcys•3w ago
thanks a lot! hey @Andrej, any luck?
Andrej
AndrejOP•3w ago
hey, yes. i moved everything to the coder script and now it works. i am using it as part of my thesis, so i can share the results once im finished
Phorcys
Phorcys•3w ago
thanks :-)
Codercord
Codercord•3w ago
@Phorcys closed the thread.

Did you find this page helpful?