[英]targeting all resources exept one in terraform(the opposite of "-target" parameter)
我至少有 20 個資源由我的 terraform 配置管理,我想部署除一個之外的所有資源,我知道我可以添加“-target”參數,但我必須指定所有資源的名稱(名稱至少為 20資源)以“-target”為目標對我來說這是一個問題,因為我經常在我的 terraform 配置中添加/刪除資源,所以我需要是否有像“-target”相反的解決方案來指定一個資源應用地形配置時被忽略。
PS:我不想刪除我的資源配置被忽略
目前沒有在 terraform 中執行此操作的選項。
這是我用作解決方法的bash
腳本,您可能會感興趣:
terraform-apply-exclude.bash
#!/usr/bin/env bash
WORKFILE=terraform-apply-exclude-cmd.bash
RESOURCE_TO_IGNORE="$1"
reset_workfile() {
if [ -d $WORKFILE ]; then
rm $WORKFILE
fi
echo "#!/usr/bin/env bash" > $WORKFILE
echo "terraform apply \\" >> $WORKFILE
}
write_resources_to_apply_in_workfile() {
local RESOURCES_TO_APPLY="$1"
# Formatting to ensure the cmd will be runnable:
# - Single quote around all resource paths
# - Escape double quotes if any inside the squarebrakets
echo -e "${RESOURCES_TO_APPLY//\"/\\\"}" | xargs -I {} echo " -target '{}' \\" >> $WORKFILE
}
# Extract all the resources that would be updated, created or destroyed
RESOURCES_TO_APPLY=$(terraform plan -no-color | grep --color=never " # " | grep -E --color=never 'will be updated|created|destroy|must be replaced' | awk -F' ' '{print $2}' )
reset_workfile
if [ -z "$RESOURCE_TO_IGNORE" ]; then
echo "If you want to ignore 1 resource, use the following syntax:"
echo "$0 \"terraform_path_of_the_resource_you_want_to_ignore\"."
echo "No resource to ignore. Still putting result in '$WORKFILE'."
echo "If you want to exclude more than 1 resource, you can manually edit the file '$WORKFILE' and then run it."
write_resources_to_apply_in_workfile "$RESOURCES_TO_APPLY"
exit 1;
fi
RESOURCE_TO_IGNORE=${RESOURCE_TO_IGNORE//\"/\\\"}
RESOURCE_TO_IGNORE=${RESOURCE_TO_IGNORE//[/\\[} # ]] Fix syntaxing highlighting for code editor
RESOURCE_TO_IGNORE=${RESOURCE_TO_IGNORE//]/\\]}
echo "Resource to ignore: $RESOURCE_TO_IGNORE"
RESOURCES_TO_APPLY=$(echo "$RESOURCES_TO_APPLY" | grep --color=never -E -v "$RESOURCE_TO_IGNORE")
write_resources_to_apply_in_workfile "$RESOURCES_TO_APPLY"
使用時,請確保將要排除的資源路徑中的"
轉義。
它應該是這樣的:
# Basic example, with no exclusion up-front (the easiest way, but a bit more manual)
$ bash terraform-apply-exclude.bash
$ <edit the 'terraform-apply-exclude-cmd.bash' file and remove the resource to exclude>
$ bash terraform-apply-exclude-cmd.bash
# Simple resource path example
$ bash terraform-apply-exclude.bash "aws_s3_bucket.bucket"
$ bash terraform-apply-exclude-cmd.bash
# Example with a more complex setup, in this case, a module with a `for_each` meta-argument
$ bash terraform-apply-exclude.bash "module.buckets[\"vacation_pictures\"].aws_s3_bucket.bucket"
$ bash terraform-apply-exclude-cmd.bash
此外,如果您遇到錯誤,可能是因為我們沒有運行相同的bash
、 grep
和/或awk
版本。 這是我正在運行的版本:
GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)
grep (GNU grep) 3.8
目前Terraform中沒有特性或功能可讓您在計划或應用中排除資源,這與 -target 標志相反地形。
同時,最好的替代方法之一是使用開關控制您的資源。
resource "aws_instance" "example" {
count = var.exclude_ec2_instance ? 0 : 1
...
}
然后,您可以使用以下命令排除資源:
terraform plan -out=tfplan -var='exclude_ec2_instance=true'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.