Skip to content

Instantly share code, notes, and snippets.

@kiranchavala
Created September 27, 2021 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiranchavala/c63243b0b76adcdafc7d06d87a304370 to your computer and use it in GitHub Desktop.
Save kiranchavala/c63243b0b76adcdafc7d06d87a304370 to your computer and use it in GitHub Desktop.
Importing Openstack resources to terraform
1. On linux using brew package manager instatll terraform and terraformer
https://brew.sh/
https://docs.brew.sh/Homebrew-on-Linux
1 sudo yum -y install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.9-1.x86_64.rpm
2 sudo yum install git
3 rpm -Uvh http://www.city-fan.org/ftp/contrib/yum-repo/rhel7/x86_64/city-fan.org-release-2-1.rhel7.noarch.rpm
4 sudo rpm -Uvh http://www.city-fan.org/ftp/contrib/yum-repo/rhel7/x86_64/city-fan.org-release-2-1.rhel7.noarch.rpm
5 yum-config-manager --disable city-fan.org
6 sudo yum-config-manager --disable city-fan.org
7 sudo yum --enablerepo=city-fan.org install libcurl libcurl-devel
8 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
9 test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
10 test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
11 test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
12 echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
13 brew install hashicorp/tap/terraform
14 brew install terraform
15 brew install terraformer
2. To install Openstack environement
launch a vm with the following specifications and follow
CentOS 8.3 with 8GB RAM , 2 vCPUs and 40GB disk space
https://gist.github.com/kiranchavala/7d8e8e67b72a8c60e3d113e416e5d0b6
3. Create a provider.tf file with the following content
mkdir -p test
cd test
vi provider.tf
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "1.43.0"
}
}
}
provider "openstack" {
# Configuration options
}
terraform init
6. To import all the existing resources from your opne stack
Download the rc from openstack
https://docs.openstack.org/ocata/user-guide/common/cli-set-environment-variables-using-openstack-rc.html
export OS_DOMAIN_NAME=Default
terraformer import openstack --resources=compute,networking --regions=RegionOne
tree
.
├── generated
│   └── openstack
│   ├── compute
│   │   └── RegionOne
│   │   ├── compute_instance_v2.tf
│   │   ├── outputs.tf
│   │   ├── provider.tf
│   │   └── terraform.tfstate
│   └── networking
│   └── RegionOne
│   ├── networking_secgroup_rule_v2.tf
│   ├── networking_secgroup_v2.tf
│   ├── outputs.tf
│   ├── provider.tf
│   └── terraform.tfstate
├── provider.tf
└── terraform.tfstate
7. Now we can control the exisitng resources
cd genrated/openstack/compute/RegionOne
[kiran@acp415finaleap RegionOne]$ terraform state replace-provider -auto-approve "openstack" "terraform-provider-openstack/openstack/"
│ Error: Invalid "to" provider "registry.terraform.io/terraform-provider-openstack/openstack/"
│ Invalid provider source string: The "source" attribute must be in the format "[hostname/][namespace/]name"
[root@acp415finaleap RegionOne]# terraform state replace-provider -auto-approve "openstack" "hashicorp/terraform-provider-openstack/terraform-provider-openstack"
│ Error: Invalid "to" provider "hashicorp/terraform-provider-openstack/terraform-provider-openstack"
│ Invalid provider type: Provider source "hashicorp/terraform-provider-openstack/terraform-provider-openstack" has a type with the prefix "terraform-provider-", which isn't valid. Although that prefix is often used in the names of
│ version control repositories for Terraform providers, provider source strings should not include it.
As a workaround
Rename the exisitng the terraform.tfstate to backup
Change the provider.tf to change the source
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "1.43.0"
}
}
}
provider "openstack" {
# Configuration options
}
Then perform
terraform init
terraform refresh
8. To change the setting of a resource after editing the respective resource tf file
terraform plan
terraform apply
ref
https://brew.sh/
https://docs.brew.sh/Homebrew-on-Linux
https://github.com/GoogleCloudPlatform/terraformer
https://learn.hashicorp.com/tutorials/terraform/state-import?in=terraform/state&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS#limitations-and-other-considerations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment