Creating more than one dev host

So I used the count function to create more than one host but do not know what code I need for ansible to pull all of the host to config them...Below is my code

resource "aws_key_pair" "k8s-node_auth" {

key_name = "${var.key_name2}"

public_key = "${file(var.public_key_path2)}"

}

#-----Workernodes-----

resource "aws_instance" "nodes-opt-us1-k8s" {

instance_type = "${var.k8s-node_instance_type}"

ami = "${var.k8s-node_ami}"

count = "${var.NodeCount}"

tags {

Name = "nodes-opt-us1-k8s"

}

key_name = "${aws_key_pair.k8s-node_auth.id}"

vpc_security_group_ids = ["${aws_security_group.opt-us1-k8s_sg.id}"]

subnet_id = "${aws_subnet.opt-us1-k8s.id}"

  }



I then will use this:

provisioner "local-exec" {

command = "aws ec2 wait instance-status-ok --instance-ids ${aws_instance.master-opt-us1-k8s.id} --profile Terraform && ansible-playbook -i aws_hosts Kubernetes-master.yml"

}




  • post-author-pic
    Chase M
    10-15-2018

    I know I need to update this somehow but not 100%

    provisioner "local-exec" {

    command = <<EOD

    cat <<EOF > aws_hosts

    [nodes]

    ${aws_instance.nodes-opt-us1-k8s.public_ip}

    [nodes:vars]

    s3code=${aws_s3_bucket.code.bucket}

    domain=${var.domain_name}

    EOF

    EOD


  • post-author-pic
    Chase M
    10-16-2018

    nobody able to help?

  • post-author-pic
    Chase M
    10-16-2018

     @derekm1215  this is the error I am getting


    aws_instance.nodes-opt-us1-k8s[0]: 1 error occured:

    Resource 'aws_instance.nodes-opt-us1-k8s' not found for variable 'aws_instance.nodes-opt-us1-k8s.public_ip'

    here is my main.tf file shorten to error area:

    #-----key pair for Workernodes-----

    resource "aws_key_pair" "k8s-node_auth" {

    key_name = "${var.key_name2}"

    public_key = "${file(var.public_key_path2)}"

    }

    #-----Workernodes-----

    resource "aws_instance" "nodes-opt-us1-k8s" {

    instance_type = "${var.k8s-node_instance_type}"

    ami = "${var.k8s-node_ami}"

    count = "${var.NodeCount}"

    tags {

    Name = "nodes-opt-us1-k8s"

    }

    key_name = "${aws_key_pair.k8s-node_auth.id}"

    vpc_security_group_ids = ["${aws_security_group.opt-us1-k8s_sg.id}"]

    subnet_id = "${aws_subnet.opt-us1-k8s.id}"

    #-----Link Terraform worker nodes to Ansible playbooks-----

    provisioner "local-exec" {

    command = <<EOD

    cat <<EOF > aws_worker_nodes_IP

    [workers]

    ${aws_instance.nodes-opt-us1-k8s.public_ip}    <------DO I NEED MORE?

    EOF

    EOD

    }

    }

    and the other files with the count variable linked:

    NodeCount = "2"

    variable "NodeCount" {}



    When I change the count to just one in the other file it runs right, but not when I place more than one in the vars file

  • post-author-pic
    Chase M
    10-16-2018

    This did not work:


    provisioner "local-exec" {

    command = <<EOD

    cat <<EOF > aws_worker_nodes_IP

    [workers]

    ${aws_instance.nodes-opt-us1-k8s.public_ip[0]}

    ${aws_instance.nodes-opt-us1-k8s.public_ip[1]}

    EOF

    EOD

  • post-author-pic
    Derek M
    10-17-2018

    Are you running the Ansible commands during build or are you just creating a list to run commands on later? In the course, I actually create a golden AMI out of that one instead of running ansible on all of the instances. I create the AMI, then deploy the instances from that AMI. It removes a lot of those moving parts and makes things easier to deal with. You can this populate an Ansible hosts file using the outputs. 

  • post-author-pic
    Chase M
    10-17-2018

    MIght give that a try ....Thanks Derek

  • post-author-pic
    Chase M
    10-19-2018

     @derekm1215  how do you create the ansible host file using outputs?

Looking For Team Training?

Learn More