Ansible's inventory is fundamentally the list of hosts – your servers, VMs, or containers – that it will manage. It acts as the "source of truth" for where Ansible needs to run its tasks. A static inventory is the simplest form, where you manually define your hosts, their IP addresses or hostnames, and group them directly within a file. This approach is straightforward and effective for smaller, stable environments, such as a development server, a few on-premise machines with fixed IPs, or a proof-of-concept setup. You typically write these in an INI or YAML format, providing clear, human-readable definitions of your infrastructure's targets.
While simple, static inventories quickly become unwieldy and error-prone as your infrastructure grows or becomes more dynamic, especially in cloud environments where instances are frequently provisioned, de-provisioned, or scaled automatically. This is where dynamic inventory shines. Instead of a fixed file, dynamic inventory relies on a script or a plugin that queries an external source – like AWS EC2, Azure VMs, Google Cloud, VMware vSphere, or even a Configuration Management Database (CMDB) – to generate the list of hosts on-the-fly. This ensures that Ansible always has the most up-to-date view of your infrastructure, automatically including new instances and excluding terminated ones.
Embracing dynamic inventory is critical for a DevOps Engineer working with modern cloud-native architectures. It eliminates manual intervention, reduces human error, and allows Ansible playbooks to adapt seamlessly to changes in your environment, such as auto-scaling events or blue/green deployments. You'd typically use static inventory for very contained, unchanging environments, perhaps for a local Vagrant setup or specific immutable infrastructure components. For anything scalable, ephemeral, or cloud-based, dynamic inventory is the go-to solution, providing the necessary automation backbone to manage complex, distributed systems efficiently.
Key Takeaways
- Static inventory manually defines hosts in a file, suitable for small, stable environments.
- Dynamic inventory automatically discovers hosts via scripts/plugins, essential for scalable cloud infrastructure.
- Dynamic inventory integrates with cloud providers (AWS, Azure, GCP), virtualization, and CMDBs.
- Use static for simplicity in fixed setups; use dynamic for automation, scalability, and cloud environments.
Code Example
[webservers]
web1.example.com
web2.example.com ansible_host=192.168.1.10
[databases]
db1.example.com
[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsaHow this code works
This Ansible inventory file acts as a central directory, telling Ansible which servers it can manage and how to communicate with them. It organizes hosts into logical groups, enabling targeted operations. The [webservers] and [databases] sections define these groups, listing individual servers like web1.example.com and db1.example.com. For web2.example.com, the ansible_host variable explicitly tells Ansible to connect to a specific IP address (192.168.1.10) instead of relying on the hostname's default DNS resolution. This flexibility is key when hostnames aren't directly routable or need specific IP mapping.
The [all:vars] section establishes global connection parameters that apply to every host listed in this inventory. ansible_user=ubuntu specifies the username for SSH connections, while ansible_ssh_private_key_file points to the authentication key. A subtle but crucial point is Ansible's connection behavior: if ansible_host is present for a host, Ansible prioritizes that specified IP address for the connection, effectively overriding any attempt to resolve the declared hostname. If ansible_host is absent, Ansible will connect directly to the hostname, expecting it to be resolvable via DNS or /etc/hosts.