Itai Ganot
Itai Ganot

Reputation: 6305

How to run the following command on 3 different regions?

I wrote the following Groovy code:

#!/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
}

The output looks like so:

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

The output of this code is being added to an Extended Parameter in Jenkins and displays a list of CIDR blocks in use.

Currently, the command only returns output for the default region which is us-west-2.

I would like to run this code and collect all CIDR blocks in use from all regions (in my case it's Oregon [us-west-2], Ireland [eu-west-1] and Virginia [us-east-1]).

How can it be done?

Upvotes: 0

Views: 96

Answers (1)

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3016

For all available regions:

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
        }

Upvotes: 1

Related Questions