radekg / terraform-provisioner-ansible

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

Interpolation not working in extra_vars #51

Closed taylorsmithgg closed 6 years ago

taylorsmithgg commented 6 years ago

I cannot currently use interpolation to inject values from terraform into extra_vars. The provisioner currently throws a false error when attempting.

  provisioner "ansible" {
    connection {
      type = "ssh"
      host = "${aws_instance.master.private_ip}"
      user = "ec2-user"
    }

    plays {
      playbook = "${var.ansible_path}/playbooks/greenplum/server.yaml"
      extra_vars {
        variable = "${aws_instance.master.private_ip}"
      }
    }

    hosts = ["${aws_instance.master.private_ip}"]
    local  = "no"
    verbose = "yes"
    use_sudo = "yes"
    skip_install = "no"
    skip_cleanup = "yes"
  }

Error

Warning: module.name.null_resource.ansible-master: nothing to play

Error: module.name.null_resource.ansible-master: file ${var.ansible_path}/playbooks/application/server.yaml does not exist
taylorsmithgg commented 6 years ago

This also doesn't work for "${var.variable}"

radekg commented 6 years ago

Definitely something to look at. I’ll try over the weekend.

radekg commented 6 years ago

With a tf file like this:

provider "aws" {
  region  = "eu-central-1"
  profile = "rad-admin"
}

## -- security groups:

resource "aws_security_group" "ssh" {
  name        = "ssh_access"
  description = "SSH access"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    self        = true
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

## -- machine:

resource "aws_instance" "bastion" {
  ami           = "ami-04d38f2458b0bb8df"
  count         = "1"
  instance_type = "t2.medium"

  security_groups = ["${aws_security_group.ssh.name}"]

  connection {
    user = "centos"
  }

  root_block_device {
    delete_on_termination = true
    volume_size           = 8
    volume_type           = "gp2"
  }
}

resource "aws_instance" "test_box" {
  ami           = "ami-04d38f2458b0bb8df"
  count         = "1"
  instance_type = "m3.medium"

  connection {
    user         = "centos"
    bastion_host = "${aws_instance.bastion.public_ip}"
  }

  security_groups = ["${aws_security_group.ssh.name}"]

  provisioner "ansible" {
    plays {
      playbook = "${path.module}/ansible/install-nginx.yml"
      extra_vars {
        variable = "${aws_security_group.ssh.name}"
      }
    }

    become = "yes"
    local  = "yes"
  }

  root_block_device {
    delete_on_termination = true
    volume_size           = 8
    volume_type           = "gp2"
  }
}

Using commit https://github.com/radekg/terraform-provisioner-ansible/commit/a846a5bf4b63e1115bb73a375eb41e5af5ed8235, I get the following result:

aws_instance.test_box (ansible): running local command: ANSIBLE_FORCE_COLOR=true ansible-playbook /Users/rad/dev/my/terraform-ansible-test/ansible/install-nginx.yml --inventory-file='/var/folders/1v/dzstxkjx10q76ky0c5qxp9br0000gn/T/temporary-ansible-inventory511737685' --become --become-method='sudo' --become-user='root' --extra-vars='{"variable":"ssh_access"}' --forks=5 --user='centos' --ssh-extra-args='-p 22 -o UserKnownHostsFile=/var/folders/1v/dzstxkjx10q76ky0c5qxp9br0000gn/T/21cbaeba-0ff4-4907-aade-25131dadb614 -o ConnectTimeout=60 -o ConnectionAttempts=10 -o ProxyCommand="ssh -p 22 -W %h:%p centos@18.184.192.227" -o ForwardAgent=yes'
aws_instance.test_box (ansible): Executing: ["/bin/sh" "-c" "ANSIBLE_FORCE_COLOR=true ansible-playbook /Users/rad/dev/my/terraform-ansible-test/ansible/install-nginx.yml --inventory-file='/var/folders/1v/dzstxkjx10q76ky0c5qxp9br0000gn/T/temporary-ansible-inventory511737685' --become --become-method='sudo' --become-user='root' --extra-vars='{\"variable\":\"ssh_access\"}' --forks=5 --user='centos' --ssh-extra-args='-p 22 -o UserKnownHostsFile=/var/folders/1v/dzstxkjx10q76ky0c5qxp9br0000gn/T/21cbaeba-0ff4-4907-aade-25131dadb614 -o ConnectTimeout=60 -o ConnectionAttempts=10 -o ProxyCommand=\"ssh -p 22 -W %h:%p centos@18.184.192.227\" -o ForwardAgent=yes'"]

This would indicate that the interpolation in extra_vars is working (with Terraform v0.11.8 as a dependency).

I'm going to close this issue, please report again, if it turns out that it actually does not work.