一千萬個為什麽

搜索

如何使用Terraform和cloud-init為EC2節點提供多個配置?



我需要使用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 說:

[[email protected] 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 () failed

編輯:

我有一個類似的,但適當的工作配置為我的後端節點:

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}"
    }
}

轉載註明原文: 如何使用Terraform和cloud-init為EC2節點提供多個配置?

一共有 0 個回答: