I’m trying to use Terraform to build my aws EC2 infrastructure and use Anisble for configuration management with null_resource. I also tried to use the remote exec and the local exec. See Code below. I’m reaching the following:
Error: remote-exec provisioner error
with null_resource.example
on main.tf line 15, in resource “null_resource” “example”:
error executing “/tmp/terraform_853853788.sh”: Process exited with status 126
Thanks in Advance
Michael
resource “aws_instance” “example” {
ami = “ami-000000…”
instance_type = “t2.medium”
vpc_security_group_ids = [“sg-00000000000x”]
key_name = “Test-Key”
tags = {
name = “example”
}
}
resource “null_resource” “example” {
provisioner “remote-exec” {
connection {
type = “ssh”
user = “centos”
private_key = file(“Test-Key-1.pem”)
host = aws_instance.example.private_ip
file = file(“files/id_rsa”)
}
inline = [“echo ‘connected!’”]
}
provisioner “local-exec” {
command = “ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u centos --private-key ./Test-Key-1.pem -T 300 -i ${aws_instance.example.public_ip}, playbook.yml”
}
}
Hi @mmauney,
Unless you are executing terraform from within the same private network as the aws_instance.
, the remote-exec
provisioner is going to need the same public_ip
as the local-exec
provisioner, since it must connect to the same host.
Hello Jbardin, thanks for your response. We have a client that we’re trying to use terraform to provision some servers and using ansible to deploy software on those servers. I’ve google and tried many google Terraform and Ansible examples unsuccessfully. I have the terraform provisioning the aws EC2 instance and etc. However I hadn’t be able to get Terraform and Ansible to work together. Based on your recommendation, I change the private_ip change to public_ip (see modified code below). I received this error:
Begining of Error
Error: remote-exec provisioner error
with null_resource.example,
on main.tf line 15, in resource “null_resource” “example”:
15: provisioner “remote-exec” {
timeout -last error: dial tcp #.##.###.##:22: i/o timeout
Ending of Error
BTW the instance appears in the AWS console and I’m able to logon via putty.
Begin private ip to public ip change
resource “null_resource” “example” {
provisioner “remote-exec” {
connection {
type = “ssh”
user = “centos”
private_key = file(“Test-Key-1.pem”)
host = aws_instance.example.public_ip
file = file(“files/id_rsa”)
}
#END private ip to public ip change
What’s the best practice to use Terraform and Ansible?
It’s hard to say what the problem might be here, the timeout could be that the host failed to come up in time, ingress from the source network is not allowed, etc. Calling ssh
from local-exec
may help troubleshoot the issue, where you can add other connection options or verbosity to the output.
The prescribed best practice for Terraform is to not use provisioners when possible. The linked documentation goes into more details, and provides examples of other methods to bootstrap the configuration on a system.