radekg / terraform-provisioner-ansible

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

Google Cloud Compute with Ubuntu image throws "[[: not found" when trying to run tf-ansible-installer #130

Closed donovanmuller closed 5 years ago

donovanmuller commented 5 years ago

Steps to reproduce

Using GCP Compute Instance and the Ubuntu 18.04 LTS image I get the following error when trying to install Ansible on the remote bastion:

google_compute_instance.vm_instance: Still creating... [4m0s elapsed]
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
...

If I run the script manually (SSH into the bastion) with (see https://unix.stackexchange.com/a/155881/131307):

$ bash tf-ansible-installer
Hit:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]

...

Installing collected packages: MarkupSafe, jinja2, PyYAML, asn1crypto, enum34, ipaddress, pycparser, cffi, six, cryptography, ansible
Successfully installed MarkupSafe-1.1.1 PyYAML-5.1.2 ansible-2.8.3 asn1crypto-0.24.0 cffi-1.12.3 cryptography-2.7 enum34-1.1.6 ipaddress-1.0.22 jinja2-2.10.1 pycparser-2.19 six-1.12.0

Ansible is installed correctly.

Expected behavior

The tf-ansible-installer script runs successfully and installs Ansible on the remote bastion host.

Actual behavior

google_compute_instance.vm_instance: Still creating... [4m0s elapsed]
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found

Configuration

Terraform version:

Terraform v0.12.5
+ provider.google v2.11.0

terraform-provisioner-ansible version/SHA:

2.3.0

Terraform file / provisioner configuration:

provider "google" {
  version = "2.11"
  project = "xxx"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
  name                    = "terraform-network"
  auto_create_subnetworks = "true"
}

resource "google_compute_firewall" "default" {
  name    = "terraform-allow-ssh"
  network = "${google_compute_network.vpc_network.name}"

  allow {
    protocol = "icmp"
  }

  allow {
    protocol = "tcp"
    ports    = ["22"]
  }
}

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "ubuntu-1804-lts"
    }
  }

  network_interface {
    network = "${google_compute_network.vpc_network.self_link}"
    access_config {
    }
  }

  metadata = {
    ssh-keys = "root:${file("${var.public_key_path}")}"
  }

  connection {
    host = "${google_compute_instance.vm_instance.network_interface.0.access_config.0.nat_ip}"
    type        = "ssh"
    agent       = false
    user        = "root"
    private_key = "${file("${var.private_key_path}")}"
  }  

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

Terraform run log:

