0
votes

I tried to use the remote-exec to execute several commands on target VM, but failed with 'bash: Permission denied', here is the code:

  connection {
    host        = "${azurerm_network_interface.nic.private_ip_address}"
    type        = "ssh"
    user        = "${var.mp_username}"
    private_key = "${file(var.mp_vm_private_key)}"
  }

  provisioner "remote-exec" {
    inline = [
      "sudo wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh",
      "sudo chown ${var.mp_username}: onboard_agent.sh",
      "sudo chmod +x onboard_agent.sh",
      "./onboard_agent.sh -w ${azurerm_log_analytics_workspace.workspace.workspace_id} -s ${azurerm_log_analytics_workspace.workspace.primary_shared_key} -d opinsights.azure.us"
    ]
  }

After checked the issue here: https://github.com/hashicorp/terraform/issues/5397, I need to wrap all the commands into a file. Then I used a template file to put all the commands in it:

OMSAgent.sh

#!/bin/bash
sudo wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh
sudo chown ${username}: onboard_agent.sh
sudo chmod +x onboard_agent.sh
./onboard_agent.sh -w ${workspaceId} -s ${workspaceKey} -d opinsights.azure.us

The code changes to:

data "template_file" "extension_data" {
  template = "${file("templates/OMSAgent.sh")}"

  vars = {
    workspaceId  = "${azurerm_log_analytics_workspace.workspace.workspace_id}"
    workspaceKey = "${azurerm_log_analytics_workspace.workspace.primary_shared_key}"
    username = "${var.mp_username}"
  }
}

resource "null_resource" "remote-provisioner" {
  connection {
    host        = "${azurerm_network_interface.nic.private_ip_address}"
    type        = "ssh"
    user        = "${var.mp_username}"
    private_key = "${file(var.mp_vm_private_key)}"
    script_path = "/home/${var.mp_username}/OMSAgent.sh"
  }

  provisioner "file" {
    content = "${data.template_file.extension_data.rendered}"
    destination  = "/home/${var.mp_username}/OMSAgent.sh"
  }

  provisioner "remote-exec" { 
    inline = [
      "chmod +x /home/${var.mp_username}/OMSAgent.sh",
      "/home/${var.mp_username}/OMSAgent.sh"
    ]
  }
}

But seems something wrong in the null_resource, the null resource installation stoped and throws this:

null_resource.remote-provisioner (remote-exec): /home/user/OMSAgent.sh: 2: /home/user/OMSAgent.sh: Cannot fork .

And the content for the shell script is this:

cat OMSAgent.sh
#!/bin/sh
chmod +x /home/user/OMSAgent.sh
/home/user/OMSAgent.sh

Seems I did the script in the wrong way.

1
What is the value of ${var.mp_username}?AmanGarg-MSFT
it is the all the same one for the terraform server and the vm been createdjoe huang

1 Answers

0
votes

@joe huang Please make sure you use the username and password provided when you created the os_profile for your VM:

os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }

https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html#example-usage-from-an-azure-platform-image-

Here is a document for installing the OMS agent:

https://support.microsoft.com/en-in/help/4131455/how-to-reinstall-operations-management-suite-oms-agent-for-linux

Hope this helps!