by Joaquin Menchaca, April 2016
This exercise that demonstrates Vagrant configuration scenarios with a global configuration file. As a bonus, an Ansible dynamic inventory script that sources the same configuration file is provided.
This demonstrates how to parse a configuration file (in INI, YAML, JSON, XML, CSV, SQL, or hosts file formats) in ruby, and bash. amd python languages.
- Static Configuration
- Dynamic Multi-Machine
- Data-structure in Memory
- Data-structure from File
- VirtualBox - virtualization system
- Vagrant - virtualization and provisioning automation tool
- Ansible (optional) - change configuration tool that can execute commands on vagrant virtual systems.
- Python - language required by Ansible
For Windows, highly recommend, MSYS2 as it gives you to bash
shell and access to tools like git
, ssh
, curl
, and rsync
. This is optional.
Note that Ansible is not officially supported on Windows for a workstation (or system used to configure other systems).
Install Homebrew, Cask, and Brew Bundle, then get other requirements by running:
$ ./install_macosx.sh
Install Chocolately using command shell (cmd.exe
) as Administrator, then run
C:\> choco install chocolately.config
C:\> vagrant plugin install sqlite3
C:\> vagrant plugin install inifile
The data is a 4D data structure (hash of hash of list of hash) to spice things up. Each section is divided into of configuration area: hosts
, defaults
, and ports
. Thus there may be redundancy.
The provisioning and inventory scripts only care about hosts
key, so they'll try to rip this out into arrays to use the data. This is used to configure hosts
and ssh_config
for ssh and name resolution convenience. For the inventory,this is used to create a JSON
file that can be used with ansible
or ansible-playbook
commands.
This is what the base file layout looks like, using YAML as an example:
.
├── multimachine-yaml
│ ├── README.md
│ ├── Vagrantfile # main ansible configuration
│ ├── config
│ │ ├── global.yaml # global configuration
│ │ └── inventory.py # ansinble inventory script, must be next to config
│ └── scripts
│ ├── baselib.src # local copy of baselib.sh
│ ├── client.sh # provisioning script
│ ├── master.sh # provisioning script
│ ├── setup-base.sh # base provisioning script for ssh_config and hosts
│ ├── slave.sh # provisioning script
│ ├── slave1.sh -> slave.sh # symlink to common provisioning script
│ └── slave2.sh -> slave.sh # symlink to common provisioning script
└── scriptlib
├── baselib.sh # main shell provisioning library
├── common.vagrantfile # common base vagrantfile that uses settings hash
└── yaml.rb # creates settings hash from ruby
MSYS2 is a light minimalist bash environment for Windows, similar to MSYS that comes with MinGW (Minimalist GNU for Windows ) and Git-Bash tools, except that it is 64-bit. If you use advanced features like rsync, then you'll need a 64-bit rsync solution.
After installing MSYS2, e.g. choco install msys2
, run:
$ pacman --needed -Sy bash pacman pacman-mirrors msys2-runtime
Close and restart MSYS2 and run:
$ pacman -Su
$ # other tools (optional)
$ pacman -S rsync
$ pacman -S git
$ pacman -S curl
These are some topics I came across while researching Vagrant, Ruby libraries and shell command-line tools and scripts:
- Vagrant
- Multi-Machine Docs: https://www.vagrantup.com/docs/multi-machine/
- Vagrant 1.5 Plugin Improvements: https://www.hashicorp.com/blog/vagrant-1-5-plugin-improvements.html#toc_1
- Vagrant Development Basics: https://www.vagrantup.com/docs/plugins/development-basics.html
- Vagrant Packaging: https://www.vagrantup.com/docs/plugins/packaging.html
- Bash
- Bash Ini Parser - http://theoldschooldevops.com/2008/02/09/bash-ini-parser/
- Parse Yaml Script - https://gist.github.com/pkuczynski/8665367
- JSON.sh - https://github.com/dominictarr/JSON.sh
- Command Line Tools
- jq (JSON Query)- https://stedolan.github.io/jq/
- xml2 - http://www.ofb.net/~egnor/xml2/
- json2csv - https://github.com/jehiah/json2csv
- xml2json - https://github.com/Inist-CNRS/node-xml2json-command
- csvkit - http://csvkit.readthedocs.org/en/0.9.1/
- yaml2json - https://www.npmjs.com/package/yamljs
- json2yaml - https://www.npmjs.com/package/yamljs
- shyaml - https://github.com/0k/shyaml
- Ruby Gems
- Python Packages
- simplejson - SimpleJSON python module
- pyaml - PyYAML
- configparser - ConfigParser for INI files
- Tutorials/References
- Python
- SQLite Python Tutorial
- Python Database API Specification v2.0
- CSV File Reading and Writing
- The Hitchhiker's Guide to Python
- Python
The content of this project itself is licensed under the Creative Commons Attribution 3.0 license, and the underlying source code is licensed under the MIT license.