3-Day1-3-inventory

Lab Exercise: Managing Ansible Inventories Effectively (INI Format)

This lab exercise focuses on managing and testing Ansible inventories in INI format. Exercises are designed with core and optional tasks, focusing on practical commands like uptime, date, checking the OS version, release, and kernel version. The text editor is updated to use vi for file management.


Core Exercises (Mandatory)

Exercise 1: Create a Basic Static Inventory

  1. Create an Inventory File:

    • Create a file named inventory.ini using vi:

      vi ~/inventory.ini
    • Add the following content and save the file:

      [web]
      web1 ansible_host=192.168.1.10 ansible_user=userxx ansible_ssh_pass=password
      web2 ansible_host=192.168.1.11 ansible_user=userxx ansible_ssh_pass=password
      
      [db]
      db1 ansible_host=192.168.1.20 ansible_user=userxx ansible_ssh_pass=password
      db2 ansible_host=192.168.1.21 ansible_user=userxx ansible_ssh_pass=password
  2. Ping All Hosts in the Inventory: Test connectivity to all hosts:

    ansible all -i ~/inventory.ini -m ping
    • Expected Output: Success messages confirming connectivity to all hosts.


Exercise 2: Group Hosts and Use Patterns

  1. Group Inventory by Environment:

    • Edit inventory.ini using vi:

    • Modify the content to include groups:

  2. Run Commands Using Group Names:

    • Test the prod group:

  3. Run Commands Using Patterns:

    • Ping only the first host in each group:

    • Run a command on all hosts except those in the web group:


Exercise 3: Check System Information

  1. Check OS Version and Release:

    • Use the command module to check OS version:

  2. Check Kernel Version:

    • Run the following command to retrieve the kernel version:

  3. Verify Disk Space Usage:

    • Use the command module to check disk space:


Exercise 4: Use Variables in Inventory

  1. Add Variables to Groups:

    • Edit inventory.ini using vi:

    • Add group-specific variables:

  2. Verify Variables:

    • Use the setup module to check variables:


Optional Exercises (For Fast Learners)

Exercise 5: Add Nested Groups

  1. Enhance the Inventory:

    • Add more groups to inventory.ini:

  2. Run Commands on Nested Groups:

    • Test the all group:


Exercise 6: Validate Inventory Syntax

  • Use the ansible-inventory command to validate the inventory file:

    • Expected Output: JSON-formatted inventory details.


Exercise 7: Combine Patterns

  1. Run Commands Using Advanced Patterns:

    • Check kernel version for all hosts in the prod group except db2:

  2. Limit Execution to Specific Hosts:

    • Check the OS release for a specific host (web1):


Expected Outcomes

  • Participants will be able to:

    • Create and manage inventory files using vi.

    • Test connectivity and execute various system commands like checking uptime, date, OS version, kernel version, and disk space.

    • Organize hosts into groups and manage variables effectively.

    • Use patterns and advanced inventory techniques to target specific hosts or groups.

  • Optional exercises enable participants to explore nested groups, validate inventory syntax, and use advanced patterns for dynamic host targeting.

Last updated