Skip to content

Latest commit

 

History

History
169 lines (121 loc) · 5.82 KB

ansible-inventory.md

File metadata and controls

169 lines (121 loc) · 5.82 KB

Ansible Inventory Overview

The Ansible inventory is a fundamental component in Ansible that defines the hosts (or nodes) and groups of hosts that Ansible will manage. It provides a list of targets to which Ansible can apply playbooks and run tasks. The inventory can be static or dynamic, depending on the needs of your environment.

Types of Inventory

  1. Static Inventory:

    • A static inventory is a simple file (usually in INI or YAML format) that lists the hosts and groups that Ansible will manage. It is ideal for environments where the infrastructure doesn't change frequently.
  2. Dynamic Inventory:

    • A dynamic inventory is generated by a script or plugin that queries external sources (e.g., cloud providers, databases, etc.) to obtain a list of hosts. It is used in dynamic environments where the infrastructure is constantly changing.

Static Inventory File

A static inventory file typically has the .ini or .yaml extension. Here's an example of a simple .ini format inventory:

# Inventory.ini
[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa

Breakdown:

  • [webservers] and [dbservers]: These are groups of hosts. Each group can contain one or more hosts.
  • web1.example.com and db1.example.com: These are the individual hosts (nodes) that belong to the groups.
  • [all:vars]: A special group that defines variables that apply to all hosts in the inventory.

Grouping Hosts

  • Host Groups:

    • Hosts can be organized into groups to apply tasks selectively. For example, you might have a group of webservers and another group of dbservers.
  • Nested Groups:

    • Ansible allows you to nest groups within other groups. For example:

      [webservers]
      web1.example.com
      web2.example.com
      
      [dbservers]
      db1.example.com
      db2.example.com
      
      [production:children]
      webservers
      dbservers
      • Here, production is a parent group containing both the webservers and dbservers groups.
  • Group Variables:

    • You can define variables that apply to an entire group of hosts:

      [webservers]
      web1.example.com
      web2.example.com
      
      [webservers:vars]
      ansible_user=webuser
      • The variable ansible_user is applied to all hosts in the webservers group.

Host Variables

  • Host-Specific Variables:
    • Variables can be defined for individual hosts directly within the inventory file:

      [webservers]
      web1.example.com ansible_host=192.168.1.10 ansible_user=webuser
      web2.example.com ansible_host=192.168.1.11 ansible_user=webuser
      • Here, each host has its own IP address (ansible_host) and username (ansible_user).

YAML Inventory Format

Ansible also supports inventory files written in YAML format. Here’s an equivalent YAML example:

all:
  hosts:
    web1.example.com:
      ansible_host: 192.168.1.10
      ansible_user: webuser
    web2.example.com:
      ansible_host: 192.168.1.11
      ansible_user: webuser
  children:
    webservers:
      hosts:
        web1.example.com:
        web2.example.com:
    dbservers:
      hosts:
        db1.example.com:
        db2.example.com:

Dynamic Inventory

A dynamic inventory is generated on-the-fly by executing a script or plugin that queries an external system (e.g., AWS, GCP, VMware, etc.) to gather host information. Ansible comes with several built-in dynamic inventory scripts, and you can also write custom scripts.

  • AWS EC2 Example:

    • Ansible provides a script to fetch inventory from AWS EC2:

      ansible-playbook -i aws_ec2.yaml playbook.yml
    • The aws_ec2.yaml file contains the necessary configuration to query AWS and generate the inventory dynamically.

  • Advantages of Dynamic Inventory:

    • Automatically reflects changes in infrastructure.
    • Reduces manual management of inventory files.
    • Supports tagging, regions, and other filters to target specific hosts.

Inventory Variables

  • Host and Group Variables:

    • Variables can be defined for both hosts and groups directly in the inventory file. These variables can be used in playbooks to customize tasks for specific hosts or groups.
  • Variables in Separate Files:

    • Host and group variables can also be stored in separate files for better organization. For example, in a directory structure:

      inventory/
      ├── group_vars/
      │   └── all.yml
      ├── host_vars/
      │   └── web1.example.com.yml
      └── hosts.ini
      • The group_vars and host_vars directories contain YAML files with variables that apply to groups and hosts, respectively.

Inventory Best Practices

  1. Use Descriptive Group Names:

    • Use clear and descriptive names for groups to make your inventory easy to understand.
  2. Organize Inventory in Directories:

    • For large environments, organize inventory into directories with separate files for groups, hosts, and variables.
  3. Use Dynamic Inventory for Cloud Environments:

    • In dynamic cloud environments, leverage dynamic inventory scripts to automate host management.
  4. Document Inventory Files:

    • Comment your inventory files to explain the purpose of each group and variable, making it easier for others to understand.
  5. Secure Sensitive Information:

    • Store sensitive data like passwords and API keys in Ansible Vault or use environment variables.

Conclusion

Ansible's inventory system is highly flexible and can be adapted to various environments, from small static setups to dynamic cloud infrastructures. By effectively organizing and managing your inventory, you can simplify your automation processes and ensure that tasks are applied to the correct hosts with the appropriate settings.