0

I'm using AWSCLI at Windows 10 Professional with GitBash. I need to have a list of all running AWS instances by account. Will start doing in an account to make a list to populate the public keys from users.

Have based on these sites:

Add SSH Key to EC2 instances with Ansible – Automated

AWS documentation:

Filtering AWS

And have done:

aws ec2 describe-instances --filters Name=instance-state-name,Values=running

But the output from this command generate a lot of info. I tried to see the public IPs and generate a little less information, just to do a filtering test.

 $ aws ec2 describe-network-interfaces --query NetworkInterfaces[].Association.PublicIp
[
    "5.1.210.2",
    "5.9.236.6",
    "5.25.225.6",
    "3.2.254.5",
    "3.9.237.67",
    "4.5.183.15",
    "8.6.37.159",
    "5.0.128.119",
    "5.8.74.231",
    "4.2.37.138",
    "5.8.103.63",
    "4.1.17.9",
    "4.4.216.193",
    "4.9.147.45",
    "4.7.169.22"
]

For security purposes I did a change at some octecs. Becomes a better output and I can't figure out how to filter in a right way the 1st example.

My add-key.yml

[root@devops list]# cat add-key.yml
- name: "Playbook to Add Key to EC2 Instances"
  hosts: hosts_to_add_key
  vars:
    - status : "present"
    - key : "user_rsa_key.pub"

  tasks:

  - name: "Copy the authorized key file from"
    authorized_key:
      user: "{{ansible_user}}"
      state: "{{status}}"
      key: "{{ lookup('file', '{{ key }}')}}"

My hosts_to_add_key:

[root@devops list]# cat hosts_to_add_key
[hosts_to_add_key]
bacula ansible_host=3.2.1.43 ansible_user=centos
1.2.1.20 ansible_host=10.9.1.50 ansible_user=centos
devops ansible_host=localhost ansible_user=centos
docker ansible_host=52.87.108.170 ansible=ec2-user
jenkins ansible_host=52.7.6.214 ansible_user=ec2-user
jira ansible_host=54.175.104.102 ansible_user=ec2-user
[hosts_to_add_key:vars]
ansible_ssh_common_args="-o StrictHostKeyChecking=no"

Removed some hosts above to show few lines. If someone could point me out about describe-instances to get better results will be fine (by name, by VPC)

3
  • 1
    Do you have a list of the AWS Accounts? Do you have access credentials in each of the AWS Accounts? You would need to call describe_instances() in each account (and in each region if applicable). Commented Jul 11, 2021 at 7:18
  • Yes I have for the major ones. @JohnRotenstein
    – Marlon
    Commented Jul 12, 2021 at 12:13
  • I don't see why you would need to use SSH. If you have a list of AWS Accounts and a set of credentials for each account, you can write a script that loops through each account, them uses the credentials for that account to loop through each region and call describe_instances(). Commented Jul 12, 2021 at 22:29

1 Answer 1

1

You can achieve this by setting up aws cli:

  1. Download and setup AWS CLI

  2. Configure AWS i.e. aws configure (provide your account access key and secret key ID)

  3. run the following code which lists the running instances along with the Private IP

     aws ec2 describe-instances \
     --filter "Name=instance-state-name,Values=running" \
     --query "Reservations[*].Instances[*].[PrivateIpAddress, Tags[?Key=='Name'].Value|[0]]" \
     --output table > ec2_list.txt
    

This also outputs the table into the ec2_list.txt file which is good for sharing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .