nbering / terraform-provider-ansible

"Logical" provider for integrating with an Ansible Dynamic Inventory script.
https://nbering.github.io/terraform-provider-ansible/
Mozilla Public License 2.0
329 stars 64 forks source link

Problems upgrading to registry-sourced plugin #34

Closed bdelvecchio closed 4 years ago

bdelvecchio commented 4 years ago

[Edited] This summarizes the steps that resulted in a successful upgrade for me. But I had missed this important step: If tf init complains that it can't find hashicorp/ansible, that means one of your modules is using the ansible plugin (say, by declaring an ansible_group resource) but does not have a required_provider statement! Could be your root module or any included module.

_TLDR: This error means you need to add a required_providers block to all the modules which list hashicorp/ansible as a provider in the output of terraform providers._

[moved from #32]

When upgrading to Terraform 0.13.x, I'm having problems related to moving from the manually-installed hashicorp/ansible plugin to a registry-installed version:

First, I added the necessary required_providers block to my root module:

terraform {
  [...]
  required_providers {
    ansible = {
      source = "nbering/ansible"
      version = "1.0.4"
    }
    aws = {
      source = "hashicorp/aws"
    }
  }
}

Now when I run tf init --upgrade it still fails looking for the older name:

 $ tf init --upgrade
Upgrading modules...
- security_group in ../modules/app-tier/security-group
- swarm_master in ../modules/app-tier/instance
- vpc in ../modules/app-tier/vpc-remote

Initializing the backend...

Initializing provider plugins...
- Finding latest version of -/aws...
- Finding latest version of nbering/ansible...
- Finding latest version of hashicorp/aws...
- Finding latest version of hashicorp/ansible...
- Installing -/aws v3.5.0...
- Installed -/aws v3.5.0 (signed by HashiCorp)
- Installing nbering/ansible v1.0.4...
- Installed nbering/ansible v1.0.4 (self-signed, key ID 1A9FBF0FFD10BE33)
- Installing hashicorp/aws v3.5.0...
- Installed hashicorp/aws v3.5.0 (signed by HashiCorp)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html

Error: Failed to install provider

Error while installing hashicorp/ansible: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/ansible

Even after replacing the provider in my project state:

 $ tf state replace-provider registry.terraform.io/-/ansible registry.terraform.io/nbering/ansible
Terraform will perform the following actions:

  ~ Updating provider:
    - registry.terraform.io/-/ansible
    + registry.terraform.io/nbering/ansible

Changing 3 resources:

  ansible_host.monitor
  module.swarm_master.ansible_group.swarm_nodes
  module.swarm_master.ansible_host.swarm_master

Do you want to make these changes?
Only 'yes' will be accepted to continue.

Enter a value: yes

Successfully replaced provider for 3 resources.

It feels like the state still contains the reference to hashicorp/ansible even though no resources have been moved to the new provider. [EDIT] it's not the state that's holding the stale reference--it's strictly the code. Use terraform providers to identify the module with the dangling reference, and add a required_providers block to that module.

nbering commented 4 years ago

This issue suggests there might be a relationship to these errors and modules. They said it went away when they added the config reference to both the root module and the submodule. Might be worth a try if you haven't already?

https://github.com/hashicorp/terraform/issues/25995

bdelvecchio commented 4 years ago

Yes, that definitely looked related.
From that report I discovered this command, which helped me resolve the problem:

 $ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/aws]
├── provider[registry.terraform.io/nbering/ansible] 1.0.4
├── module.security_group
│   └── provider[registry.terraform.io/hashicorp/aws]
├── module.swarm_master
│   ├── provider[registry.terraform.io/hashicorp/aws]
│   └── provider[registry.terraform.io/hashicorp/ansible]
└── module.vpc
    └── provider[registry.terraform.io/hashicorp/aws]

Providers required by state:

    provider[registry.terraform.io/-/aws]

    provider[registry.terraform.io/-/ansible]

I stated that I had added the required_providers clause to the modules, but I obviously missed one!.

bdelvecchio commented 4 years ago

OK, thanks for your help @nbering! I updated the first post in this issue with the solution, in case anyone else stumbles upon this. I'll also close this issue, since I'm fully working now.

nbering commented 3 years ago

I'm glad you got it working!

rrajapaksh commented 2 years ago

Hi Guy's I am facing the same issue. How Can I fix this. I am using tf v1.2.4.. How can I solve this issue???

image