Ansible manages inventory through a structured list of hosts or systems that it can automate. The inventory defines the target machines for automation tasks and can be specified in various formats. Here are the main ways Ansible manages inventory:
-
Static Inventory:
- Defined in a simple text file (usually
hostsorinventory). - Hosts are listed under groups, allowing for organized management.
- Example of a static inventory file:
[webservers] web1.example.com web2.example.com [dbservers] db1.example.com db2.example.com
- Defined in a simple text file (usually
-
Dynamic Inventory:
- Generated on-the-fly using scripts or plugins that query external sources (like cloud providers).
- Useful for environments where hosts change frequently (e.g., cloud instances).
- Ansible provides built-in dynamic inventory scripts for various platforms (AWS, GCP, etc.).
-
Inventory Variables:
- You can define variables specific to hosts or groups within the inventory file.
- Example:
[webservers] web1.example.com ansible_user=admin ansible_port=2222 web2.example.com ansible_user=admin ansible_port=2222
-
Inventory Plugins:
- Ansible supports various inventory plugins that allow for more complex inventory management.
- These plugins can pull inventory from different sources, such as cloud providers, LDAP, or databases.
-
Host Patterns:
- Ansible allows you to target specific hosts or groups using patterns in playbooks.
- Example of targeting all web servers:
- hosts: webservers tasks: - name: Ensure Apache is installed apt: name: apache2 state: present
By using these methods, Ansible provides flexibility in managing inventory, making it suitable for a wide range of environments and use cases.
