Having some issue with increasing the EC2 resources for terraform and applying the changes with Ansible. I currently have a basic template with:
resource "aws_instance" "linux_node" {
count = 2
ami = "ami-6df1e514"
instance_type = "t2.micro"
key_name = ""
vpc_security_group_ids = [""]
iam_instance_profile = ""
user_data = "${file("userdata.sh")}"
tags {
Name = "linux-node"
}
provisioner "local-exec" {
command = "sleep 120; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --private-key ~/.ssh/aws-patrick-oregon.pem -i '${aws_instance.linux_node.public_ip},' ../install_packages.yml"
}
}
The issue when I increase the count to anything more than 1, I get an error on aws_instance.linux_node.public_ip
where the error says:
* aws_instance.linux_node[1]: 1 error(s) occurred:
* Resource 'aws_instance.linux_node' not found for variable 'aws_instance.linux_node.public_ip'
I think it stems from the bracket [1]. I'm wondering how to write a ansible command to pick up the change when I make the change to add an EC2 instance.
When a resource has count
set, it needs to be addressed in a different way to specify which of the instances you wish to use.
For provisioners in particular it's a common case to want to take actions based on the "current" instance the provisioner is running for, so a shorthand is provided with the self
variable type:
provisioner "local-exec" {
command = "sleep 120; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --private-key ~/.ssh/aws-patrick-oregon.pem -i '${self.public_ip},' ../install_packages.yml"
}