nbering / terraform-inventory

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

Issue with attributes for ansible group #15

Closed designermonkey closed 5 years ago

designermonkey commented 5 years ago

I have the following being reported in my terminal:

ANSIBLE_TF_WS_NAME=staging ./terraform-inventory.py 
Traceback (most recent call last):
  File "./terraform-inventory.py", line 400, in _main
    inventory.add_resource(resource)
  File "./terraform-inventory.py", line 243, in add_resource
    self.add_group_resource(resource)
  File "./terraform-inventory.py", line 216, in add_group_resource
    groupname = resource.read_attr("inventory_group_name")
  File "./terraform-inventory.py", line 184, in read_attr
    return self._raw_attributes().get(key, None)
  File "./terraform-inventory.py", line 189, in _raw_attributes
    return self.source_json["attributes"]
KeyError: 'attributes'

This is using a 'remote' terraform backend, Terraform v0.12.0. The state says it is version 4. Here's an example of the state:

{
  "version": 4,
  "terraform_version": "0.12.0",
  "serial": 2,
  "lineage": "1a478ae2-6647-8ebd-0815-46b948b7b22e",
  "outputs": {
    "infra_vpn_static_ip": {
      "value": "139.59.203.99",
      "type": "string"
    }
  },
  "resources": [
    {
      "mode": "managed",
      "type": "ansible_group",
      "name": "database",
      "provider": "provider.ansible",
      "instances": [
        {
          "schema_version": 0,
          "attributes_flat": {
            "id": "database",
            "inventory_group_name": "database"
          }
        }
      ]
    }
}

I can see 'attributes_flat' but not 'attributes'. I've tried changing a value here and there to see if it worked, but I'm not a python dev so I can't see how the data is structured at line 189 to see what the key should be.

I'll keep digging though.

designermonkey commented 5 years ago

Line 189 needs to be: return self.source_json["attributes_flat"]

This raises the next problem that none of the groups get hosts added to them in the final output. I will keep trying to track this down but I fear I am out of my depth.

designermonkey commented 5 years ago

Ok then, adding the following host in to the example data:

    {
      "mode": "managed",
      "type": "ansible_host",
      "name": "infra_vpn",
      "provider": "provider.ansible",
      "instances": [
        {
          "schema_version": 0,
          "attributes_flat": {
            "groups.#": "3",
            "groups.0": "staging",
            "groups.1": "public",
            "groups.2": "http",
            "id": "68.183.43.62",
            "inventory_hostname": "68.183.43.62",
            "vars.%": "1",
            "vars.do_hostname": "vpn001-lon1-stg"
          },
          "depends_on": [
            "ansible_group.http",
            "ansible_group.public",
            "ansible_group.staging",
            "digitalocean_droplet.infra_vpn"
          ]
        }
      ]
    }

Now the release for v0.12.0 of terraform is out, it seems they have decided to use flat variables. This means the current check is not sufficient to distinguish between .11 and .12 releases.

I've forked the repo and will attempt a PR.

nbering commented 5 years ago

🤦‍♂

They really reverted? I'll take a look at this. The test cases would need to be updated to ensure this works as intended.

nbering commented 5 years ago

On a high note, I had no idea what to call the difference in the way attributes were formatted. It's kind of flattering to see that they called it basically the same thing.

designermonkey commented 5 years ago

I see what you did there 'flat'tering lol

designermonkey commented 5 years ago

Also, I don't know how the test cases work, so I won't be offended if you don't accept this and alter it's implementation; it works for my case for now.

nbering commented 5 years ago

I opened issue #17 to cover that test documentation deficiency. Thanks for pointing that out.

designermonkey commented 5 years ago

I'm really confused today. I haven't changed my setup, and rerun my inventory, and today Terraform has attributes not attributes_flat.

Very confused.

nbering commented 5 years ago

That's interesting. Perhaps it's something about the provider version?

designermonkey commented 5 years ago

Yeah, I think maybe the state file got confused somewhere along the line and partially applied newer content and not all of it.

I'll close this. Thanks for your input.

sdemura commented 5 years ago

Seeing this as well. Terraform 0.12.7 using s3 remote state.

nbering commented 5 years ago

@sdemura Does the state contents look like the sample provided in the opening comment?