Ash*_*faq 3 python amazon-web-services python-2.7 autoscaling boto3
我试图获取自动缩放组,其应用程序标记值为"CCC".
清单如下,
gweb
prd-dcc-eap-w2
gweb
prd-dcc-emc
gweb
prd-dcc-ems
CCC
dev-ccc-wer
CCC
dev-ccc-gbg
CCC
dev-ccc-wer
Run Code Online (Sandbox Code Playgroud)
我在下面编写的脚本给出的输出包括一个没有CCC标签的ASG.
#!/usr/bin/python
import boto3
client = boto3.client('autoscaling',region_name='us-west-2')
response = client.describe_auto_scaling_groups()
ccc_asg = []
all_asg = response['AutoScalingGroups']
for i in range(len(all_asg)):
all_tags = all_asg[i]['Tags']
for j in range(len(all_tags)):
if all_tags[j]['Key'] == 'Name':
asg_name = all_tags[j]['Value']
# print asg_name
if all_tags[j]['Key'] == 'Application':
app = all_tags[j]['Value']
# print app
if all_tags[j]['Value'] == 'CCC':
ccc_asg.append(asg_name)
print ccc_asg
Run Code Online (Sandbox Code Playgroud)
我得到的输出如下,
['prd-dcc-ein-w2', 'dev-ccc-hap', 'dev-ccc-wfd', 'dev-ccc-sdf']
Run Code Online (Sandbox Code Playgroud)
as 'prd-dcc-ein-w2'具有不同标记的asg 'gweb'.并且(dev-ccc-msp-agt-asg)缺少CCC ASG列表中的最后一个.我需要输出如下,
dev-ccc-hap-sdf
dev-ccc-hap-gfh
dev-ccc-hap-tyu
dev-ccc-mso-hjk
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
在boto3中,您可以使用带有JMESPath过滤的Paginators,以更加简洁的方式非常有效地执行此操作.
来自boto3文档:
JMESPath是JSON的查询语言,可以直接用于分页结果.您可以使用通过PageIterator的搜索方法应用于每个结果页面的JMESPath表达式来过滤客户端的结果.
使用JMESPath表达式进行过滤时,由paginator生成的每个结果页面都通过JMESPath表达式进行映射.如果JMESPath表达式返回的单个值不是数组,则直接生成该值.如果将JMESPath表达式应用于结果页面的结果是列表,则单独生成列表的每个值(实际上是实现平面映射).
以下是在Python代码中看起来如何为Auto Scaling Group的标记提供的CCP值Application:
import boto3
client = boto3.client('autoscaling')
paginator = client.get_paginator('describe_auto_scaling_groups')
page_iterator = paginator.paginate(
PaginationConfig={'PageSize': 100}
)
filtered_asgs = page_iterator.search(
'AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format(
'Application', 'CCP')
)
for asg in filtered_asgs:
print asg['AutoScalingGroupName']
Run Code Online (Sandbox Code Playgroud)
详细说明 Michal Gasek 的回答,这里有一个选项,它根据 tag:value 对的字典过滤 ASG。
def get_asg_name_from_tags(tags):
asg_name = None
client = boto3.client('autoscaling')
while True:
paginator = client.get_paginator('describe_auto_scaling_groups')
page_iterator = paginator.paginate(
PaginationConfig={'PageSize': 100}
)
filter = 'AutoScalingGroups[]'
for tag in tags:
filter = ('{} | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format(filter, tag, tags[tag]))
filtered_asgs = page_iterator.search(filter)
asg = filtered_asgs.next()
asg_name = asg['AutoScalingGroupName']
try:
asgX = filtered_asgs.next()
asgX_name = asg['AutoScalingGroupName']
raise AssertionError('multiple ASG\'s found for {} = {},{}'
.format(tags, asg_name, asgX_name))
except StopIteration:
break
return asg_name
Run Code Online (Sandbox Code Playgroud)
例如:
asg_name = get_asg_name_from_tags({'Env':env, 'Application':'app'})
Run Code Online (Sandbox Code Playgroud)
它期望只有一个结果,并通过尝试使用 next() 获得另一个结果来检查这一点。StopIteration 是“好”的情况,然后跳出分页器循环。
我用下面的脚本让它工作。
#!/usr/bin/python
import boto3
client = boto3.client('autoscaling',region_name='us-west-2')
response = client.describe_auto_scaling_groups()
ccp_asg = []
all_asg = response['AutoScalingGroups']
for i in range(len(all_asg)):
all_tags = all_asg[i]['Tags']
app = False
asg_name = ''
for j in range(len(all_tags)):
if 'Application' in all_tags[j]['Key'] and all_tags[j]['Value'] in ('CCP'):
app = True
if app:
if 'Name' in all_tags[j]['Key']:
asg_name = all_tags[j]['Value']
ccp_asg.append(asg_name)
print ccp_asg
Run Code Online (Sandbox Code Playgroud)
如果您有任何疑问,请随时询问。
| 归档时间: |
|
| 查看次数: |
7080 次 |
| 最近记录: |