如何使用Terraform和cloud-init为EC2节点提供多个配置?

时间:2017-08-10 11:34:45

标签: amazon-web-services amazon-ec2 terraform cloud-init

我需要使用Terraform在我的EC2节点上使用几个配置。

data "template_cloudinit_config" "puppetserver_config" {
    gzip             = true
    base64_encode    = true
    part {
        filename     = "initial.sh"
        content_type = "text/x-shellscript"
        content      = "${data.template_file.initial_config.rendered}"
    }

    part {
        filename     = "puppetserver_config.cfg"
        content_type = "text/cloud-config"
        content      = "${data.template_file.puppetserver_config.rendered}"
    }
}
data "template_file" "puppetserver_config" {
    template = "${file("${path.module}/puppetserver_config.tpl")}"
}

data "template_file" "initial_config" {
    template = "${file("${path.module}/initial_config.sh")}"
}

节点声明:

resource "aws_instance" "puppetserver" {
    depends_on             = ["aws_security_group.puppetserver"]
    count                  = 1
    key_name               = "${var.key_name}"
    ami                    = "${data.aws_ami.centos7.id}"
    instance_type          = "${var.instance_type}"
    user_data              = "${data.template_cloudinit_config.puppetserver_config.rendered}"
    security_groups        = ["${aws_security_group.puppetserver.name}"]

    tags {
        Name = "puppet"
    }
}

但是当它被引导时我只看到initial_config有效。 puppetserver_config被忽略。

经过数小时的故障排除后,我发现编码可能有问题。 /var/log/cloud-init.log说:

[root@ip-172-31-29-73 ec2-user]# cat /var/log/cloud-init.log
Aug 10 09:45:24 ip-172-31-29-73 cloud-init: Cloud-init v. 0.7.6 running 'init-local' at Thu, 10 Aug 2017 13:45:24 +0000. Up 5.79 seconds.
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: Cloud-init v. 0.7.6 running 'init' at Thu, 10 Aug 2017 13:45:31 +0000. Up 12.31 seconds.
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: ++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +--------+------+--------------+---------------+-------------------+
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: | Device |  Up  |   Address    |      Mask     |     Hw-Address
|
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +--------+------+--------------+---------------+-------------------+
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: |  lo:   | True |  127.0.0.1   |   255.0.0.0   |         .
|
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: | eth0:  | True | 172.31.29.73 | 255.255.240.0 | 0a:99:c0:8b:b0:60 |
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +--------+------+--------------+---------------+-------------------+
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +++++++++++++++++++++++++++++++Route info++++++++++++++++++++++++++++++++
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +-------+-------------+-------------+---------------+-----------+-------+
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: | Route | Destination |   Gateway   |    Genmask    | Interface | Flags |
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +-------+-------------+-------------+---------------+-----------+-------+
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: |   0   |   0.0.0.0   | 172.31.16.1 |    0.0.0.0    |    eth0   |
 UG  |
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: |   1   | 172.31.16.0 |   0.0.0.0   | 255.255.240.0 |    eth0   |
 U   |
Aug 10 09:45:31 ip-172-31-29-73 cloud-init: ci-info: +-------+-------------+-------------+---------------+-----------+-------+
Aug 10 09:45:32 ip-172-31-29-73 cloud-init: 2017-08-10 09:45:32,367 - cc_write_files.py[WARNING]: Undecodable permissions None, assuming 420
Aug 10 09:45:32 ip-172-31-29-73 cloud-init: 2017-08-10 09:45:32,372 - util.py[WARNING]: Running module write-files (<module 'cloudinit.config.cc_write_files' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_write_files.pyc'>) failed

要使其正常工作,我需要做些什么?也许我的配置有错误的扩展名?或者我只是不触发它的&#39;发射?

编辑:

我的后端节点有一个类似但正常工作的配置:

data "template_file" "backendnode_config" {
    template = "${file("${path.module}/backendnode_config.tpl")}"
    vars {
        puppetserver_address = "${aws_instance.puppetserver.private_ip}"
        puppet_environment   = "${var.puppet_environment}"
    }
}
data "template_cloudinit_config" "backendnode_config" {
    gzip             = true
    base64_encode    = true
    part {
        filename     = "initial.sh"
        content_type = "text/x-shellscript"
        content      = "${data.template_file.initial_config.rendered}"
    }
    part {
        filename     = "backendnode_config.cfg"
        content_type = "text/cloud-config"
        content      = "${data.template_file.backendnode_config.rendered}"
    }
}

0 个答案:

没有答案