nbering / terraform-inventory

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

Unable to parse #21

Open fred-gb opened 4 years ago

fred-gb commented 4 years ago

Hello, Terraform 0.12.17 Ansible 2.9.6

ansible-playbook -i /etc/ansible/terraform.py play/test.yml
 [WARNING]:  * Failed to parse /etc/ansible/terraform.py with script plugin: Inventory script (/etc/ansible/terraform.py) had an execution error: Traceback (most recent call last):
File "/etc/ansible/terraform.py", line 395, in _main     tfstate = TerraformState(_execute_shell())   File "/etc/ansible/terraform.py", line 390, in _execute_shell     return
json.loads(out_cmd, encoding=encoding)   File "/usr/lib/python3.6/json/__init__.py", line 354, in loads     return _default_decoder.decode(s)   File
"/usr/lib/python3.6/json/decoder.py", line 339, in decode     obj, end = self.raw_decode(s, idx=_w(s, 0).end())   File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

 [WARNING]:  * Failed to parse /etc/ansible/terraform.py with ini plugin: /etc/ansible/terraform.py:3: 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'

Already do chmod +x

Part of my tfstate:

      "mode": "managed",
      "type": "ansible_host",
      "name": "wordpress01",
      "provider": "provider.ansible",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "groups": [
              "wordpress"
            ],
            "id": "wordpress01",
            "inventory_hostname": "wordpress01",
            "variable_priority": 50,
            "vars": {
              "ansible_connection": "lxd"
            }
          }

Where is my error?

nbering commented 4 years ago

chmod +x would have been my first guess, but I see you've tried that.

For some reason, Ansible is trying to parse the script as an inventory file. That would seem to suggest that it wasn't able to execute it, despite the permissions setting you already tried.

Try executing the script yourself, ie ./terraform.py from Bash or other shell. It should spit out some Ansible inventory JSON. Or, if there's an error that might help you figure out what's wrong.

fred-gb commented 4 years ago

Hello, I found something about my mistake.

When: ansible -i inventories/betatest/terraform.py -m ping all I have the parse error. When: cd inventories/betatest/terraform.py and ansible -i inventories/betatest/terraform.py -m ping all

Tadaaaa:

mariadb-master | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
redis01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
wordpress01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
haproxy01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

It's works!

I need to be in the same directory.

So if I want to play a playbook, like: ansible-playbook -i inventories/betatest/terraform.py play/test.yml I have the parse error.

How I can do to use terraform.py with differents directories? Because I need to change inventory.

inventories/
`-- betatest
    |-- group_vars
    |   `-- all.yml
    |-- host_vars
    |   `-- host01.yml
    |-- terraform.py
    `-- terraform.tfstate
-- betatest1
    |-- group_vars
    |   `-- all.yml
    |-- host_vars
    |   `-- host01.yml
    |-- terraform.py
    `-- terraform.tfstate
-- betatest2
    |-- group_vars
    |   `-- all.yml
    |-- host_vars
    |   `-- host01.yml
    |-- terraform.py
    `-- terraform.tfstate

Thanks

nbering commented 4 years ago

Perhaps it's trying to run it, and falls back to reading the file when it fails. I can't recall if that's how it behaved when I was developing it.

I think what you want is to set the Terraform project path. There's an override for that as an environment variable. See the README for details. https://github.com/nbering/terraform-inventory#ansible_tf_dir

fred-gb commented 4 years ago

Thansk, that is! But how to fix configuration of terraform.py to not have to add each time ANSIBLE_TF_DIR=blablablablabla ansible -i terraform.py -m ping all Thanks

fred-gb commented 4 years ago

Finally, I change string in terraform.py TERRAFORM_DIR = os.environ.get('ANSIBLE_TF_DIR', '/full/path/to/workdir/') And it's works!

nbering commented 4 years ago

That'll work, assuming you don't need to update it. Personally, I use direnv to manage my project-specific environment variables.