jonmorehouse / terraform-provisioner-ansible

A provisioner for bootstrapping terraform resources with ansible
526 stars 68 forks source link

ansible-local.py does not set extra-vars #34

Open phumpal opened 7 years ago

phumpal commented 7 years ago

Problem

A play expects the Terraform Ansible provisioner to pass extra_vars but the play fails because the variable is undefined.

Steps to Reproduce

Terraform configuration snippet

    provisioner "ansible" {
      connection {
        user = "ubuntu"
      }
      playbook = "../ansible/bootstrap.yml"
      groups = ["all"]
      extra_vars = {
        "server_name" = "bastion"
      }
    }

Command run on instance

python ansible-local.py \
  --playbook=/tmp/ansible/bootstrap.yml \
  --hosts=all \
  --plays=bootstrap.yml \
  --groups=all \
  --extra-vars="{\"server_name\": \"bastion\"}"

In roles/common/tasks/main.yml

- debug: var="{{ server_name }}"

Output

TASK [common : debug] **********************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'server_name' is undefined\n\nThe error appears to have been in '/tmp/ansible/roles/common/tasks/main.yml': line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug: var=\"{{ server_name }}\"\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

Versions

$ python -V
Python 2.7.12
$ ansible --version
ansible 2.2.1.0
  config file = /tmp/ansible/ansible.cfg
  configured module search path = Default w/o overrides

Workaround

The non-idiomatic workaround appears to be replacing https://github.com/jonmorehouse/terraform-provisioner-ansible/blob/master/ansible-local.py#L113 with

variable_manager.extra_vars = args.extra_vars

Perhaps this is related or I am simply misunderstanding the issue.