ansible / terraform-provider-ansible

community terraform provider for ansible
https://registry.terraform.io/providers/ansible/ansible/latest
GNU General Public License v3.0
183 stars 42 forks source link

Empty Inventory when using terraform-provider-ansible with Terraform Modules #52

Closed moritzxbe closed 3 months ago

moritzxbe commented 11 months ago

I am encountering issues with the terraform-provider-ansible plugin when deploying VMs using Terraform Modules. The inventory appears to be empty when I run the command ansible-inventory -i inventory.yml --graph --vars, showing the output as below:

@all: |--@ungrouped:

However, when I create VMs directly without using Terraform Modules, the inventory works correctly and shows the VMs.

Is there a way to make the terraform-provider-ansible plugin work with Terraform Modules?

alleje02 commented 10 months ago

I am also experiencing this same bug.

EdanBrooke commented 8 months ago

I believe I'm seeing this issue too. I'm trying to use a remote Terraform state (GitLab-backed), how about you @moritzxbe @alleje02 ?

Verbose command output for ansible-inventory -i inventory.yml --graph --vars -vvv -

ansible-inventory [core 2.15.5]
  config file = /home/edan/.ansible.cfg
  configured module search path = ['/home/edan/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/edan/.local/pipx/venvs/ansible/lib/python3.10/site-packages/ansible
  ansible collection location = /home/edan/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/edan/.local/bin/ansible-inventory
  python version = 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] (/home/edan/.local/pipx/venvs/ansible/bin/python)
  jinja version = 3.1.2
  libyaml = True
Using /home/edan/.ansible.cfg as config file
host_list declined parsing /home/edan/projects/iac/infrastructure/terraform/inventory.yml as it did not pass its verify_file() method
script declined parsing /home/edan/projects/iac/infrastructure/terraform/inventory.yml as it did not pass its verify_file() method
Using inventory plugin 'ansible_collections.cloud.terraform.plugins.inventory.terraform_provider' to process inventory source '/home/edan/projects/iac/infrastructure/terraform/inventory.yml'
Parsed /home/edan/projects/iac/infrastructure/terraform/inventory.yml inventory source with auto plugin
@all:
  |--@ungrouped:

My inventory.yml looks like this:

---
plugin: cloud.terraform.terraform_provider
state_file: "" # Use remote Terraform state by specifying empty string

Thanks.

grebois commented 8 months ago

@moritzxbe @alleje02 @EdanBrooke an option to use modules was merged in main but not yet release;

Here is a workaround:

requirements.yml

---
collections:
  - name: git@github.com:ansible-collections/cloud.terraform.git
    type: git
    version: main

inventory.yml

---
plugin: cloud.terraform.terraform_provider
search_child_modules: True
mikeruu commented 8 months ago

@grebois I'm having trouble using the workaround. Using the requirements file seems to install the 1.1.0 release and not the version from main and still get the empty inventory. Do you know what I might be missing? TIA

% ansible-galaxy install -r requirements.yaml
Starting galaxy collection install process
Process install dependency map
Cloning into '/Users/migu/.ansible/tmp/ansible-local-77382nn3s3x07/tmpyweio5a1/cloud.terraformbix8z7ks'...
remote: Enumerating objects: 1676, done.
remote: Counting objects: 100% (326/326), done.
remote: Compressing objects: 100% (154/154), done.
remote: Total 1676 (delta 238), reused 179 (delta 171), pack-reused 1350
Receiving objects: 100% (1676/1676), 344.34 KiB | 1.54 MiB/s, done.
Resolving deltas: 100% (623/623), done.
Already on 'main'
Your branch is up to date with 'origin/main'.
Starting collection install process
Installing 'cloud.terraform:1.1.0' to '/Users/migu/.ansible/collections/ansible_collections/cloud/terraform'
Created collection for cloud.terraform:1.1.0 at /Users/migu/.ansible/collections/ansible_collections/cloud/terraform
cloud.terraform:1.1.0 was installed successfully

requirements.yaml

collections:
  - name: git@github.com:ansible-collections/cloud.terraform.git
    type: git
    version: main