google_compute_instance.vm_instance (ansible): Connecting to remote host via SSH...
google_compute_instance.vm_instance (ansible):   Host: xxx
google_compute_instance.vm_instance (ansible):   User: root
google_compute_instance.vm_instance (ansible):   Password: false
google_compute_instance.vm_instance (ansible):   Private key: true
google_compute_instance.vm_instance (ansible):   Certificate: false
google_compute_instance.vm_instance (ansible):   SSH Agent: false
google_compute_instance.vm_instance (ansible):   Checking Host Key: false
google_compute_instance.vm_instance: Still creating... [1m10s elapsed]
google_compute_instance.vm_instance (ansible): Connected!
google_compute_instance.vm_instance (ansible): Uploading the parent directory 'ansible/playbooks' of playbook './ansible/playbooks/install-tree.yml' to '/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4'...
google_compute_instance.vm_instance (ansible): Generating temporary ansible inventory...
google_compute_instance.vm_instance (ansible): Writing temporary ansible inventory to '/tmp/tf-ansible-bootstrap/bf543a3a00e33421aa9400f11f4734d4/.inventory-a8b2b419-28b9-4110-b2e1-9879a2cb2b22'...
google_compute_instance.vm_instance (ansible): Ansible inventory written.
google_compute_instance.vm_instance (ansible): Uploading roles path 'ansible/roles' to '/tmp/tf-ansible-bootstrap/28d7b170d0b1854bed049bde1275641a'...
google_compute_instance.vm_instance (ansible): Installing Ansible 'ansible' using default installer...
google_compute_instance.vm_instance: Still creating... [1m20s elapsed]
google_compute_instance.vm_instance (ansible): Uploading Ansible installer program to '/tmp/tf-ansible-installer'...
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
google_compute_instance.vm_instance (ansible): /tmp/tf-ansible-installer: 6: /tmp/tf-ansible-installer: [[: not found
donovanmuller commented 5 years ago

Seems a duplicate of #107, specifically this https://github.com/radekg/terraform-provisioner-ansible/pull/107#discussion_r257520163

radekg commented 5 years ago

Thank you for reporting this, I will fix that and release a new version. Went unnoticed because it was snug in a more complex pr. Sorry for that!

radekg commented 5 years ago

In the meantime, it’s possible to supply a custom installer program using remote.local_installer_path.

radekg commented 5 years ago

Will be released soon.

donovanmuller commented 5 years ago

@radekg Any chance we can get a release soon? Thanks

radekg commented 4 years ago

I am really sorry. It will be released this week.

donovanmuller commented 4 years ago

@radekg I'm still getting this error with 2.3.1, specifically with the Ubuntu 18.04 LTS image on GCP.

radekg commented 4 years ago

Hi @donovanmuller, thank you for reporting. I believe not all places were fixed in the code. My fault, totally. I've added the following commit: https://github.com/radekg/terraform-provisioner-ansible/commit/9f97685693e8b1294ed12b1e18c01a309f1d1c1f

The way I've tested this is, I have created the following program locally on disk:

#!/bin/sh

EXPECTED_ANSIBLE_VERSION=${EXPECTED_ANSIBLE_VERSION-"ansible==1.2.2"}
if [ -n "${SIMULATE_ANSIBLE_INSTALLED}" ]; then
    touch /bin/ansible-playbook
    chmod +x /bin/ansible-playbook
fi

if [ -n "${TAF_DOCKER_CENTOS_LATEST_RUN}" ]; then
  yum update -y && yum install -y which
fi
set -eu
if [ -z "$(which ansible-playbook)" ]; then
  # only check the cloud boot finished if the directory exists
  if [ -d /var/lib/cloud/instance ]; then
    while [ -f /var/lib/cloud/instance/boot-finished ]; do
      sleep 1
    done
  fi
  # install dependencies
  if [ -f /etc/redhat-release ]; then
    yum update -y \
    && yum groupinstall -y "Development Tools" \
    && yum install -y python-devel
  else
    echo "apt-get update && apt-get install -y build-essential python-dev"
  fi
  # install pip, if necessary
  if [ -z "$(which pip)" ]; then
    echo "curl https://bootstrap.pypa.io/get-pip.py | python"
  fi
  # install ansible
  # pip install {{ .AnsibleVersion}}
  echo "should install ${EXPECTED_ANSIBLE_VERSION}"
else
  expected_version="${EXPECTED_ANSIBLE_VERSION}"
  installed_version="1.2.2"
  installed_version="ansible==$installed_version"
  case "$expected_version" in
    *==*)
      if [ "$expected_version" != "$installed_version" ]; then
        echo "pip install $expected_version"
      fi
    ;;
  esac
fi

This very closely matches the installer program from the commit referenced above. I then executed these docker commands:

[rad] script-test $ docker run -ti --rm -v $(pwd):/usr/scripts ubuntu:18.04 /bin/sh -c '/usr/scripts/program.sh'
apt-get update && apt-get install -y build-essential python-dev
curl https://bootstrap.pypa.io/get-pip.py | python
should install ansible==1.2.2
[rad] script-test $ docker run -ti --rm -v $(pwd):/usr/scripts -e SIMULATE_ANSIBLE_INSTALLED=1 ubuntu:18.04 /bin/sh -c '/usr/scripts/program.sh'
[rad] script-test $
[rad] script-test $ docker run -ti --rm -v $(pwd):/usr/scripts -e SIMULATE_ANSIBLE_INSTALLED=1 -e EXPECTED_ANSIBLE_VERSION=ansible==1.2.3 ubuntu:18.04 /bin/sh -c '/usr/scripts/program.sh'
pip install ansible==1.2.3

It seems to be correct now.

donovanmuller commented 4 years ago

Thanks for the speedy resolution πŸ‘

radekg commented 4 years ago

I will cut a release within a few hours.

crazy2k commented 4 years ago

I think I'm hitting the same bug. Waiting for the release :)

radekg commented 4 years ago

Version 2.3.2 is out. Please let me know if this finally solves the problem.

donovanmuller commented 4 years ago

@radekg Will do as soon as I can. Thanks for the release πŸ‘