In this tutorial we use Ansible (installed in virtualenv) and Vagrant. Furthermore, we have different machines (Debian, CentOS). For all hosts we want to have Provisioning on startup and via command.
Precondition
- pip and virtualenv installed
- Vagrant installed
- BaseBoxes created (or downloaded)
Folder structure
. ├── Makefile ├── Vagrantfile ├── playbook.yml ├── requirements.txt └── roles └── common └── tasks └── main.yml
Files
ansible ansible-lint
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # CentOS 7 VM config.vm.define "centos" do |centos| centos.vm.box = "lupin/centos7" centos.vm.network :forwarded_port, guest: 22, host: 2221, id: 'ssh' centos.vm.provider "virtualbox" do |vb| vb.name = "CentOS7-Vagrant-Ansible" end centos.vm.provision "ansible" do |ansible| # ansible.verbose = "v" ansible.playbook = "playbook.yml" end end # Debian 8 VM config.vm.define "debian" do |debian| debian.vm.box = "lupin/debian8" debian.vm.network :forwarded_port, guest: 22, host: 2222, id: 'ssh' debian.vm.provider "virtualbox" do |vb| vb.name = "Debian-Vagrant-Ansible" end debian.vm.provision "ansible" do |ansible| # ansible.verbose = "v" ansible.playbook = "playbook.yml" end end end
--- - hosts: all become: yes gather_facts: yes roles: - common
--- - debug: msg="System {{ ansible_distribution }}"
ENV_DIR = env CURRENT_DIR := $(shell pwd) INTERPRETER = $(CURRENT_DIR)/$(ENV_DIR)/bin/ PATH := ${PATH}:$(INTERPRETER) help: @echo "Run make <target> with:" @echo " > env : create virtualenv on folder $(ENV_DIR)" @echo " > deps : install dependentcies" @echo " > cleanenv : delete virtualenv" @echo " > start : run vagrant up" @echo " > provisioning : start ansible provisioning" @echo " > kill : run vagrant destroy" debug: @echo " > ansible location is : $(INTERPRETER)" @echo " > environment variable is : $(PATH)" vagrant status env: virtualenv $(ENV_DIR) && \ . $(ENV_DIR)/bin/activate && \ make deps deps: $(ENV_DIR)/bin/pip install -r requirements.txt cleanenv: rm -fr $(ENV_DIR) start: vagrant up provisioning: vagrant provision kill: vagrant destroy -f
Usage
# create environment $ make env # start vagrant (create VM`s and run provisioning) $ make start # run provisioning (on started VM`s) $ make provisioning # stop vagrant (delete VM`s) $ make kill # delete environment $ make cleanenv
Hint
Check out the by Vagrant generated inventory file!
$ cat .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory