title: how to get private IP of EC 2 dynamically and put it in /etc/hosts
body: <p>I would like to create multiple EC2 instances using Terraform and write the private IP addresses of the instances to <code>/etc/hosts</code> on every instance.</p>
  
  <p>Currently I am trying the following code but it's not working:</p>
  
  <pre><code>resource "aws_instance" "ceph-cluster" {
    count = "${var.ceph_cluster_count}"
    ami           = "${var.app_ami}"
    instance_type = "t2.small"
    key_name      = "${var.ssh_key_name}"
  
    vpc_security_group_ids = [
      "${var.vpc_ssh_sg_ids}",
      "${aws_security_group.ceph.id}",
    ]
  
    subnet_id                   = "${element(split(",", var.subnet_ids), count.index)}"
  
    associate_public_ip_address = "true"
    // TODO 一時的にIAM固定
    //iam_instance_profile        = "${aws_iam_instance_profile.app_instance_profile.name}"
    iam_instance_profile        = "${var.iam_role_name}"
  
    root_block_device {
      delete_on_termination = "true"
      volume_size           = "30"
      volume_type           = "gp2"
    }
  
    connection {
      user        = "ubuntu"
      private_key = "${file("${var.ssh_key}")}"
      agent = "false"
    }
  
    provisioner "file" {
      source      = "../../../scripts"
      destination = "/home/ubuntu/"
    }
  
    tags {
      Name = "${var.infra_name}-ceph-cluster-${count.index}"
      InfraName = "${var.infra_name}"
    }
  
    provisioner "remote-exec" {
        inline = [
          "cat /etc/hosts",
          "cat ~/scripts/ceph/ceph_rsa.pub &gt;&gt; ~/.ssh/authorized_keys",
          "cp -arp  ~/scripts/ceph/ceph_rsa ~/.ssh/ceph_rsa",
          "chmod 700 ~/.ssh/ceph_rsa",
          "echo 'IdentityFile    ~/.ssh/ceph_rsa' &gt;&gt; ~/.ssh/config",
          "echo 'User            ubuntu' &gt;&gt; ~/.ssh/config",
          "echo '${aws_instance.ceph-cluster.0.private_ip} node01 ceph01' &gt;&gt; /etc/hosts ",
          "echo '${aws_instance.ceph-cluster.1.private_ip} node02 ceph02' &gt;&gt; /etc/hosts "
        ]
    }
  
  }
  
  
  aws_instance.ceph-cluster. *. private_ip
  </code></pre>
  
  <p>I would like to get the result of the above command and put it in <code>/etc/hosts</code>.</p>