0

I'm running this command for getting the public IP of an EC2. It works as expected!

aws ec2 describe-instances --filters "Name=tag:Name,Values=EC2" --query 'Reservations[*].Instances[*].PublicIpAddress' --output text

I'm trying to use this value for running this command, so instead of XX.XX.XX.XX it has to be the value I get from ec2 describe-instances ansible-playbook provisioning/site.yml -i inventory --ssh-common-args '-o "proxycommand ssh -W %h:%p -i Key.pem [email protected]"'

So I have my commands like this

IP=aws ec2 describe-instances --filters "Name=tag:Name,Values=$NAME_PREFIX*" --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
export BASTION="ubuntu@${IP}"
ansible-playbook provisioning/site.yml -i inventory --ssh-common-args '-o "proxycommand ssh -W %h:%p -i Key.pem ${BASTION}"'

So not sure, how should I use that output, if you could help me please

1
  • Use: IP=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$NAME_PREFIX*" --query 'Reservations[*].Instances[*].PublicIpAddress' --output text) Commented Oct 15, 2019 at 21:40

1 Answer 1

1

It is better to add "Name=instance-state-name,Values=running" clause in filters to make sure you get a running instance only so your aws command would be:

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values=$NAME_PREFIX*" --query 'Reservations[*].Instances[*].PublicIpAddress' --output text

Now to store output IP in a variable, you can use command substitution:

ip=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values=$NAME_PREFIX*" --query 'Reservations[*].Instances[*].PublicIpAddress' --output text)

Then to use it:

export BASTION="ubuntu@$ip"
ansible-playbook provisioning/site.yml -i inventory --ssh-common-args '-o "proxycommand ssh -W %h:%p -i Key.pem '"$BASTION"'"'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.