user389955
user389955

Reputation: 10467

cannot run ansible to AWS EC2 created by terraform

I have EC2 created by terraform, and I can login the ec2 using: ssh -vvvv -i /home/ec2-user/.ssh/mykey.pub [email protected] without password,(XX.XX.XX.XX) is the IP of the EC2 created by terraform.

but when I try to run ansible file in terraform when ec2 is created, ansible cannot run and error message is:

aws_instance.dev (local-exec): TASK [Gathering Facts] 
*********************************************************
The authenticity of host 'XX.XX.XX.XX (XX.XX.XX.XX)' can't be 
established.
...
Are you sure you want to continue connecting (yes/no)? 
aws_instance.dev: Still creating... (6m40s elapsed)

note the ansible yml is started after I manually force the terraform to sleep for 6m and at that time, the EC2 already started (I can login it myself, although it showed "aws_instance.dev: Still creating...") i.e.

resource "aws_instance" "dev" {
  ...
      provisioner "local-exec" {
  command = "sleep 6m && ansible-playbook -i hosts myansible.yml"
  }
  ...
}

I run the terraform as ec2-user, I set ansible yml as:

  remote_user: ec2-user
  become_user: ec2-user

what is the reason the ansible cannot ssh to the EC2?

Upvotes: 0

Views: 639

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

There is a message for you:

The authenticity of host 'XX.XX.XX.XX (XX.XX.XX.XX)' can't be established.
...
Are you sure you want to continue connecting (yes/no)?

Either execute ssh-keyscan XX.XX.XX.XX before executing ansible-playbook, or disable host key checking in ansible.

Upvotes: 1

Related Questions