Skip to content

Static Inventory

Ansible's static inventory configuration is a foundational concept for defining hosts and groups in a declarative manner. Unlike dynamic inventory, which leverages scripts or APIs to fetch host information, static inventory requires manual definition of hosts and groups in a file. This approach is ideal for small environments, testing, or scenarios where precise control over inventory structure is needed. While it lacks the scalability of dynamic inventory, it offers simplicity and clarity for straightforward use cases.


Inventory File Structure

The static inventory file (typically inventory.ini or a custom name) uses a simple syntax to define hosts and groups. Hosts are grouped into logical categories, and variables can be assigned to hosts or groups.

Basic Syntax

[web_servers]
web1.example.com
web2.example.com

[db_servers]
db1.example.com

[monitoring]
monitor.example.com
  • [group_name] defines a host group.
  • Hosts are listed under their respective groups.
  • Additional sections can define variables for groups or hosts.

Example with Variables

[web_servers]
web1.example.com http_port=80
web2.example.com http_port=8080

[web_servers:vars]
proxy_host=proxy.example.com
  • http_port is a host-specific variable.
  • proxy_host is a group-wide variable.

Group Definitions and Hierarchies

Groups can be nested to create hierarchical structures, enabling more granular organization. For example:

[webservers]
web1.example.com

[database]
db1.example.com

[app_servers]
web1.example.com
db1.example.com

Here, app_servers includes hosts from both webservers and database groups. This allows shared variables or playbooks to target multiple groups simultaneously.


Variables and Overriding

Variables in static inventory can be defined at the host, group, or global level. When conflicts arise, Ansible prioritizes variables defined closer to the target (host > group > global).

Host Variables

[webservers]
web1.example.com http_port=80

Group Variables

[webservsers:vars]
proxy_host=proxy.example.com

Overriding Variables

[webservers]
web1.example.com http_port=80

[webservers:vars]
http_port=8080

In this case, web1.example.com will use http_port=80 because it is defined explicitly.


Best Practices

  1. Organize by logical roles: Group hosts by function (e.g., web_servers, databases).
  2. Use separate files for variables: Store group-specific variables in group_vars/ and host-specific variables in host_vars/ directories.
  3. Avoid redundancy: Define shared variables in group sections rather than repeating them for each host.
  4. Secure sensitive data: Use Ansible Vault to encrypt variables like passwords or API keys.
  5. Limit scope: Use static inventory for small environments; scale to dynamic inventory for large or dynamic infrastructures.

Key takeaways

  • Static inventory is a manual, declarative way to define hosts and groups in Ansible.
  • Use [group_name] sections to organize hosts and assign variables.
  • Prioritize variables defined closer to the target (host > group > global).
  • Combine static inventory with group_vars and host_vars for modular management.
  • Reserve static inventory for small or static environments; use dynamic inventory for scalability.