jtopjian / ansible-terraform-inventory

Ansible Dynamic Inventory with Terraform
11 stars 7 forks source link

Inventory generator is not working. #4

Open jeka opened 5 years ago

jeka commented 5 years ago

Hello, at last, I have little time to play a bit more with this generator. First, sorry for the vague description, but I have no idea how to describe it better.

So, I have a case where nbering's python version of inventory generator works and this one fails. python version:

$ ../../terraform.py --list                    
{
  "all": {
    "hosts": [
      "master-01",
      "worker-01"
    ],
    "vars": {},
    "children": []
  },
  "_meta": {
    "hostvars": {
      "master-01": {
        "ansible_host": "10.164.136.39",
        "ansible_user": "centos"
      },
      "worker-01": {
        "ansible_host": "10.164.133.190",
        "ansible_user": "centos"
      }
    }
  },
  "bootstrap": {
    "hosts": [
      "master-01",
      "worker-01"
    ],
    "vars": {},
    "children": []
  },
  "k8s-master": {
    "hosts": [
      "master-01"
    ],
    "vars": {},
    "children": []
  },
  "k8s-worker": {
    "hosts": [
      "worker-01"
    ],
    "vars": {},
    "children": []
  }
}

Go version:

$ ../../ansible-terraform-inventory-linux-amd64 --list
{"_meta":{"hostvars":{}}}

I suspect that data source 'local_file' is confusing your generator. I added a line

if err != nil {
        return nil, fmt.Errorf("Error unmarshaling state: %s\n", err)
    }
+   fmt.Printf("%+v\n", s)
    return &s, nil
}

and the resulting output is:

