在启动EC2时,我可以在user_data字段中包含带参数的脚本文件吗?

时间:2017-12-11 02:04:56

标签: amazon-web-services terraform

启动EC2时,我可以在user_data字段中包含带参数的脚本文件吗? 如:     user_data =" $ {file(" install_software.sh"),user_name pass_word}"

此外,我可以在user_data字段中包含多个脚本文件吗?如:     user_data =" $ {file(" install_software1.sh"," install_software2.sh")}"

我还没有看到它们的任何例子,所以我不知道格式是什么。如果我不能使用user_data这样做,该怎么做?我应该使用remote-exec还是......别的?

=============更新: 谢谢KOE回复我。在这里,我发布完整的答案,如果某人。别的需要。我测试了它们。

++++++++++++++++++++++++++++++++++++++++++++++++++++++

userdata_arguments_test.tf:

data "template_file" "user_data_test" {
      template = "${file("userdata_arg_test.tfl")}"
      vars {
        myuser = "${var.myuser}"
        mypassword = "${var.mypassword}"
      }
}
resource "aws_instance" "user_data_test" {
......
  user_data = "${data.template_file.user_data_test.rendered}"
}

userdata_arg_test.tfl

#!/bin/bash
echo ${myuser}, ${mypassword} >/tmp/my_info.log

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

userdata_mfiles_test.tf:

 data "template_file" "user_data_common" {
  template = "${file("userdata_arg_test.tfl")}"
  vars {
    myuser = "${var.myuser}"
    mypassword = "${var.mypassword}"
  }
}
data "template_file" "user_data_master" {
  template = "${file("userdata_arg_test2.tfl")}"
  vars {
    myuser2 = "${var.myuser}"
    mypassword2 = "${var.mypassword}"
  }
}
data "template_cloudinit_config" "master" {
  gzip          = true
  base64_encode = true

 part {
   content_type = "text/x-shellscript"
   content      = "${data.template_file.user_data_common.rendered}"
 }

  part {
    content_type = "text/x-shellscript"
    content      = "${data.template_file.user_data_master.rendered}"
  }
}
resource "aws_instance" "user_data_test" {
  ....
  user_data     = "${data.template_cloudinit_config.master.rendered}"
}

userdata_arg_test.tfl

#!/bin/bash
echo ${myuser}, ${mypassword} >/tmp/my_info.log

userdata_arg_test2.tfl

#!/bin/bash
echo ${myuser2} + ${mypassword2} >/tmp/my_info2.log

2 个答案:

答案 0 :(得分:3)

您要使用的是template_file

data "template_file" "user_data" {
  template = "${file("script1.sh.tpl")}"

  vars {
    var1 = "foo"
    var2 = "bar"
  }
}

如果要在user_data中使用多个脚本,则需要遵循cloud-init多部分格式并将其放在一个或多个文件* .tpl中,然后使用terraform标准插值或内联模板进行组合。

参考:

https://www.terraform.io/docs/providers/template/d/file.html http://cloudinit.readthedocs.io/en/latest/topics/format.html#mime-multi-part-archive https://github.com/ukayani/cloud-init-example/blob/master/SampleUserData

答案 1 :(得分:1)

感谢KOE回复我。在这里,我发布完整的答案,如果某人。别的需要。我测试了它们。

如何在user_data字段中包含参数

<强> userdata_arguments_test.tf:

data "template_file" "user_data_test" {
      template = "${file("userdata_arg_test.tfl")}"
      vars {
        myuser = "${var.myuser}"
        mypassword = "${var.mypassword}"
      }
}

resource "aws_instance" "user_data_test" {
......
  user_data = "${data.template_file.user_data_test.rendered}"
}

<强> userdata_arg_test.tfl

#!/bin/bash
echo ${myuser}, ${mypassword} >/tmp/my_info.log

如何在user_data字段中包含多个文件

<强> userdata_mfiles_test.tf:

 data "template_file" "user_data_common" {
  template = "${file("userdata_arg_test.tfl")}"
  vars {
    myuser = "${var.myuser}"
    mypassword = "${var.mypassword}"
  }
}

data "template_file" "user_data_master" {
  template = "${file("userdata_arg_test2.tfl")}"
  vars {
    myuser2 = "${var.myuser}"
    mypassword2 = "${var.mypassword}"
  }
}

data "template_cloudinit_config" "master" {
  gzip          = true
  base64_encode = true

 part {
   content_type = "text/x-shellscript"
   content      = "${data.template_file.user_data_common.rendered}"
 }

  part {
    content_type = "text/x-shellscript"
    content      = "${data.template_file.user_data_master.rendered}"
  }
}

resource "aws_instance" "user_data_test" {
  ....
  user_data     = "${data.template_cloudinit_config.master.rendered}"
}

<强> userdata_arg_test.tfl

#!/bin/bash
echo ${myuser}, ${mypassword} >/tmp/my_info.log

<强> userdata_arg_test2.tfl

#!/bin/bash
echo ${myuser2} + ${mypassword2} >/tmp/my_info2.log