Download - One vagrantfile to rule them all
![Page 1: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/1.jpg)
One Vagrantfile to rule them allConfig Management Camp 2016, Ghent
Bert Van Vreckem ([email protected])
![Page 2: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/2.jpg)
I use Vagrant a lot
$ find ~ -type f -name Vagrantfile | wc --lines350
![Page 3: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/3.jpg)
$ vagrant init
⇓
# CommentsVagrant.configure(2) do |config|
# more commentsconfig.vm.box = "base"
# Lots more commentsend
![Page 4: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/4.jpg)
typical single machine setup
Vagrant.configure(2) do |config|config.vm.box = ’bertvv/centos71’config.vm.hostname = ’box001’config.vm.network ’private_network’,
ip: 192.168.56.10
config.vm.provision ’ansible’ do |ansible|ansible_playbook = ’ansible/site.yml’
endend
![Page 5: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/5.jpg)
Multi-machine setup
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define ’box001’ do |node|node.vm.hostname = ’box001’node.vm.box = ’bertvv/centos7’node.vm.network :private_network, ip: ’192.168.56.10’
end
config.vm.define ’box002’ do |node|node.vm.hostname = ’box002’node.vm.box = ’bertvv/centos7’node.vm.network :private_network, ip: ’192.168.56.11’
end
config.vm.provision ’ansible’ do |ansible|ansible_playbook = ’ansible/site.yml’
endend
![Page 6: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/6.jpg)
this starts to smell
• not a config file, but code!• copy/paste code blocks• hard-coded values• will become unmaintanable!
![Page 7: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/7.jpg)
what I want
• reusable• sane defaults• DRY code• separation of data and instructions• avoid Ruby
![Page 8: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/8.jpg)
in Vagrantfile:
hosts = YAML.load_file(’vagrant-hosts.yml’)
# vagrant-hosts.yml---- name: box001
ip: 192.168.56.10- name: box002
ip: 192.168.56.11
![Page 9: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/9.jpg)
# vagrant-hosts.yml---- name: box003
box: fedora22box_url: https://example.com/boxes/fedora22.boxip: 172.22.0.5netmask: 255.255.0.0mac: ’00:11:22:33:44:55’intnet: truesynced_folders:
- src: testdest: /tmp/test
- src: wwwdest: /var/www/htmloptions:
:create: true:owner: root:group: root
![Page 10: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/10.jpg)
Vagrantfile
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
hosts.each do |host|config.vm.define host[’name’] do |node|
node.vm.box = host[’box’] ||= DEFAULT_BASE_BOXif host.has_key? ’box_url’
node.vm.box_url = host[’box_url’]end
node.vm.hostname = host[’name’]node.vm.network :private_network,
network_options(host)custom_synced_folders(node.vm, host)
# ...
![Page 11: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/11.jpg)
Vagrantfile (cont’d)
node.vm.provider :virtualbox do |vb|vb.name = host[’name’]vb.customize [’modifyvm’, :id,
’--groups’, PROJECT_NAME]end
endendprovision_ansible(config)
end
![Page 12: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/12.jpg)
helper functions
Helper functions:
• provision_ansible(): support running from Windowshost
• network_options(): parse Yaml config to Ruby argumentlist
• custom_synced_folders()
![Page 13: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/13.jpg)
Workflow
1 atb-init testbox bertvv.el7 bertvv.httpd• Download scaffolding code from Github into directorytestbox/
• Optionally, installs roles from Ansible Galaxy• Initialise Git repo & initial commit
![Page 14: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/14.jpg)
Workflow (cont’d)
2 Edit vagrant-hosts.yml
# vagrant-hosts.yml---- name: testbox
ip: 192.168.56.56
![Page 15: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/15.jpg)
Workflow (cont’d)
3 Assign role in ansible/site.yml
# ansible/site.yml---- hosts: testbox
sudo: trueroles:
- bertvv.el7- bertvv.http
4 vagrant up
![Page 16: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/16.jpg)
Code
• https://github.com/bertvv/ansible-skeleton• https://github.com/bertvv/ansible-toolbox• https://github.com/bertvv/ansible-role-skeleton
Need something fancier? See James Shubin’s Oh My Vagranthttps://github.com/purpleidea/oh-my-vagrant
![Page 17: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/17.jpg)
Other stuff
• Twitter: @bertvanvreckem• Blog: https://bertvv.github.io/notes-to-self/• Ansible roles (CentOS 7):https://galaxy.ansible.com/bertvv/
![Page 18: One vagrantfile to rule them all](https://reader034.vdocuments.us/reader034/viewer/2022042600/5876fe3f1a28abf3398b6c61/html5/thumbnails/18.jpg)
Thanks for listening!
Talk to me about system administration andeducation!