$ /home/jekov/go/bin/terraform-inventory --list
{Modules:[{Resources:map[]} {Resources:map[openstack_compute_floatingip_associate_v2.fip:{Type:openstack_compute_floatingip_associate_v2 Primary:{ID:10.164.136.39/1e821802-0e32-4929-bb22-60b415295198/192.168.0.5 Attributes:map[instance_id:1e821802-0e32-4929-bb22-60b415295198 region:RegionOne fixed_ip:192.168.0.5 floating_ip:10.164.136.39 id:10.164.136.39/1e821802-0e32-4929-bb22-60b415295198/192.168.0.5]}} openstack_compute_instance_v2.this:{Type:openstack_compute_instance_v2 Primary:{ID:1e821802-0e32-4929-bb22-60b415295198 Attributes:map[all_metadata.ssh_user:centos block_device.0.source_type:image network.0.port: block_device.0.boot_index:0 block_device.0.destination_type:volume flavor_id:7145dc32-6709-4c0f-992f-1ca4e38ceb0f power_state:active access_ip_v4:192.168.0.5 id:1e821802-0e32-4929-bb22-60b415295198 network.0.floating_ip: region:RegionOne network.0.uuid:57cba9da-bebf-4583-bed6-2e490e0b3cbd access_ip_v6: all_metadata.%:1 block_device.0.delete_on_termination:true metadata.%:1 name:master-01 network.0.name:internal security_groups.3814588639:default stop_before_destroy:false block_device.0.device_type: block_device.0.guest_format: metadata.ssh_user:centos network.0.fixed_ip_v6: network.0.mac:fa:16:3e:3d:19:d2 security_groups.#:1 block_device.#:1 image_id:Attempt to boot from volume - no image supplied network.0.access_network:false block_device.0.disk_bus: block_device.0.uuid:a15d834c-50a1-42a3-8f43-788e8e5ed54c block_device.0.volume_size:20 flavor_name:aa.002-0006 force_delete:false network.0.fixed_ip_v4:192.168.0.5 availability_zone:es-si-os-dhn-80_1 key_pair:deployment network.#:1]}} openstack_compute_volume_attach_v2.extra_volume:{Type:openstack_compute_volume_attach_v2 Primary:{ID:1e821802-0e32-4929-bb22-60b415295198/fdf58ca6-6b47-4f31-97bb-2e3a7a45c28d Attributes:map[device:/dev/vdb id:1e821802-0e32-4929-bb22-60b415295198/fdf58ca6-6b47-4f31-97bb-2e3a7a45c28d instance_id:1e821802-0e32-4929-bb22-60b415295198 region:RegionOne volume_id:fdf58ca6-6b47-4f31-97bb-2e3a7a45c28d]}} openstack_networking_floatingip_v2.fip:{Type:openstack_networking_floatingip_v2 Primary:{ID:24e4d91c-be6c-4871-87c3-985501a88088 Attributes:map[address:10.164.136.39 description: fixed_ip:192.168.0.5 id:24e4d91c-be6c-4871-87c3-985501a88088 tags.#:0 tenant_id:68bb3d65b5e043a3a57d73e9a011c897 all_tags.#:0 pool:datacentre port_id:570ec4d9-b289-4d2c-96f1-a1ebef87fb6d region:RegionOne]}} ansible_host.host:{Type:ansible_host Primary:{ID:master-01 Attributes:map[inventory_hostname:master-01 vars.%:2 vars.ansible_host:10.164.136.39 vars.ansible_user:centos groups.#:2 groups.0:bootstrap groups.1:k8s-master id:master-01]}} data.local_file.ssh_priv_key:{Type:local_file Primary:{ID:b18a9471580e405d1b8809fb5d6d6f53f8481983 Attributes:map[content:-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAvrbyOJ538U2cfpCk/p8sIT68R0bEfdmbkLLxGhH9k6K3AORC
6xpjqdEZC+9m1wy1iPj5TTlVaHH2I8TL+4Jiakn10WjiTBkaByAkJro9fzzbGRaI
[ --- cut --- ]
ubzyd7lE7/iqkJlZbxFDPW+FPYpYKORydk7y9YLQzsXdYGJbx8pb/ctf41Iqlw7T
Cq7waOgmiCEihlks3GflSqa9h6h1m0OVJ0lUSm0y7DWQim0HC2tWFBY=
-----END RSA PRIVATE KEY-----
 filename:/home/jekov/.ssh/deployment.key id:b18a9471580e405d1b8809fb5d6d6f53f8481983]}} openstack_blockstorage_volume_v3.extra_volume:{Type:openstack_blockstorage_volume_v3 Primary:{ID:fdf58ca6-6b47-4f31-97bb-2e3a7a45c28d Attributes:map[attachment.#:1 metadata.readonly:False source_vol_id: volume_type: metadata.%:2 name:extra-vol-01 region:RegionOne size:10 snapshot_id: attachment.3725217.id:fdf58ca6-6b47-4f31-97bb-2e3a7a45c28d availability_zone:nova id:fdf58ca6-6b47-4f31-97bb-2e3a7a45c28d metadata.attached_mode:rw attachment.3725217.device:/dev/vdb attachment.3725217.instance_id:1e821802-0e32-4929-bb22-60b415295198 description: enable_online_resize:true]}}]} {Resources:map[openstack_networking_floatingip_v2.fip:{Type:openstack_networking_floatingip_v2 Primary:{ID:e968825c-2b7f-4348-8445-88f6d68c9f67 Attributes:map[address:10.164.133.190 description: region:RegionOne tags.#:0 tenant_id:68bb3d65b5e043a3a57d73e9a011c897 all_tags.#:0 fixed_ip:192.168.0.12 id:e968825c-2b7f-4348-8445-88f6d68c9f67 pool:datacentre port_id:db4968b3-c953-4d00-a936-32ce08f0588d]}} ansible_host.host:{Type:ansible_host Primary:{ID:worker-01 Attributes:map[groups.#:2 groups.0:bootstrap groups.1:k8s-worker id:worker-01 inventory_hostname:worker-01 vars.%:2 vars.ansible_host:10.164.133.190 vars.ansible_user:centos]}} data.local_file.ssh_priv_key:{Type:local_file Primary:{ID:b18a9471580e405d1b8809fb5d6d6f53f8481983 Attributes:map[content:-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAvrbyOJ538U2cfpCk/p8sIT68R0bEfdmbkLLxGhH9k6K3AORC
6xpjqdEZC+9m1wy1iPj5TTlVaHH2I8TL+4Jiakn10WjiTBkaByAkJro9fzzbGRaI
[ --- cut --- ]
ubzyd7lE7/iqkJlZbxFDPW+FPYpYKORydk7y9YLQzsXdYGJbx8pb/ctf41Iqlw7T
Cq7waOgmiCEihlks3GflSqa9h6h1m0OVJ0lUSm0y7DWQim0HC2tWFBY=
-----END RSA PRIVATE KEY-----
 filename:/home/jekov/.ssh/deployment.key id:b18a9471580e405d1b8809fb5d6d6f53f8481983]}} openstack_blockstorage_volume_v3.extra_volume:{Type:openstack_blockstorage_volume_v3 Primary:{ID:1c3c992c-9b1f-4a7a-a0ed-f47ae5b26734 Attributes:map[description: enable_online_resize:true snapshot_id: volume_type: attachment.3586984480.instance_id:4a061d06-2ac1-418b-843e-ebfe4ec581b1 metadata.attached_mode:rw name:extra-vol-01 region:RegionOne source_vol_id: attachment.#:1 attachment.3586984480.device:/dev/vdb id:1c3c992c-9b1f-4a7a-a0ed-f47ae5b26734 metadata.%:2 metadata.readonly:False attachment.3586984480.id:1c3c992c-9b1f-4a7a-a0ed-f47ae5b26734 availability_zone:nova size:10]}} openstack_compute_floatingip_associate_v2.fip:{Type:openstack_compute_floatingip_associate_v2 Primary:{ID:10.164.133.190/4a061d06-2ac1-418b-843e-ebfe4ec581b1/192.168.0.12 Attributes:map[region:RegionOne fixed_ip:192.168.0.12 floating_ip:10.164.133.190 id:10.164.133.190/4a061d06-2ac1-418b-843e-ebfe4ec581b1/192.168.0.12 instance_id:4a061d06-2ac1-418b-843e-ebfe4ec581b1]}} openstack_compute_instance_v2.this:{Type:openstack_compute_instance_v2 Primary:{ID:4a061d06-2ac1-418b-843e-ebfe4ec581b1 Attributes:map[force_delete:false key_pair:deployment block_device.0.guest_format: network.0.mac:fa:16:3e:36:0f:cf region:RegionOne access_ip_v4:192.168.0.12 block_device.0.boot_index:0 block_device.0.disk_bus: block_device.0.source_type:image network.0.fixed_ip_v4:192.168.0.12 network.0.floating_ip: network.0.port: power_state:active access_ip_v6: block_device.#:1 metadata.ssh_user:centos network.0.access_network:false block_device.0.delete_on_termination:true id:4a061d06-2ac1-418b-843e-ebfe4ec581b1 stop_before_destroy:false all_metadata.ssh_user:centos flavor_id:a9a30c92-20ca-40fb-946c-6c58023170a9 block_device.0.uuid:a15d834c-50a1-42a3-8f43-788e8e5ed54c metadata.%:1 network.#:1 network.0.uuid:57cba9da-bebf-4583-bed6-2e490e0b3cbd all_metadata.%:1 availability_zone:es-si-os-dhn-80_1 flavor_name:fa.004-0016 block_device.0.volume_size:20 image_id:Attempt to boot from volume - no image supplied name:worker-01 network.0.fixed_ip_v6: network.0.name:internal security_groups.#:1 block_device.0.destination_type:volume block_device.0.device_type: security_groups.3814588639:default]}} openstack_compute_volume_attach_v2.extra_volume:{Type:openstack_compute_volume_attach_v2 Primary:{ID:4a061d06-2ac1-418b-843e-ebfe4ec581b1/1c3c992c-9b1f-4a7a-a0ed-f47ae5b26734 Attributes:map[id:4a061d06-2ac1-418b-843e-ebfe4ec581b1/1c3c992c-9b1f-4a7a-a0ed-f47ae5b26734 instance_id:4a061d06-2ac1-418b-843e-ebfe4ec581b1 region:RegionOne volume_id:1c3c992c-9b1f-4a7a-a0ed-f47ae5b26734 device:/dev/vdb]}}]}]}
{"_meta":{"hostvars":{}}}

I can provide more information if needed.

jtopjian commented 5 years ago

@jeka I'm so sorry for the late reply. I finally had some time to look into this. I added a local_file data source to the test fixtures, but that didn't recreate the problem you're seeing, so it must be something else.

Would it be possible to get a sanitized version of your entire terraform.tfstate file?

jtopjian commented 5 years ago

@jeka Actually, I might have found the cause: It appears that you are not explicitly declaring any ansible_group resources?

You have ansible_host resources which declare groups and I suppose that we could generate groups based on this implicit group membership.

jeka commented 5 years ago

@jtopjian You are right, I am not declaring ansible_group resources. As this is a part of an 'instance' module and I am assigning groups on invocation, i. e.:

resource "ansible_host" "host" {
  inventory_hostname = "${openstack_compute_instance_v2.this.*.name[count.index]}"
  groups             = "${var.ansible_groups}"

  vars {
    ansible_user = "${var.ssh_user}"
    ansible_host = "${openstack_networking_floatingip_v2.fip.*.address[count.index]}"
  }
}
jtopjian commented 5 years ago

@jeka Thanks for the info and for reporting this. I think this is now fixed and you can try it out in the latest release (https://github.com/jtopjian/ansible-terraform-inventory/releases/tag/v0.2.0). Let me know if you're still having problems, though.