Terraform - 如何定义要 运行 的模块并使用变量创建
Terraform - How to define which modules to run and create using variables
我有创建和运行几个模块的 terraform 代码——VPC、网络、ansible 和 Jenkins。
我需要根据真值或假值创建 Ansible 和 Jenkins 模块的选项。
当我试图注释掉 Jenkins 模块时,我需要删除所有相关变量,这是一个混乱。我需要从根文件夹(变量、tfvars 和输出文件)和网络模块中注释掉变量,因为我正在传递 Jenkins 安全组 ID。
有没有一种聪明的方法可以在不编辑文件的情况下选择哪些模块应该被排除在外,包括它们的变量?
根文件夹 - Main.tf
module "network" {
source = "./modules/network"
vpc_id = module.vpc.vpcid
number_of_public_subnets = 2
number_of_private_subnets = 2
public-subnet-block = var.public-subnet-block
private-subnet-block = var.private-subnet-block
availability_zone = var.availability_zone
gateway_id = module.vpc.gwid
tag_enviroment= var.tag_enviroment
project_name = var.project_name
**jenkins_server_target_group_arn = module.jenkins.jenkins-server-target-group-arn**
}
module "ansible-server"{
source = "./modules/ansible-server"
ami_id = "ami-04505e74c0741db8d"
instance_type = var.ansible_server_instance-type
availability_zone = var.availability_zone[0]
subnet_id = module.network.public-subnet-id[0]
tag_enviroment= var.tag_enviroment
project_name = var.project_name
vpc_id = module.vpc.vpcid
key_name = aws_key_pair.mid_project_key.key_name
private_key_file_name = var.private_key_file_name
iam_instance_profile = aws_iam_instance_profile.ec2-role.name
depends_on = [local_file.mid_project_key]
}
module "jenkins"{
source = "./modules/jenkins"
ami_id = "ami-0e472ba40eb589f49"
jenkins_nodes_number_of_server = 2
jenkins-server-instance-type = var.jenkins-server-instance-type
jenkins-node-instance-type = var.jenkins-node-instance-type
private_subnet_id = module.network.private-subnet-id
tag_enviroment= var.tag_enviroment
project_name = var.project_name
vpc_id = module.vpc.vpcid
key_name = aws_key_pair.mid_project_key.key_name
private_key_file_name = var.private_key_file_name
iam_instance_profile = aws_iam_instance_profile.ec2-role.name
alb1_security_group_id = module.network.alb1-security-group-id
}
根文件夹 - Main.tf variables.tf
variable "ansible_server_instance-type" {
type = string
default = "t2.micro"
}
variable "private-subnet-block" {
type = list(string)
}
variable "public-subnet-block" {
type = list(string)
}
variable "availability_zone" {}
variable "private_key_file_name" {}
variable "consul-instance-type" {}
variable "jenkins-node-instance-type" {}
variable "jenkins-server-instance-type" {}
根文件夹 - terraform.tfvars
jenkins-node-instance-type = "t2.micro"
jenkins-server-instance-type = "t2.micro"
模块文件夹 -> main.tf
resource "aws_alb_listener" "jenkins" {
load_balancer_arn = aws_alb.alb1.arn
port = "9000"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = var.jenkins_server_target_group_arn
}
}
模块文件夹 -> varibles.tf
variable "jenkins_server_target_group_arn" {}
count
meta argument can help you here. Just create 0 or 1 instances of the module depending on the outcome of a conditional expression.
variable "create_ansible_server {
type = bool
}
...
module "ansible-server"{
source = "./modules/ansible-server"
count = var.create_ansible_server ? 1 : 0
...
}
我有创建和运行几个模块的 terraform 代码——VPC、网络、ansible 和 Jenkins。 我需要根据真值或假值创建 Ansible 和 Jenkins 模块的选项。
当我试图注释掉 Jenkins 模块时,我需要删除所有相关变量,这是一个混乱。我需要从根文件夹(变量、tfvars 和输出文件)和网络模块中注释掉变量,因为我正在传递 Jenkins 安全组 ID。
有没有一种聪明的方法可以在不编辑文件的情况下选择哪些模块应该被排除在外,包括它们的变量?
根文件夹 - Main.tf
module "network" {
source = "./modules/network"
vpc_id = module.vpc.vpcid
number_of_public_subnets = 2
number_of_private_subnets = 2
public-subnet-block = var.public-subnet-block
private-subnet-block = var.private-subnet-block
availability_zone = var.availability_zone
gateway_id = module.vpc.gwid
tag_enviroment= var.tag_enviroment
project_name = var.project_name
**jenkins_server_target_group_arn = module.jenkins.jenkins-server-target-group-arn**
}
module "ansible-server"{
source = "./modules/ansible-server"
ami_id = "ami-04505e74c0741db8d"
instance_type = var.ansible_server_instance-type
availability_zone = var.availability_zone[0]
subnet_id = module.network.public-subnet-id[0]
tag_enviroment= var.tag_enviroment
project_name = var.project_name
vpc_id = module.vpc.vpcid
key_name = aws_key_pair.mid_project_key.key_name
private_key_file_name = var.private_key_file_name
iam_instance_profile = aws_iam_instance_profile.ec2-role.name
depends_on = [local_file.mid_project_key]
}
module "jenkins"{
source = "./modules/jenkins"
ami_id = "ami-0e472ba40eb589f49"
jenkins_nodes_number_of_server = 2
jenkins-server-instance-type = var.jenkins-server-instance-type
jenkins-node-instance-type = var.jenkins-node-instance-type
private_subnet_id = module.network.private-subnet-id
tag_enviroment= var.tag_enviroment
project_name = var.project_name
vpc_id = module.vpc.vpcid
key_name = aws_key_pair.mid_project_key.key_name
private_key_file_name = var.private_key_file_name
iam_instance_profile = aws_iam_instance_profile.ec2-role.name
alb1_security_group_id = module.network.alb1-security-group-id
}
根文件夹 - Main.tf variables.tf
variable "ansible_server_instance-type" {
type = string
default = "t2.micro"
}
variable "private-subnet-block" {
type = list(string)
}
variable "public-subnet-block" {
type = list(string)
}
variable "availability_zone" {}
variable "private_key_file_name" {}
variable "consul-instance-type" {}
variable "jenkins-node-instance-type" {}
variable "jenkins-server-instance-type" {}
根文件夹 - terraform.tfvars
jenkins-node-instance-type = "t2.micro"
jenkins-server-instance-type = "t2.micro"
模块文件夹 -> main.tf
resource "aws_alb_listener" "jenkins" {
load_balancer_arn = aws_alb.alb1.arn
port = "9000"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = var.jenkins_server_target_group_arn
}
}
模块文件夹 -> varibles.tf
variable "jenkins_server_target_group_arn" {}
count
meta argument can help you here. Just create 0 or 1 instances of the module depending on the outcome of a conditional expression.
variable "create_ansible_server {
type = bool
}
...
module "ansible-server"{
source = "./modules/ansible-server"
count = var.create_ansible_server ? 1 : 0
...
}