Keith Adler
Keith Adler

Reputation: 21178

How can I start all AWS EC2 instances in Ansible

I have found a script for starting/stopping a dynamically created ec2 instance, but how do I start any instances in my inventory?

Upvotes: 0

Views: 515

Answers (2)

Arbab Nazar
Arbab Nazar

Reputation: 23811

BMW, give you an excellent startup, but you can even summarise the thing like this: 1) First get the id of all the instances and save them into a file

aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' --region us-east-1 --output text >> id.txt

2) Then simply run this command to start all the instances

for id in $(awk '{print $1}' id.txt); do echo "starting the following instance $id"; aws ec2 start-instances --instance-ids --region us-east-1 $id; done

Please change the region, I am considering that you have installed and setup the AWS CLI tools properly. Thanks

Upvotes: 0

BMW
BMW

Reputation: 45343

Seems you are talking about scripting, not SDK. So there are two tools to do the job.

1 AWS CLI tools

  1. download aws cli tool and set the API Key in $HOME/.aws/credentials

  2. list all instances on region us-east-1

Confirm which instances you are targeting.

aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' --region us-east-1 --output text

2 Amazon EC2 Command Line Interface Tools

  1. download and setup instruction

  2. list all instances on region us-east-1

You should get same output as WAY #1.

ec2-describe-instances --region us-west-2 |awk '/INSTANCE/{print $2}'

With the instance ID list, you can use your command to start them one by one.

for example, the instance name are saved in file instance.list

while read instance
do
  echo "Starting instance $instance ..."
  ec2-start-instances "$linstance"
done  < instance.list

Upvotes: 1

Related Questions