Skip to content

Commit c4060ab

Browse files
author
Gustavo Muniz do Carmo
committed
first commit
0 parents  commit c4060ab

33 files changed

+697
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.vagrant/
2+
.vscode/
3+
*.retry
4+
ubuntu-*-cloudimg-console.log
5+
env
6+
__pycache__

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
language: python
3+
python: "3.6"
4+
5+
addons:
6+
apt:
7+
packages:
8+
- python-pip
9+
10+
install:
11+
- pip install -r requirements.txt
12+
13+
script:
14+
- molecule test -s docker
15+
16+
notifications:
17+
webhooks: https://galaxy.ansible.com/api/v1/notifications/

.yamllint

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
extends: default
3+
ignore: |
4+
**/lib/
5+
rules:
6+
braces:
7+
max-spaces-inside: 1
8+
level: error
9+
brackets:
10+
max-spaces-inside: 1
11+
level: error
12+
line-length: disable
13+
# NOTE(retr0h): Templates no longer fail this lint rule.
14+
# Uncomment if running old Molecule templates.
15+
# truthy: disable

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Esign Consulting Ltda.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Same configuration file with different content in different environments
2+
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![GitHub release](https://img.shields.io/github/release/codeyourinfra/same_cfgfile_diff_content.svg)](https://github.com/codeyourinfra/same_cfgfile_diff_content/releases/latest) [![Build status](https://travis-ci.org/codeyourinfra/same_cfgfile_diff_content.svg?branch=master)](https://travis-ci.org/codeyourinfra/same_cfgfile_diff_content) [![Ansible Role](https://img.shields.io/ansible/role/42056.svg)](https://galaxy.ansible.com/codeyourinfra/same_cfgfile_diff_content) [![Ansible Role downloads](https://img.shields.io/ansible/role/d/42056.svg)](https://galaxy.ansible.com/codeyourinfra/same_cfgfile_diff_content)
4+
5+
This solution is explained in detail in the Codeyourinfra project blog post [How to deal with the same configuration file with different content in different environments](http://codeyourinfra.today/how-to-deal-with-the-same-configuration-file-with-different-content-in-different-environments). Check it out!
6+
7+
## Problem
8+
9+
The dev team has to deploy the application in different QA environments. The application requires a properties file where either the database string connection and the database credentials are defined. In each environment they are different. This is just an example of the same configuration file with different content in different environments.
10+
11+
## Solution
12+
13+
Instead of having a version of the same configuration file for each environment, it's possible to keep just one, including all the values required in every environment ([conncfg.yml](conncfg.yml)). This variables are then used during the **same_cfgfile_diff_content** [Ansible role](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html)'s execution, when the configuration file's content is set, specific to each environment.
14+
15+
```yml
16+
---
17+
- hosts: qa
18+
vars_files:
19+
- conncfg.yml
20+
roles:
21+
- same_cfgfile_diff_content
22+
```
23+
24+
## Test
25+
26+
First of all, turn on the 3 VMs that represent the QA environments, executing the command `$ vagrant up`. After that, execute the command `$ ansible-playbook playbook.yml`. Finally, in order to check the configuration file's content of every environment, execute the command `$ ansible qa -m shell -a "cat /etc/conn.properties"`.
27+
28+
### Automated tests
29+
30+
You can also test the solution automaticaly, by executing `./test.sh` or using [Molecule](https://molecule.readthedocs.io). With the latter, you can perform the test not only locally (the default), but in [AWS](https://aws.amazon.com) as well. During the Codeyourinfra's *continuous integration* process in Travis CI, the solution is tested on [Amazon EC2](https://aws.amazon.com/ec2).
31+
32+
In order to get your environment ready for using *Molecule*, prepare your [Python virtual environment](https://docs.python.org/3/tutorial/venv.html), executing `python3 -m venv env && source env/bin/activate && pip install -r requirements.txt`. After that, just run the command `molecule test`, to test the solution locally in a [VirtualBox](https://www.virtualbox.org) VM managed by [Vagrant](https://www.vagrantup.com).
33+
34+
If you prefer performing the test in AWS, bear in mind you must have your credentials appropriately in **~/.aws/credentials**. You can [configure it through the AWS CLI tool](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). The test is performed in the AWS region *Europe - London (eu-west-2)*. Just run `molecule test -s aws` and check the running instances through your [AWS Console](https://eu-west-2.console.aws.amazon.com/ec2/v2).

Vagrantfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
load '../common/timestamp-appender.rb'
5+
6+
Vagrant.configure("2") do |config|
7+
config.vm.box = "ubuntu/bionic64"
8+
9+
(1..3).each do |i|
10+
config.vm.define "qa#{i}" do |qa|
11+
qa.vm.hostname = "qa#{i}"
12+
qa.vm.network "private_network", ip: "192.168.33.#{i}0"
13+
end
14+
end
15+
16+
config.vm.provider "virtualbox" do |v|
17+
v.linked_clone = true
18+
end
19+
end

ansible.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[defaults]
2+
callback_whitelist = profile_tasks
3+
host_key_checking = False
4+
roles_path = ../
5+
inventory = inventory.yml
6+
localhost_warning = False
7+
8+
[inventory]
9+
enable_plugins = yaml

conncfg.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
cfgfile_path: /etc/conn.properties
3+
content:
4+
qa1:
5+
url: jdbc:mysql://db1/mydb
6+
username: qa1user
7+
password: qa1secret
8+
qa2:
9+
url: jdbc:mysql://db2/mydb
10+
username: qa2user
11+
password: qa2secret
12+
qa3:
13+
url: jdbc:mysql://db3/mydb
14+
username: qa3user
15+
password: qa3secret

defaults/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
cfgfile_path: /etc/conf
3+
content:
4+
qa1:
5+
prop1: A
6+
prop2: B
7+
qa2:
8+
prop1: C
9+
prop2: D
10+
qa3:
11+
prop1: E
12+
prop2: F

inventory.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
all:
3+
children:
4+
qa:
5+
hosts:
6+
qa1:
7+
ansible_host: 192.168.33.10
8+
ansible_ssh_private_key_file: .vagrant/machines/qa1/virtualbox/private_key
9+
qa2:
10+
ansible_host: 192.168.33.20
11+
ansible_ssh_private_key_file: .vagrant/machines/qa2/virtualbox/private_key
12+
qa3:
13+
ansible_host: 192.168.33.30
14+
ansible_ssh_private_key_file: .vagrant/machines/qa3/virtualbox/private_key
15+
vars:
16+
ansible_user: vagrant
17+
ansible_python_interpreter: python3

0 commit comments

Comments
 (0)