radekg / terraform-provisioner-ansible

Ansible with Terraform 0.14.x
Apache License 2.0
572 stars 100 forks source link

When using a remote bastion, if an Ansible play has run before but failed any future applys fail on the bastion #138

Closed donovanmuller closed 4 years ago

donovanmuller commented 4 years ago

Steps to reproduce

When using a remote bastion, if an Ansible play has been run before but it did not complete successfully on the target host, then any subsequent terraform apply will fail after Ansible connects with this error message on the bastion instance:

...
google_compute_instance.x[0] (ansible): Connected!
google_compute_instance.x[0] (ansible): Command '"/bin/sh -c 'if [ -d \"/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4\" ]; then exit 50; fi'"' exited with non-zero exit status: 50, reason Process exited with status 50

Error: Command '"/bin/sh -c 'if [ -d \"/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4\" ]; then exit 50; fi'"' exited with non-zero exit status: 50, reason Process exited with status 50

Deleting the directory /tmp/tf-ansible-bootstrap/ on the bastion is a workaround.

Expected behavior

Ansible play is re-run as it was the first time

Actual behavior

Fails after first failed apply with:

Error: Command '"/bin/sh -c 'if [ -d \"/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4\" ]; then exit 50; fi'"' exited with non-zero exit status: 50, reason Process exited with status 50

Configuration

Terraform version: Terraform v0.12.9

terraform-provisioner-ansible version/SHA: 2.3.1/1ea0e20

Terraform file / provisioner configuration:

provisioner "ansible" {
    plays {
        playbook {
            file_path = "${path.module}/ansible/playbooks/rke.yml"
            roles_path = [
                "${path.module}/ansible/roles"
            ]
        }
    }

    remote {}
}

Terraform run log:

...
google_compute_instance.x[0] (ansible): Connected!
google_compute_instance.x[0] (ansible): Command '"/bin/sh -c 'if [ -d \"/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4\" ]; then exit 50; fi'"' exited with non-zero exit status: 50, reason Process exited with status 50

Error: Command '"/bin/sh -c 'if [ -d \"/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4\" ]; then exit 50; fi'"' exited with non-zero exit status: 50, reason Process exited with status 50
radekg commented 4 years ago

Thank you for reporting this, @donovanmuller. It seems that the original test for the error code might have been too clever. It seems that the test did what it should have done, it failed with the expected code and it even said that. However, the code handling did not handle the magic value correctly.

140 should help.