radekg / terraform-provisioner-ansible

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

Ansible SSH settings ConnectionAttempts ignored when set #150

Open aedwa038 opened 4 years ago

aedwa038 commented 4 years ago

Steps to reproduce

Currently trying to use terraform-provisioner-ansible with GCP When creating vms in gcp using the provsioner would sometimes fail to ssh when a vm is not ready to be ssh'd into. Even if I try to set the connection attempts argument to a higher number. I still see ssh failures fairly quickly

It looks like ansible code based around this has changed a bit and its ignoring the argument ConnectionAttempts.

 --ssh-extra-args='-p 22 -o ConnectTimeout=60 -o ConnectionAttempts=20 -o StrictHostKeyChecking=no'" 

Upon further inspection ansible no longer takes the argument -o ConnectionAttempts=20 inside the ssh-extra-args

There is now seems to be something called retries https://github.com/ansible/ansible/blob/aa53eb0e7196c6c920e6326f5d11accfd077d151/lib/ansible/plugins/connection/ssh.py#L142

This can be worked around by setting the environment variable ANSIBLE_SSH_RETRIES before executing the ansible playbooks in terraform

Expected behavior

When setting Ansible SSH settings connection attempts I expect the provsioner/ansible to retry failed ssh connection attempts.
...

Actual behavior

When setting the connection attempts argument the terraform ansible provsioner is passing it in the ssh extra args as -o ConnectTimeout in the incorrect format. ...

Configuration

Terraform version:0.12.10

terraform-provisioner-ansible version/SHA:v2.3.3

Terraform file / provisioner configuration:

  resource "null_resource" "bas_box" {
      depends_on = ["google_compute_instance.bas_vm"]
      count = var.bas_count

      provisioner "ansible" {
        plays {
        playbook {
            file_path = "simplePlaybook.yml"
        }
         forks = 1
         enabled = true
         hosts = [ "${local.bas_hostnames[count.index]}"]

         verbose = true
        }
        ansible_ssh_settings {
          connect_timeout_seconds = 60
          connection_attempts = 20
          insecure_no_strict_host_key_checking = true
          insecure_bastion_no_strict_host_key_checking = true
        }
      }
    }

Terraform run log:

faruqisan commented 4 years ago

I have the same issue here, even I add tasks in the playbook to wait for ssh it still fails. but when I re-run terraform apply it succeed.

- name: Wait for port 22 to become open and contain "OpenSSH"
      wait_for:
        port: 22
        host: '{{ (ansible_ssh_host|default(ansible_host)|default(inventory_hostname) }}'
        search_regex: OpenSSH
        delay: 15
      connection: local