DEV Community

Lucas M. Ríos
Lucas M. Ríos

Posted on • Updated on

How to configure multiple environments with Ansible Automation Platform?

🦾 Introduction to environment configuration with Ansible Automation Platform


🔗Related content

You can find repo related in:

🐱‍🏍GitHub

You can connect with me in:

🧬LinkedIn


Resume 🧾

🚀 Ansible is a powerful configuration management tool that can be used for provisioning infrastructure.

🛠️ Provisioning refers to the process of configuring and setting up remote systems or infrastructure.

📝 Ansible uses Playbooks, which are written in YAML format, to define the desired state of the infrastructure.

📦 Ansible provides many built-in modules for performing provisioning tasks, such as installing packages, configuring network settings, managing users and groups, and more.


Glosary

AAP: Ansible Automation Platform


Introduction

Supose you have an app and need deploy it in three or more enviornment with specific configuration.

Imagine deploy manually all application included configuration is easy and manageble, but in a small organization will have a minimum of ten applications this is tedious but managable configurate in each server.

Now think in twenty applications this is simply unimaginable manage fast and apply configuration correctly.

In my last time, I have worked a lot with Ansible using AAP and solved many problems in deploying and updating applications.

Other times we don't make mistakes just simply the server doesn't have infrastructure settings appropriately.


What we need to fix all of that?

We will use a function called renderize.

  1. I want belive you have this step.
    We need use good practices of development and understand holisticly all system. Included code, data bases, server, settings...

  2. Create a repo with app. I calling app to code.
    Supose this is called as "repo-app".

  3. Create a repo with configuration values by each environment.
    Supose this is called as "repo-config".

  4. Create a repo with ansible code configuration.
    Supose this is called as "repo-ansible".


What we need make in repo-app?

In files where exists configuration specific of each environment like database of develop, qa and master; or values to configure Auth0.
Files can has any format, i.e: json, yaml, xml, etc.

We will write files where:

# appsetings.json
{
  "API": {
    "Endpoint": "**Endpoint-develop**"
  },
  "reCaptcha": {
    "SiteKey": "**SiteKey-develop**",
    "SecretKey": "**SecretKey-develop**",
    "Score": **Score-develop**,
  },  
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
Enter fullscreen mode Exit fullscreen mode

Insted set values set { { NAME_VARIABLE } }, key squares need are join:

# appsetings.json
{
  "API": {
    "Endpoint": "{ { Client.Endpoint } }"
  },
  "reCaptcha": {
    "SiteKey": "{ { Client.SiteKey } }",
    "SecretKey": "{ { Client.SecretKey } }",
    "Score": { { Client.Score } },
  },  
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
Enter fullscreen mode Exit fullscreen mode

What we need make in repo-config?

WARNING: This repo is special, so stay focus. We don't make merge or promote between branchs. We work with each branch as independent.

In my case I use a file in format yaml to set values of configurations.

Here we set values and this values will appear in server where will be app and values where we configurate with { { NAME_VARIABLE } }:

---
# appsetings.yaml
Client:
  Endpoint: Endpoint-develop
  SiteKey: SiteKey-develop
  SecretKey: SecretKey-develop
  Score: Score-develop
Enter fullscreen mode Exit fullscreen mode

In qa:
Insted set values set { { NAME_VARIABLE } }:

---
# appsetings.yaml
Client:
  Endpoint: Endpoint-qa
  SiteKey: SiteKey-qa
  SecretKey: SecretKey-qa
  Score: Score-qa
Enter fullscreen mode Exit fullscreen mode

In master:

---
# appsetings.yaml
Client:
  Endpoint: Endpoint-master
  SiteKey: SiteKey-master
  SecretKey: SecretKey-master
  Score: Score-master
Enter fullscreen mode Exit fullscreen mode

Here you can put some secrets encrypted with vault and later this will be renderize with AAP credentials.

And other file with origin and destination of files to renderize:

---
# templates.yaml
templates:
  - origin: { { local_path } }/appsettings.json
    dest: { { dest_path } }/appsettings.json
Enter fullscreen mode Exit fullscreen mode

And add many origin and dest for files to set values in your server.


What we need make in repo-ansible?

Usually we will have a repo with only a branch that will be used.

The tasks we need use to renderize app are:
Note: Imagine we use group vars.

---
# renderize.yaml
- name: Download app
  git:
    repo: "https://{ { repos.app.url } }"
    dest: "{ { repos.app.dir } }"
    version: "{ { repos.app.branch } }"
    force: no
  delegate_to: localhost
  run_once: true

- name: Download config
  git:
    repo: "https://{ { repos.config.url } }"
    dest: "{ { repos.config.dir } }"
    version: "{ { repos.config.branch } }"
    force: no
  delegate_to: localhost
  run_once: true

# TO WINDOWS
- name: Renderize templates in windows
  win_template:
    src: "{ { repos.app.dir } }/{ { item.origin } }"
    dest: "{ { item.dest } }"
  loop: "{ { templates } }"

# TO LINUX
- name: Renderize templates in linux
  template:
    src: "{ { repos.app.dir } }/{ { item.origin } }"
    dest: "{ { item.dest } }"
  loop: "{ { templates } }"

Enter fullscreen mode Exit fullscreen mode

Say thanks, give like and share if this has been of help/interest 😁🖖


Top comments (0)