![从 Jenkins 批量删除 ec2 快照](https://linux22.com/image/747828/%E4%BB%8E%20Jenkins%20%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4%20ec2%20%E5%BF%AB%E7%85%A7.png)
用例:
将 txt 文件 (old_snapshots.txt) 中的快照列表导出到 S3 bucket。从 Jenkins,使用 aws cp 命令将文件复制到 Jenkins /tmp/directory
--dry-run did not show any error
但是,当在 Jenkins 中传递带有 aws delete 命令的以下 bash 行时,它显示成功,同时表示发生了错误并且快照未被删除。
## actual deletion
file="/tmp/old_snapshots.txt"
while read delete_data
do
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
echo "Deleting snapshot $delete_data"
done <"$file"
输出
An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-xxxx81xxxxxx7fxxx
An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-xxxacc49xxxxxxc26
An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-04xxxxxxxx3fa3cxxxxxxf4
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
要求:这个错误An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation
似乎太笼统了?有人遇到过类似的问题吗?此外,我还手动测试了使用 aws cli 删除单个快照,方法是在 Jenkins 中手动添加快照 ID,效果很好。如有任何建议,我将不胜感激。
包括 aws 命令
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id snap-01x1618xxxxxxa51x
Found snapshotid: snap-01x1618xxxxxxa51x in the uploaded file: /tmp/old_snapshots.txt
Now deleting snapshot id snap-01x1618xxxxxxa51x
答案1
您需要稍微修改一下您的脚本。
$ sh abc.sh
What is in delete_data snap-xxxx81xxxxxx7fxxx
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-xxxx81xxxxxx7fxxx
What is in delete_data snap-xxxacc49xxxxxxc26
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-xxxacc49xxxxxxc26
What is in delete_data snap-04xxxxxxxx3fa3cxxxxxxf4
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-04xxxxxxxx3fa3cxxxxxxf4
$ cat abc.sh
## actual deletion
file="/tmp/old_snapshots.txt"
while read delete_data
do
#aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
echo "What is in delete_data $delete_data"
echo "What is in file $file"
echo "Deleting snapshot $delete_data"
done < $file
您的脚本调用的file
变量是常量,但您需要逐行传递文件内容。因此,请替换以下内容
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
和
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $delete_data
答案2
为了其他社区用户的利益,我使用该--debug
标志来发现'\r'
正在向折断-#
调试输出
2020-01-28 18:09:02,295 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.ec2.delete-snapshot.snapshot-id: calling handler <awscli.paramfile.URIArgumentHandler object at >
2020-01-28 18:09:02,296 - MainThread - botocore.hooks - DEBUG - Event process-cli-arg.ec2.delete-snapshot: calling handler <awscli.argprocess.ParamShorthandParser object at >
2020-01-28 18:09:02,296 - MainThread - awscli.arguments - DEBUG - Unpacked value of u'snap-0xxxxxxxxxxxxxxxx\r' for parameter "snapshot_id": u'snap-0xxxxxxxxxxxxxxxx\r'
为了解决这个问题:我传递tr -d '\r'
给保存值的变量,以进行对话。
例子:
tr -d '\r' < input > output
更新脚本
file="/tmp/old_snapshots.txt"
cat $file | tr -d '\r' | while read -r line;
do
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $line
done