Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How to use dynamic inventories in Ansible automation

September 4, 2023
Deepankar Jain Himanshu Yadav
Related topics:
Automation and management
Related products:
Red Hat Ansible Automation PlatformRed Hat Ansible Automation Platform via AWS Marketplace

Share:

    Dynamic inventories in the Red Hat Ansible Automation Platform revolutionize the way to manage infrastructure in the cloud. Instead of relying on static inventories that require manual updates when virtual machines (VMs) are launched, terminated, or replaced, dynamic inventories automatically discover and run VMs across any cloud provider. This means that when you delete an old VM and launch a new one, Ansible Automation Platform seamlessly adapts and performs actions on the updated infrastructure without requiring manual intervention.

    By harnessing dynamic inventories, the Ansible Automation Platform empowers cloud administrators and DevOps teams to effortlessly manage and orchestrate on-demand cloud resources. This article explores the power of dynamic inventories, focusing on their utilization with AWS as the cloud provider. While applicable to any cloud provider, we showcase seamless AWS infrastructure management using dynamic inventories. We will demonstrate how to create EC2 instances, fetching details with dynamic inventory and running scripts for system health insights. By following these examples, you'll gain hands-on experience in effectively managing and monitoring your cloud infrastructure using dynamic inventories within the Ansible Automation Platform.

    Demo setup

    We will walk you through this hands-on example, creating three EC2 instances in AWS. We will then explore how to leverage dynamic inventory in the Ansible Automation Platform to automatically fetch the details of these instances. Using a playbook, we will execute tasks on these instances and observe the desired changes.

    We will also delete one of the EC2 instances and spin up a new one, showcasing the dynamic nature of inventories. By syncing the inventory in the Ansible Automation Platform, we will observe how it seamlessly adapts to the changes, enabling us to easily continue managing the updated infrastructure.

    Prerequisites

    1. You must have an active AWS account.
    2. Generate the access key and client secret for AWS.
    3. Navigate to the Credentials tab
    4. Under the Add button, select Amazon Web Services.
    5. Add your access key and secret key and save the credentials (Figure 1).
    A screenshot of the Ansible create AWS credentials page.
    Figure 1: The Ansible create credentials page.

    How to use dynamic inventory to manage AWS infrastructure

    1. Create three EC2 instances in the AWS console as follows:
    • Log in to your AWS console and navigate to the EC2 sections.
    • Under the EC2 console, click on Launch Instances and create three instances with different names for Ubuntu image.

    NOTE: Make sure to use a single private key for all three EC2 instances as shown in Figure 2.

    1. Create the credentials to SSH into the AWS machine as follows:
    • Navigate to the credentials tab.
    • Under the Add button, select machine.
    • Enter a name for the credential.
    • Under Username, enter “ubuntu” and enter “root” for Privilege Escalation Username.
    • Add your AWS SSH private key and click save.
    Figure 2: Create machine credentials
    Figure 2: Create machine credentials.
    1. Set up the inventory as follows:
    • Select the inventory from the left menu.
    • Click on add and select add inventory.
    • Enter a name for the inventory and save it.
    • Select the sources from inventories and click on add.
    • Give a name to the source.
    • Under source, select Amazon EC2.
    • In the Credentials field, select the AWS credentials you created earlier.
    • Save the sources and then click on the Sync button.
    • Now, navigate to the Groups section and notice that aws_ec2 group is created, and your EC2 IPs will be available in the hosts (Figure 3).
    Figure 4: AWS hosts in Inventory
    Figure 3: AWS hosts in Inventory.
    1. Create and Configure the project as follows:
    • Log in to the Ansible Automation Platform portal in the browser.
    • Navigate to the Projects tab under Resources in the left pane.
    • Click on Add to create a new project.
    • Enter a name for the project and choose Git as the source control type with URL https://github.com/redhat-developer-demos/Ansible-use-cases in the Source Control URL field.
    • Save the changes and wait for the operation to complete successfully.
    1. Create and Configure the job templates:
    • Go to the Templates tab under Resources in the left pane and click on the Add button and select Job template from the options.
    • Enter a name for the job you want to create and select the Demo-Inventory or Default inventory in the inventory section.
    • In the Project section, click on the project name you previously created and select the Dynamic Inventory in Ansible Automation Platform/get_sys_data.yml file.
    • In the credential section, select the machine category and choose the credentials for AWS.
    1. Launch the playbooks:
    • Launch the playbook to see the logs of the output as shown in Figure 4:
    Figure 4: Playbook output
    Figure 4: The playbook output.
    1. Delete an EC2 instance and spin up a new instance:
    • Now, let us delete the third machine and create a new ec2 instance.
    • Under AWS console, navigate to the EC2 instance, delete any machine, and create a new instance with the previous configuration setup (Figure 5).
    Figure 5: Deleting an AWS machine
    Figure 5: Deleting an AWS machine.
    1. Sync the inventory:
    • Navigate to the inventory section and click on the inventory you previously created.
    • Under the Sources Tab, select the name created earlier and click on sync.
    • Further, we can also schedule the task to update the inventory after every interval.
    • Notice that our hosts have been updated (Figure 6). It takes time from the AWS console to completely terminate your old EC2 instances, and it might show up under hosts.
    Figure 6: The updated hosts under Ansible Automation Platform
    Figure 6: The updated hosts under Ansible Automation Platform.
    1. Launch the playbook with the updated inventory:
    • Navigate to the Templates section and select the job template created earlier.
    • Launch the playbook (Figure 7).
    Figure 7: The playbook output with updated inventory
    Figure 7: The playbook output with updated inventory.

    Continue your journey with Ansible Automation Platform

    Dynamic inventory not only simplifies inventory management but also enhances the agility and scalability of your workflows. In this article, we have demonstrated the power of efficient infrastructure automation by showcasing the creation and management of three EC2 instances in AWS and the utilization of dynamic inventory to seamlessly adapt to changes.

    Get started with Ansible Automation Platform by exploring interactive hands-on labs. Download Ansible Automation Platform at no cost and begin your automation journey. You can refer to An IT executive's guide to automation e-book for a better understanding of automation. Additionally, check out our series where we explain how to create an EC2 instance in AWS using Ansible, empowering you to efficiently manage your cloud resources.

    Last updated: January 11, 2024

    Related Posts

    • How to create an EC2 instance in AWS using Ansible automation

    • How to create an EC2 instance in AWS using Ansible CLI

    • How to create an EC2 instance in AWS using Ansible workflow

    • Using Ansible to automate Google Cloud Platform

    Recent Posts

    • GuideLLM: Evaluate LLM deployments for real-world inference

    • Unleashing multimodal magic with RamaLama

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    What’s up next?

    This cheat sheet gets you started using Ansible to automate tasks on Cisco Meraki, an enterprise-cloud managed networking solution for managing access points, security appliances, L2 and L3 switches, and more.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue