English translation
15. Grouping Hosts and Defining Host Variables in Ansible Inventory
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous article, we explored Ansible’s dynamic inventory, which enables flexible management and generation of inventory files. In this tutorial, we focus on using groups and host variables in Ansible’s static inventory files to better organize and manage server configurations.
Introduction to Ansible Inventory
Inventory is one of Ansible’s core concepts—it defines the managed hosts and how Ansible interacts with them. By leveraging inventory effectively, you can group hosts logically and assign specific variables to individual hosts or groups.
Inventory File Formats
Ansible supports multiple inventory file formats, including INI and YAML. Below is an example INI-formatted inventory file:
[web]
web01.example.com
web02.example.com
[db]
db01.example.com
db02.example.com
[production:children]
web
db
[production:vars]
env=production
location=us-west
In this example:
- Two host groups are defined:
webanddb. - A parent group named
productionis created using the:childrensuffix, containing thewebanddbsubgroups. - Group-level variables (
envandlocation) are assigned to theproductiongroup using the:varssuffix.
Groups and Host Variables
Groups
Groups allow logical organization of hosts. In practice, you might separate web servers, database servers, and other infrastructure components into distinct groups. Ansible supports hierarchical grouping—enabling parent-child relationships for fine-grained control.
Host Variables
Host variables let you define unique configuration parameters for individual hosts—for instance, different SSH users or ports per web server. Example:
[web]
web01.example.com ansible_user=admin ansible_port=2222
web02.example.com ansible_user=ubuntu ansible_port=2200
[db]
db01.example.com ansible_user=dbadmin
Here, web01.example.com and web02.example.com use different SSH users and ports—providing flexibility during task execution.
Using Group Variables
Group variables apply the same configuration across all hosts in a group—for example, security settings or application versions. You can declare them inline using the :vars suffix:
[db:vars]
db_password=SuperSecretPassword
In this case, every host in the db group inherits the db_password variable.
Using Host Variables
Beyond group variables, Ansible lets you assign variables directly to individual hosts within the inventory file—by appending key-value pairs after the hostname:
db01.example.com ansible_user=dbadmin db_host=db01.example.com
Mini Use Case
Consider a simple infrastructure with two web servers and one database server. We want to centralize configuration in the inventory file:
[web]
web01.example.com ansible_user=webuser
web02.example.com ansible_user=webuser
[db]
db01.example.com ansible_user=dbuser db_password=mysecurepassword
[production:children]
web
db
[production:vars]
env=production
Here, both web servers share the same user (webuser), while the database server uses its own dedicated user and password. These variables can then be referenced directly in Ansible playbooks.
Summary
This tutorial covered how to use groups and host variables in Ansible inventory files. Groups provide logical structure for organizing hosts, while host and group variables deliver powerful flexibility—allowing tailored configuration per host or across entire groups. Thoughtful inventory design significantly improves efficiency and maintainability in Ansible-based automation.
In the next article, we’ll introduce Ansible modules—covering common use cases, practical examples, and best practices. Stay tuned!
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after 15. Grouping Hosts and Defining Host Variables in Ansible Inventory?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue