【问题标题】:How to run the following command on 3 different regions?如何在 3 个不同的区域运行以下命令?
【发布时间】:2017-12-12 23:25:04
【问题描述】:

我编写了以下 Groovy 代码:

#!/usr/local/bin/groovy
def p = ['/usr/local/bin/aws', 'ec2', 'describe-vpcs'].execute() | 'grep -w CidrBlock'.execute() | ['awk', '{print $2}'].execute() | ['tr', '-d', '"\\"\\|,\\|\\{\\|\\\\["'].execute() | 'uniq'.execute()
p.waitFor()
def output = []
p.text.eachLine { line ->
        output << line
}

output.each {
        println it
}

输出如下:

172.31.0.0/16
172.60.0.0/16
172.54.0.0/16
172.59.0.0/16

此代码的输出被添加到 Jenkins 的扩展参数中,并显示正在使用的 CIDR 块列表。

目前,该命令仅返回默认区域 us-west-2 的输出。

我想运行此代码并收集所有地区正在使用的所有 CIDR 块(在我的例子中是俄勒冈 [us-west-2]、爱尔兰 [eu-west-1] 和弗吉尼亚 [us-east-1 ])。

怎么做?

【问题讨论】:

  • 您的意思是除了使用 --region xyz 选项运行 awscli 吗?
  • 是的,我需要在所有 3 个区域上运行该命令后加入该命令的结果。

标签: amazon-web-services jenkins groovy


【解决方案1】:

对于所有可用区域:

def regions = ['/usr/local/bin/aws', 'ec2', 'describe-regions', '--output', 'text'].execute() | 'cut -f3'.execute()
        regions.waitFor()
        def output = []
        regions.text.eachLine { region ->
            def p = ['/usr/local/bin/aws', 'ec2', 'describe-vpcs', '--region', region].execute() | 'grep -w CidrBlock'.execute() | ['awk', '{print $2}'].execute() | ['tr', '-d', '"\\"\\|,\\|\\{\\|\\\\["'].execute() | 'uniq'.execute()
            p.waitFor()
            p.text.eachLine { line ->
                output << line
            }
        }
        output.each {
            println it
        }

【讨论】:

    猜你喜欢
    • 2020-09-14
    • 2021-06-17
    • 1970-01-01
    • 2019-05-15
    • 2011-09-26
    • 2021-08-28
    • 2019-02-11
    • 1970-01-01
    • 2017-06-24
    相关资源
    最近更新 更多