Efficient Variable Management in Ansible for Complex Multi-Environment Applications

Managing variables in Ansible can become a challenge when you’re dealing with multiple applications, environments, and server types. In this post, I will show you how to structure your Ansible inventory using group_vars to efficiently manage variables for a complex infrastructure setup.

Scenario: Multi-Application, Multi-Environment Setup

Let’s imagine we have three different application groups, each identified by an application code: AP001AP002, and AP003. Each application group contains the following types of servers:

  • 2 web servers (per environment: development, pre-production, production)
  • 2 application servers (per environment)
  • 2 database servers (per environment)

In this scenario, certain variables such as apache_port, database connection details, and application specific configurations differ between environments (development, pre-production, production) and across server types (web, application, database).

Structuring group_vars by Application, Environment, and Server Type

To handle this complexity, a clean and scalable solution is to organize group_vars by application code, environment, and server type. Here’s an example of how to structure the directories and files:

group_vars/
  AP001/
    dev/
      webservers.yml
      appservers.yml
      dbservers.yml
    preprod/
      webservers.yml
      appservers.yml
      dbservers.yml
    prod/
      webservers.yml
      appservers.yml
      dbservers.yml
  AP002/
    dev/
      webservers.yml
      appservers.yml
      dbservers.yml
    preprod/
      webservers.yml
      appservers.yml
      dbservers.yml
    prod/
      webservers.yml
      appservers.yml
      dbservers.yml
  AP003/
    dev/
      webservers.yml
      appservers.yml
      dbservers.yml
    preprod/
      webservers.yml
      appservers.yml
      dbservers.yml
    prod/
      webservers.yml
      appservers.yml
      dbservers.yml

This structure allows you to define specific variables for each application code, environment, and server type.

Example Variable Definitions

  • AP001 (Development, Web Servers):
# group_vars/AP001/dev/webservers.yml
apache_port: 8080
server_name: "dev-webserver1.ap001.local"
max_clients: 200
  • AP001 (Pre-production, Application Servers):
# group_vars/AP001/preprod/appservers.yml
app_port: 8001
app_debug: false
server_name: "preprod-appserver1.ap001.local"
  • AP002 (Production, Database Servers):
# group_vars/AP002/prod/dbservers.yml
db_port: 5432
db_name: "prod_db_ap002"
db_backup_enabled: true
  • AP003 (Development, Application Servers):
# group_vars/AP003/dev/appservers.yml
app_port: 8002
app_debug: true
log_level: "debug"

Why This Approach Works

This structure has several advantages:

  1. Scalability: As new applications or environments are added, you can easily extend this structure without making significant changes.
  2. Clarity: Organizing variables by application, environment, and server type makes it easy to find and manage specific configurations without confusion.
  3. Granular Control: You can define environment-specific variables while still maintaining shared values across server types where needed.

Potential Enhancements

If you find yourself repeating variables across multiple application groups or environments, consider creating higher-level variable files for shared configurations, or using defaults/ in specific roles to reduce duplication.

Compartir:

This article was written by RoberMB

💻OS, ☁️Cloud, 🛡️Cybersecurity, ✈️Traveling #Linux, #Ansible, #AWS, #VMware, #Docker 🏴‍☠️ CEH v10, CPHE 🏴‍☠️ ... Always learning, always enjoying.

Leave a Reply

Your email address will not be published. Required fields are marked *