Skip to main content

Command Palette

Search for a command to run...

Dynamic AWS Inventory in Ansible

Updated
1 min read
S

I have been working at the rapid changing software development field over 19 years as a software engineer, architect, devops. I am an expert in designing and implementing scalable architectures to deal with large transactions and massive requests on e-commerce and messaging services. I am skillful at using opensource technologies like Node.js, Nginx, Redis, Cassandra, ELK and Kafka to secure site reliability. I also have proficiency in C#, Java, Javascript, Python and Go.

Ansible gives powerful ways to manage a lot of EC2 instances and Cloud infrastructures with YAML. This snippet would be helpful to gather host informations such as private_ip_address even instances are added by EC2 autoscaling in AWS.

# inventory.aws_ec2.yml

plugin: aws_ec2
regions:
  - ap-northeast-1
filters:
  instance-state-name: running
hostnames:
  - tag:Name
groups:
  # dev
  dev : "'-dev-' in tags.Name"
  # qa 
  qa : "'-qa-' in tags.Name"
  # prod 
  prod : "'-prod-' in tags.Name"
keyed_groups:
  - key: tags['aws:autoscaling:groupName']
    prefix: ''
compose:
  # Use the private IP address to connect to the host
  # (note: this does not modify inventory_hostname, which is set via I(hostnames))
  ansible_host: private_ip_address
# invoke command 'df' to all instances in dev group
ansible -i inventory.aws_ec2.yml dev -a 'df'