nbering / terraform-inventory

An Ansible dynamic inventory script to pair with nbering/terraform-provider-ansible.
MIT License
175 stars 51 forks source link

workspaces not supported #23

Open Rafaelrico7 opened 3 years ago

Rafaelrico7 commented 3 years ago

Hi

I'm using the Gitlab Remote State Backend for my terraform state. While executing the terraform.py I get the following error:

$ ansible-playbook -i /etc/ansible/terraform.py ../ansible/playbook.yml [WARNING]: * Failed to parse /etc/ansible/terraform.py with script plugin: Inventory script (/etc/ansible/terraform.py) had an execution error: workspaces not supported [WARNING]: * Failed to parse /etc/ansible/terraform.py with ini plugin: /etc/ansible/terraform.py:2: Error parsing host definition ''''': No closing quotation [WARNING]: Unable to parse /etc/ansible/terraform.py as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

chmod +x was executed on the file and I also tried: ANSIBLE_TF_DIR=/path/to/terraform/workdir/ ansible -i terraform.py ...

what is my error?

nbering commented 3 years ago

Try running terraform.py by itself, without Ansible to see if you get the inventory output you expect as JSON. That might provide some meaningful next steps to look at.

I checked the Ansible changelog to see if there's breaking changes lately, but the docs claim continued compatibility with inventory scripts. Still... it might help to know what version of Ansible and Terraform you're using.

nbering commented 3 years ago

By the way... actual error you're getting says Ansible is trying to read your script file as a static ini-formatted inventory file. This might mean the permissions were not correct to execute the file, or perhaps it failed to execute and Ansible is falling back to parsing it as a static inventory.

mantiz commented 3 years ago

@Rafaelrico7 I had the same problem. It seems like gitlab does not (yet) support workspaces, so I removed these lines https://github.com/nbering/terraform-inventory/blob/master/terraform.py#L374-L381 and removed one level of indentation for these lines https://github.com/nbering/terraform-inventory/blob/master/terraform.py#L382-L390.

In addition, I had to remove the encoding parameter in this line https://github.com/nbering/terraform-inventory/blob/master/terraform.py#L390 because it was removed with python 3.9 (https://docs.python.org/3/library/json.html#json.loads).

With those modifications I was able to use this script, hope it helps.

@nbering Thanks for the script :bouquet:

acook8 commented 3 years ago

@nbering I had the same error as well. I'm running Terraform version 0.14.6 and Python 3.8.5. To fix it I changed tf_workspace = [TERRAFORM_PATH, 'workspace', 'select', TERRAFORM_WS_NAME] to tf_workspace = [TERRAFORM_PATH, 'select', TERRAFORM_WS_NAME] here: https://github.com/nbering/terraform-inventory/blob/1e8dfeeb92e77a1c73e42dddac1df38c9d3b8fac/terraform.py#L374

I wonder if there was an update to Terraform that caused the issue