azurerm_virtual_machine os_profile custom_data HOWTO?

2,297 views
Skip to first unread message

Steve Button

unread,
Jan 22, 2018, 10:11:18 AM1/22/18
to Terraform
Hi,

I'm trying to set up some custom data for some Ubuntu VMs that I'm spinning up in Azure, however I'm a little confused by the documentation. It mentions using a here document (using <<-EOF) or putting the script steps straight into a string, but I would like to put them into a separate file. I looked at file provisioners as well, but this is not what I need.

Lots of googling, but not found any decent example code how to do this.

Thanks,

Steve

Lowe Schmidt

unread,
Jan 22, 2018, 1:34:54 PM1/22/18
to terrafo...@googlegroups.com
You can use ${file(your_file)} to read a file from disk instead of using a heredoc.

--
Lowe Schmidt | +46 723 867 157

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/c14970d6-d746-47a4-a83c-9c1f4d9818be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steve Button

unread,
Jan 23, 2018, 4:54:00 AM1/23/18
to Terraform
Thanks for that, it's useful. 

After reading the docs some more I think I need to use a template, as I'm setting up an Elastic Search cluster with three nodes. Each of the nodes will need a slightly different configuration file for ES, for example node.name=es01 and node.name=es02. 

I've tried putting in a data source of "template_file" and copying the .sh script into a .tpl, and using this code :-


data "template_file" "setup_elasticsearch" {
  template = "${file("setup_elasticsearch.tpl")}"
  vars {
    elasicsearch_instance = "01"
  }
}

However, I only put in the variable as a test to try and get it to pass the "terraform plan". 

Within the .tpl I've got :-

sudo docker run \
  -d \
  -e "node.name=es${elasticsearch_instance}" \

And I simply need to call this template 3 times, once for each VM and pass in a different node number each time. 

The docs I'm looking at 


are really good for explaining how to set up the provider, but not for how to actually call it. 

I'm probably just looking in the wrong place in the docs, but there is SO MUCH excellent documentation, it's hard to find what you're looking for sometimes!? 

Any pointers much appreciated, but I'll carry on ploughing through it myself. 

Thanks,

Steve Button



On Monday, 22 January 2018 18:34:54 UTC, Lowe Schmidt wrote:
You can use ${file(your_file)} to read a file from disk instead of using a heredoc.

--
Lowe Schmidt | +46 723 867 157

On 22 January 2018 at 16:11, Steve Button <steve....@gmail.com> wrote:
Hi,

I'm trying to set up some custom data for some Ubuntu VMs that I'm spinning up in Azure, however I'm a little confused by the documentation. It mentions using a here document (using <<-EOF) or putting the script steps straight into a string, but I would like to put them into a separate file. I looked at file provisioners as well, but this is not what I need.

Lots of googling, but not found any decent example code how to do this.

Thanks,

Steve

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Steve Button

unread,
Jan 23, 2018, 6:34:20 AM1/23/18
to Terraform
Hi,

After finding this on the page :-


I'm getting closer to making this work. 

I've changed the template data source to have a count = "3", but that just makes the azurerm_virtual_machine_extension resource give the error message 3 times, instead of once. 

Error: Error refreshing state: 1 error(s) occurred:

* data.template_file.setup_elasticsearch: 3 error(s) occurred:

* data.template_file.setup_elasticsearch[1]: data.template_file.setup_elasticsearch.1: failed to render : 4:21: unknown variable accessed: elasticsearch_instance
* data.template_file.setup_elasticsearch[2]: data.template_file.setup_elasticsearch.2: failed to render : 4:21: unknown variable accessed: elasticsearch_instance
* data.template_file.setup_elasticsearch[0]: data.template_file.setup_elasticsearch.0: failed to render : 4:21: unknown variable accessed: elasticsearch_instance

There doesn't seem to be a count parameter on the azurerm_virtual_machine or azurerm_virtual_machine_extension, but perhaps these are generic parameters which you can set with any resource??

I think I'm getting this "inside out" somehow, but I just don't get how to make it work.

Steve Button

unread,
Jan 23, 2018, 8:07:32 AM1/23/18
to Terraform
Still banging my head against a wall with this one. I'm now facing this problem :-

resource "azurerm_virtual_machine_extension" "elastic_uk0" {
  count                 = 3
  name                  = "uks-elastic-vm-0-ext"
  location              = "${azurerm_resource_group.elastic_uk.location}"
  resource_group_name   = "${azurerm_resource_group.elastic_uk.name}"
  #virtual_machine_name  = "${azurerm_virtual_machine.elastic_uk0[count.index].name}"
  virtual_machine_name  = "${element(azurerm_virtual_machine.elastic_uk0.*.name,count.index)}"
  publisher             = "Microsoft.OSTCExtensions"
  type                  = "CustomScriptForLinux"
  type_handler_version  = "1.2"
  #settings              = "${data.template_file.setup_elasticsearch.*.rendered[count.index]}"

  settings             = <<SETTINGS
    {
      "fileUris": [
        ""
      ],
      "commmandToExecute": "./setup_docker.sh && ./setup_elasticsearch-0.sh && ./setup_kibana.sh"
    }
SETTINGS
}

I need to get the commandToExecute to run a different setup_elasticsearch script for each of the nodes that get created. Or send a parameter to the script. Also, how do I even get the scripts onto the node itself? I suppose I could use a file copy resource for that. 

Steve Button

unread,
Jan 24, 2018, 5:37:43 AM1/24/18
to Terraform
Fixed that with :-  

settings             = <<SETTINGS
    {
      "fileUris": [
        ""
      ],
      "commmandToExecute": "./setup_docker.sh && ./setup_elasticsearch-0.sh ${count.index} && ./setup_kibana.sh"
    }
SETTINGS

And then using $1 within the script. 

Lowe Schmidt

unread,
Jan 24, 2018, 7:53:57 AM1/24/18
to terrafo...@googlegroups.com
Awesome work Steve :)

--
Lowe Schmidt | +46 723 867 157

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/0c25d6c9-20bc-42e6-9dbe-ac7d07163132%40googlegroups.com.

mis...@hashicorp.com

unread,
May 22, 2018, 2:20:49 AM5/22/18
to Terraform
This is really helpful. :)
Reply all
Reply to author
Forward
0 new messages