I even tried specifying the branch with this other option and it ends up installing 1.1.0

ansible-galaxy collection install git@github.com:ansible-collections/cloud.terraform.git,main
Cloning into '/Users/migu/.ansible/tmp/ansible-local-774295xt__j73/tmpe8jwwuk9/cloud.terraform0hsps5b9'...
remote: Enumerating objects: 1676, done.
remote: Counting objects: 100% (308/308), done.
remote: Compressing objects: 100% (139/139), done.
remote: Total 1676 (delta 225), reused 176 (delta 168), pack-reused 1368
Receiving objects: 100% (1676/1676), 341.00 KiB | 2.58 MiB/s, done.
Resolving deltas: 100% (626/626), done.
Already on 'main'
Your branch is up to date with 'origin/main'.
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'cloud.terraform:1.1.0' to '/Users/migu/.ansible/collections/ansible_collections/cloud/terraform'
Created collection for cloud.terraform:1.1.0 at /Users/migu/.ansible/collections/ansible_collections/cloud/terraform
cloud.terraform:1.1.0 was installed successfully
EdanBrooke commented 8 months ago

@grebois I'm having trouble using the workaround. Using the requirements file seems to install the 1.1.0 release and not the version from main and still get the empty inventory. Do you know what I might be missing? TIA

% ansible-galaxy install -r requirements.yaml
Starting galaxy collection install process
Process install dependency map
Cloning into '/Users/migu/.ansible/tmp/ansible-local-77382nn3s3x07/tmpyweio5a1/cloud.terraformbix8z7ks'...
remote: Enumerating objects: 1676, done.
remote: Counting objects: 100% (326/326), done.
remote: Compressing objects: 100% (154/154), done.
remote: Total 1676 (delta 238), reused 179 (delta 171), pack-reused 1350
Receiving objects: 100% (1676/1676), 344.34 KiB | 1.54 MiB/s, done.
Resolving deltas: 100% (623/623), done.
Already on 'main'
Your branch is up to date with 'origin/main'.
Starting collection install process
Installing 'cloud.terraform:1.1.0' to '/Users/migu/.ansible/collections/ansible_collections/cloud/terraform'
Created collection for cloud.terraform:1.1.0 at /Users/migu/.ansible/collections/ansible_collections/cloud/terraform
cloud.terraform:1.1.0 was installed successfully

requirements.yaml

collections:
  - name: git@github.com:ansible-collections/cloud.terraform.git
    type: git
    version: main

I even tried specifying the branch with this other option and it ends up installing 1.1.0

ansible-galaxy collection install git@github.com:ansible-collections/cloud.terraform.git,main
Cloning into '/Users/migu/.ansible/tmp/ansible-local-774295xt__j73/tmpe8jwwuk9/cloud.terraform0hsps5b9'...
remote: Enumerating objects: 1676, done.
remote: Counting objects: 100% (308/308), done.
remote: Compressing objects: 100% (139/139), done.
remote: Total 1676 (delta 225), reused 176 (delta 168), pack-reused 1368
Receiving objects: 100% (1676/1676), 341.00 KiB | 2.58 MiB/s, done.
Resolving deltas: 100% (626/626), done.
Already on 'main'
Your branch is up to date with 'origin/main'.
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'cloud.terraform:1.1.0' to '/Users/migu/.ansible/collections/ansible_collections/cloud/terraform'
Created collection for cloud.terraform:1.1.0 at /Users/migu/.ansible/collections/ansible_collections/cloud/terraform
cloud.terraform:1.1.0 was installed successfully

Hi,

It worked for me even though it shows it installed 1.1.0 successfully. I think it just reads the most recent tag from the time the merge commit was done with the improved Terraform options available, which happens to be 1.1.0.

I did end up using the HTTPS path to the repo because I've not got my work laptop's SSH key on my GitHub account yet. ansible-galaxy collection install git+https://github.com/ansible-collections/cloud.terraform.git,main

gravesm commented 3 months ago

Closing as this should no longer be an issue with the 2.0.0 version of cloud.terraform.