286

Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

  • Upload
    others

  • View
    24

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 2: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 3: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingDevelopmentEnvironmentswithVagrantSecondEdition

Page 4: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

TableofContents

CreatingDevelopmentEnvironmentswithVagrantSecondEdition

Credits

AbouttheAuthor

AbouttheReviewers

www.PacktPub.com

Supportfiles,eBooks,discountoffers,andmore

Whysubscribe?

FreeaccessforPacktaccountholders

Preface

Whatthisbookcovers

Whatyouneedforthisbook

Whothisbookisfor

Conventions

Readerfeedback

Customersupport

Downloadingtheexamplecode

Errata

Piracy

Questions

1.GettingStartedwithVagrant

IntroducingVagrant

RequirementsforVagrant

Gettingstarted

InstallingVirtualBox

InstallingVagrant

Summary

2.ManagingVagrantBoxesandProjects

CreatingourfirstVagrantproject

ManagingVagrant-controlledguestmachines

Page 5: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PoweringupaVagrant-controlledvirtualmachine

Suspendingavirtualmachine

Resumingavirtualmachine

Shuttingdownavirtualmachine

Startingfromscratch

UpdatingbasedonVagrantfilechanges

ConnectingtothevirtualmachineoverSSH

Managingintegrationbetweenhostandguestmachines

Portforwarding

Syncedfolders

Networking

Autorunningcommands

ManagingVagrantboxes

AddingVagrantboxes

ListingVagrantboxes

Checkingforupdates

RemovingVagrantboxes

RepackagingaVagrantbox

Updatingthecurrentenvironment’sbox

ToomanyVagrants!

Summary

3.ProvisioningwithPuppet

Provisioning

Puppet

CreatingmodulesandmanifestswithPuppet

Puppetclasses

DefaultPuppetmanifests

Resources

Resourcerequirements

Resourceexecutionordering

Thenotify,subscribe,andrefreshonlyparameters

Page 6: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Executingresourcesinstages

Installingsoftware

Updatingourpackagemanager

Installingthenginxpackage

Runningthenginxservice

Filemanagement

Copyingafile

Creatingasymlink

Creatingfolders

Creatingmultiplefoldersinonego

Cronmanagement

Runningcommands

Managingusersandgroups

Creatinggroups

Creatingusers

Updatingthesudoersfile

Creatingconfigurableclasses

Puppetmodules

UsingPuppettoprovisionservers

Summary

4.UsingAnsible

UnderstandingAnsible

InstallingAnsible

Creatinganinventory

CreatingAnsibleplaybooks

Modules–whatAnsiblecando

Installingsoftware

Updatingourpackagemanager

Installingthenginxpackage

Runningthenginxservice

Understandingfilemanagement

Page 7: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Copyingafile

Creatingasymlink

Creatingfolders

Managingcron

Runningcommands

Managingusersandgroups

Creatinggroups

Creatingusers

UsingAnsibleroles

UsingAnsibletoprovisionservers

Summary

5.UsingChef

KnowingaboutChef

CreatingcookbooksandrecipeswithChef

Resources–whatChefcando

Installingsoftware

Updatingourpackagemanager

Installingthenginxpackage

Runningthenginxservice

Understandingfilemanagement

Copyingafile

Creatingasymlink

Creatingfolders

Creatingmultiplefoldersinasingleprocesswithlooping

Managingcron

Runningcommands

Managingusersandgroups

Creatinggroups

Creatingusers

Updatingthesudoersfile

Knowingcommonresourcefunctionalities

Page 8: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingChefcookbooks

UsingCheftoprovisionservers

Summary

6.ProvisioningVagrantMachineswithPuppet,Ansible,andChef

ProvisioningwithinVagrant

ProvisioningwithPuppetonVagrant

UsingPuppetinstandalonemode

Puppetprovisioninginaction

UsingPuppetinclient/servermode

ProvisioningwithAnsibleonVagrant

ProvisioningwithChefonVagrant

UsingChef-solo

UsingChefinclient/servermode

ProvisioningwithSSH–arecap

Usingmultipleprovisionersonasingleproject

Overridingprovisioningviathecommandline

Summary

7.WorkingwithMultipleMachines

UsingmultiplemachineswithVagrant

Definingmultiplevirtualmachines

ConnectingtothemultiplevirtualmachinesoverSSH

Networkingthemultiplevirtualmachines

Provisioningthemachinesseparately

Destroyingamultimachineproject

Summary

8.CreatingYourOwnBox

Gettingstarted

PreparingtheVirtualBoxmachine

VirtualBoxGuestAdditions

Vagrantauthentication

Vagrantuserandadmingroup

Page 9: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Thesudoersfile

Insecurepublic/privatekeypair

Provisioners

InstallingPuppet

InstallingChef

CleaninguptheVM

Export

Summary

9.HashiCorpAtlas

Discoveringboxes

Installingnewboxes

Updatingexistingboxes

Checkingforoutdatedboxes

Distributingboxes

SharingandconnectingwithAtlas

LoggingVagrantintoVagrantCloud

SharingaVagrantvirtualmachineoverHTTP(S)

SharingandconnectingtoaVagrantvirtualmachine

Summary

A.ASampleLEMPStack

CreatingtheVagrantproject

CreatingthePuppetmanifests

InstallingNginx

InstallingPHP

InstallingtheMySQLmodule

Defaultmanifest

InstallingNginxandPHP

Hostnameconfiguration

E-mailsendingservices

MySQLconfiguration

Launchingthevirtualmachine

Page 10: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Summary

Index

Page 11: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 12: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingDevelopmentEnvironmentswithVagrantSecondEdition

Page 13: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 14: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingDevelopmentEnvironmentswithVagrantSecondEditionCopyright©2015PacktPublishing

Allrightsreserved.Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformorbyanymeans,withoutthepriorwrittenpermissionofthepublisher,exceptinthecaseofbriefquotationsembeddedincriticalarticlesorreviews.

Everyefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyoftheinformationpresented.However,theinformationcontainedinthisbookissoldwithoutwarranty,eitherexpressorimplied.Neithertheauthor,norPacktPublishing,anditsdealersanddistributorswillbeheldliableforanydamagescausedorallegedtobecauseddirectlyorindirectlybythisbook.

PacktPublishinghasendeavoredtoprovidetrademarkinformationaboutallofthecompaniesandproductsmentionedinthisbookbytheappropriateuseofcapitals.However,PacktPublishingcannotguaranteetheaccuracyofthisinformation.

Firstpublished:August2013

Secondedition:March2015

Productionreference:1050315

PublishedbyPacktPublishingLtd.

LiveryPlace

35LiveryStreet

BirminghamB32PB,UK.

ISBN978-1-78439-702-9

www.packtpub.com

Page 15: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 16: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreditsAuthor

MichaelPeacock

Reviewers

JonathanBardo

AnirudhBhatnagar

CommissioningEditor

UshaIyer

AcquisitionEditors

RichardBrookes-Bland

EllenBishop

ContentDevelopmentEditor

SriramNeelakantan

TechnicalEditor

MrunalM.Chavan

CopyEditor

RashmiSawant

ProjectCoordinator

AboliAmbardekar

Proofreaders

SimranBhogal

MariaGould

PaulHindle

Indexer

MonicaAjmeraMehta

ProductionCoordinator

ArvindkumarGupta

CoverWork

ArvindkumarGupta

Page 17: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 18: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

AbouttheAuthorMichaelPeacockisanexperiencedsoftwaredeveloperandteamleadfromNewcastle,UK,withadegreeinsoftwareengineeringfromtheUniversityofDurham.

Afterspendinganumberofyearsrunninghisownwebagency,andsubsequently,workingdirectlyforanumberofsoftwarestart-ups,henowrunshisownsoftwaredevelopmentagency,workingonarangeofprojectsforanarrayofdifferentclients.

HeistheauthorofCreatingDevelopmentEnvironmentswithVagrant,PHP5SocialNetworking,PHP5E-CommerceDevelopment,Drupal7SocialNetworking,andSellingonlinewithDrupale-CommerceandBuildingWebsiteswithTYPO3,allbyPacktPublishing.TheotherpublicationsMichaelhasbeeninvolvedinincludeAdvancedAPISecurity,MobileWebDevelopment,JenkinsContinuousIntegrationCookbook,andDrupalforEducationandE-Learning;fortheseheactedasatechnicalreviewer.

Michaelhasalsopresentedatanumberofusergroupsandtechnicalconferences,includingPHPUKConference,DutchPHPConference,ConFoo,PHPNE,PHPNW,andCouldConnectSantaClara.

YoucanfollowMichaelonTwitter(@michaelpeacock),orfindoutmoreabouthimthroughhiswebsite(www.michaelpeacock.co.uk).

I’dliketothanktheteamatPacktPublishingfortheirhelpingettingthisrevisededitionofthebookpublished,andthetechnicalreviewersforensuringtechnicalaccuracyinthebook.

Page 19: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 20: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

AbouttheReviewersJonathanBardoisaMontreal-basedwebdeveloperwithakeeninterestfornewtechnologiesandautomation.Hehasworkedformanylarge-scalewebsitesdealingwithmillionsofdailyvisitorsonvariousplatforms.Whenheisnotprogramming,helikestowatchagoodTVshowortravelsomewherehehasneverbeenbefore.Ifyouseehimridinghismotorcycleorskiingdownahill,justsayhi!Heisveryfriendly!

Jonathanrunshisownconsultingcompany,whichletshimmeetallsortsofinterestingclients,suchasFoxBroadcasting(USA),RogersDigitalMedia(Canada),andYellowPagesGroup(Canada).

Aspecialthankstoeveryonewhohasbeenapartofmyjourneysofar!Iwouldn’tbeherewithoutalltheincrediblepeopleIworkedwitheveryday.

AnirudhBhatnagarisaprincipalconsultantatXebia.Hestartedhiscareerasadeveloperworkinginproduct-basedcompaniessuchasAdobe.

AnirudhhasbeenworkingmostlywithJava-basedtechnologystacksthatuseSpring,Hibernate,XML,webservices,REST,CMS,SSO,ESB,andLiferay.

Duringthelastfewyears,AnirudhhasbeenadvocatingContinuousDeliveryandisinterestedintechnologiessuchasChef,Puppet,Jenkins,Vagrant,Docker,andmanymore.Heregularlycontributestothecommunityviablogs,articles,meetups,conferences,andopensourceprojects.

Moredetailsabouthimcanbefoundonhisblog(http://anirudhbhatnagar.com).

Page 21: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 22: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

www.PacktPub.com

Page 23: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Supportfiles,eBooks,discountoffers,andmoreForsupportfilesanddownloadsrelatedtoyourbook,pleasevisitwww.PacktPub.com.

DidyouknowthatPacktofferseBookversionsofeverybookpublished,withPDFandePubfilesavailable?YoucanupgradetotheeBookversionatwww.PacktPub.comandasaprintbookcustomer,youareentitledtoadiscountontheeBookcopy.Getintouchwithusat<[email protected]>formoredetails.

Atwww.PacktPub.com,youcanalsoreadacollectionoffreetechnicalarticles,signupforarangeoffreenewslettersandreceiveexclusivediscountsandoffersonPacktbooksandeBooks.

https://www2.packtpub.com/books/subscription/packtlib

DoyouneedinstantsolutionstoyourITquestions?PacktLibisPackt’sonlinedigitalbooklibrary.Here,youcansearch,access,andreadPackt’sentirelibraryofbooks.

Page 24: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Whysubscribe?FullysearchableacrosseverybookpublishedbyPacktCopyandpaste,print,andbookmarkcontentOndemandandaccessibleviaawebbrowser

Page 25: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

FreeaccessforPacktaccountholdersIfyouhaveanaccountwithPacktatwww.PacktPub.com,youcanusethistoaccessPacktLibtodayandview9entirelyfreebooks.Simplyuseyourlogincredentialsforimmediateaccess.

Page 26: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 27: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PrefaceWeb-basedsoftwareprojectsareincreasinglycomplicated,witharangeofdifferentdependencies,requirements,andinterlinkingcomponents.Swappingbetweenprojects,whichrequiredifferentversionsofthesamesoftware,becomestroublesome.Gettingteammembersupandrunningonnewprojectsbecomestime-consuming.

Vagrantisapowerfultoolusedtocreate,manage,andworkwithvirtualizeddevelopmentenvironmentsforyourprojects.Bycreatingavirtualenvironmentforeachproject,theirdependenciesandrequirementsareisolated,theyalsodon’tinterferewiththesoftwareinstalledonyourownmachinesuchasWAMPorMAMP.Colleaguescanbeupandrunningonanewprojectinminuteswithasinglecommand.WithVagrant,wecanwipetheslatecleanifwebreakourenvironmentandbebackupandrunninginnotime.

Page 28: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

WhatthisbookcoversChapter1,GettingStartedwithVagrant,introducestheconceptofvirtualization,itsimportanceintheroleofthedevelopmentenvironment,andwalksyouthroughtheVagrantinstallationprocess.

Chapter2,ManagingVagrantBoxesandProjects,walksyouthroughcreatingVagrantprojects,exploringandconfiguringtheVagrantfile,andworkingwithbaseboxes.

Chapter3,ProvisioningwithPuppet,exploresPuppet,theprovisioningtool,andhowtocreatePuppetmanifeststoprovisionaserver.

Chapter4,UsingAnsible,exploresAnsible,theprovisioningtool,andhowtocreateAnsibleplaybookstoprovisionaserver.

Chapter5,UsingChef,exploresChef,theprovisioningtool,andhowtocreateChefrecipestoprovisionaserver.

Chapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,discusseshowtousePuppet,Ansible,andChefwithinthecontextofVagranttoprovisiondevelopmentenvironments.

Chapter7,WorkingwithMultipleMachines,exploresusingVagranttocreateandmanageprojectsthatusemultiplevirtualmachines,whichcommunicatewitheachother.

Chapter8,CreatingYourOwnBox,discussestheprocessofcreatingyourownbaseboxforusewithinaVagrantproject.

Chapter9,HashiCorpAtlas,walksyouthroughusingVagrantSharetoshareSSHandHTTP(S)accesstoaVagrant-managedmachine,andhowtousetheservicesprovidedthroughtheVagrantCloud.

Appendix,ASampleLEMPStack,walksyouthroughtheprocessofcreatingaLEMPserverwithinanewVagrantproject.

Page 29: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 30: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

WhatyouneedforthisbookYouwillneedaWindows,OSX,orLinuxcomputerwithVagrantandOracleVirtualBoxinstalled,althoughtheinstallationprocessforthesewillbediscussedinChapter1,GettingStartedwithVagrant.

Page 31: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 32: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

WhothisbookisforThisbookisforsoftwaredevelopers,developmentmanagers,andtechnicalteamleaderswhowanttohaveamoreefficient,robust,andflexibledevelopmentenvironmentfortheirprojectsandfortheirteam.

Page 33: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 34: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ConventionsInthisbook,youwillfindanumberofstylesoftextthatdistinguishbetweendifferentkindsofinformation.Herearesomeexamplesofthesestyles,andanexplanationoftheirmeaning.

Codewordsintext,databasetablenames,foldernames,filenames,fileextensions,pathnames,dummyURLs,userinput,andTwitterhandlesareshownasfollows:“AfterinstallingVagrant,weranthevagrantcommandtocheckwhetheritwasinstalledcorrectly.”

Ablockofcodeissetasfollows:

VAGRANTFILE_API_VERSION="2"

Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|

config.vm.box="base"

end

Whenwewishtodrawyourattentiontoaparticularpartofacodeblock,therelevantlinesoritemsaresetinbold:

---

-hosts:default

tasks:

-name:updateaptcache

apt:update_cache=yes

-name:ensurenginxisinstalled

apt:pkg=nginxstate=present

-name:writethenginxconfigfile

template:src=nginx-default-site.confdest=/etc/nginx/sites-

available/default.conf

notify:

-restartnginx

-name:ensurenginxisrunning

service:name=nginxstate=started

handlers:

-name:restartnginx

service:name=nginxstate=restarted

Anycommand-lineinputoroutputiswrittenasfollows:

ansible-playbookour-playbook.yml-iour-inventory-file

Newtermsandimportantwordsareshowninbold.Wordsthatyouseeonthescreen,inmenusordialogboxesforexample,appearinthetextlikethis:“Again,onOSX,thefirststepistodouble-clickontheVagrant.pkgicon.”

NoteWarningsorimportantnotesappearinaboxlikethis.

TipTipsandtricksappearlikethis.

Page 35: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 36: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ReaderfeedbackFeedbackfromourreadersisalwayswelcome.Letusknowwhatyouthinkaboutthisbook—whatyoulikedormayhavedisliked.Readerfeedbackisimportantforustodeveloptitlesthatyoureallygetthemostoutof.

Tosendusgeneralfeedback,simplysendane-mailto<[email protected]>,andmentionthebooktitleviathesubjectofyourmessage.

Ifthereisatopicthatyouhaveexpertiseinandyouareinterestedineitherwritingorcontributingtoabook,seeourauthorguideonwww.packtpub.com/authors.

Page 37: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 38: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CustomersupportNowthatyouaretheproudownerofaPacktbook,wehaveanumberofthingstohelpyoutogetthemostfromyourpurchase.

Page 39: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

DownloadingtheexamplecodeYoucandownloadtheexamplecodefilesforallPacktbooksyouhavepurchasedfromyouraccountathttp://www.packtpub.com.Ifyoupurchasedthisbookelsewhere,youcanvisithttp://www.packtpub.com/supportandregistertohavethefilese-maileddirectlytoyou.

Page 40: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ErrataAlthoughwehavetakeneverycaretoensuretheaccuracyofourcontent,mistakesdohappen.Ifyoufindamistakeinoneofourbooks—maybeamistakeinthetextorthecode—wewouldbegratefulifyouwouldreportthistous.Bydoingso,youcansaveotherreadersfromfrustrationandhelpusimprovesubsequentversionsofthisbook.Ifyoufindanyerrata,pleasereportthembyvisitinghttp://www.packtpub.com/submit-errata,selectingyourbook,clickingontheerratasubmissionformlink,andenteringthedetailsofyourerrata.Onceyourerrataareverified,yoursubmissionwillbeacceptedandtheerratawillbeuploadedonourwebsite,oraddedtoanylistofexistingerrata,undertheErratasectionofthattitle.Anyexistingerratacanbeviewedbyselectingyourtitlefromhttp://www.packtpub.com/support.

Page 41: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PiracyPiracyofcopyrightmaterialontheInternetisanongoingproblemacrossallmedia.AtPackt,wetaketheprotectionofourcopyrightandlicensesveryseriously.Ifyoucomeacrossanyillegalcopiesofourworks,inanyform,ontheInternet,pleaseprovideuswiththelocationaddressorwebsitenameimmediatelysothatwecanpursuearemedy.

Pleasecontactusat<[email protected]>withalinktothesuspectedpiratedmaterial.

Weappreciateyourhelpinprotectingourauthors,andourabilitytobringyouvaluablecontent.

Page 42: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

QuestionsYoucancontactusat<[email protected]>ifyouarehavingaproblemwithanyaspectofthebook,andwewilldoourbesttoaddressit.

Page 43: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 44: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter1.GettingStartedwithVagrantDevelopingmodernweb-basedapplicationscanbecomplicated!

Thetechnologybehindourprojectsisbecomingmoreadvancedanddiverse.Whereonceprojectsranwithsimplyawebserver,adatabase,andasetprogramminglanguage,nowweusetoolsbuiltinavarietyofdifferentlanguages.Weusecomponentsanddependenciesthatneedtobeinstalled,andtheirmanagedversions,andoftenprojectsneedtorunacrossmultiplemachines.

Differentprojectshavetheirownrequirementsanddependencies,whichareoftenincompatiblewithoneanother.AlegacyprojectmightrequireaspecificversionofPHPorspecificversionsofextensionsforApache,whereasanotherprojectmightrequireanewerversionofPHPandrunningonNginx.Projectswitchinginthisscenarioisnoteasy.

Often,weneedtoworkwithteamsofpeople,someofwhichmightbeusingtheirownequipment,workingremotely,andcontractors.Thisrequiresyoutoensurethateveryonerunsthesamedevelopmentenvironment,regardlessoftheirownsystemanditsconfiguration,theinfrastructurechangesforprojectsaretrackedandmadeavailabletotheteam,andtheprojectsetupisfastfornewteammembers.

Combiningthesethreefactorsandsettinguptraditionaldevelopmentenvironmentsisbecomingmoredifficult,lessrelevant,andlesshelpfulfordevelopers.

Asprojectsgetmorecomplicated,it’salsoeasyforauxiliaryconfigurationstobeforgottenabout.Backgroundworkers,messagequeues,cronjobs,andmultiserverconfigurationsneedtobemanaged,distributedtotheentireteam,andthenwhenthetimecomes,appliedtotheprojectwhenitgetsdeployedintoaproductionenvironment.

Virtualizeddevelopmentenvironmentscanhelpwiththis.Insteadofhavingtobattleconfigurationswhenworkingonotherprojects,eachprojectcansimplyhaveitsownvirtualizedenvironment.Itcanhaveitsowndedicatedwebserver,databaseserver,andtheversionsoftheprogramminglanguageandotherdependenciesitneeds.Becauseitisvirtualized,itdoesn’timpactonotherprojects;justshutitdownandbootuptheenvironmentfortheotherproject.

Withavirtualizedenvironment,thedevelopmentenvironmentscanalsomimictheproductionenvironment.Youdon’tneedtoworryaboutwhethersomethingwillworkwhenitgetsdeployed,ifitisbeingdevelopedonamachinewiththeexactsamesoftwareconfiguration.EvenifyoudeployonaLinuxmachinebutdeveloponWindows,yourvirtualizedenvironmentcanbeLinux,runningthesamedistributionasyourproductionenvironment.

Whileavirtualizedenvironmentmakesdifferentprojectsandtheirdependencieseasiertomanageandseparate,theyarenottheeasiestofthingstoconfigureandmanage.Theystillneedtobeconfiguredtoworkwiththeprojectinquestion,whichofteninvolvessomelevelofsystemadministrationskills,andweneedtoconnecttotheseenvironmentsandworkwiththem.Theyalso,bydesign,arenotveryportable.Youneedtoexportalarge

Page 45: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

imageofthevirtualizedenvironmentandsharethatwithyourcolleagues,andkeepingthatimageuptodateasprojectsevolvecanbecumbersome.Thankfully,thereisatoolthatcanmanagethesevirtualizedenvironmentsforus,andprovideasimpleinterfacetoconfigurethem;aninterfacethatinvolvesstoringconfigurationsinsimpleplaintextfiles,whichareeasytosharewithcolleagues,keepingeveryoneuptodateastheprojectchanges.ThistoolisVagrant.

Page 46: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

IntroducingVagrantVagrant(http://www.vagrantup.com/)isapowerfuldevelopmenttoolthatletsyoumanageandsupportthevirtualizationofyourdevelopmentenvironment.Insteadofrunningallyourprojectslocallyonyourowncomputer,havingtojugglethedifferentrequirementsanddependenciesofeachproject,Vagrantletsyouruneachprojectinitsowndedicatedvirtualenvironment.

Vagrantprovidesacommand-lineinterfaceandacommonconfigurationlanguagethatallowsyoutoeasilydefineandcontrolvirtualmachinesthatrunonyourownsystems,butwhichtightlyintegrate,andalsoallowsyoutodefinehowyourownmachineandthevirtualmachineinteract.Thiscaninvolvesyncingfolderssuchthattheprojectcode,whichyoueditusingtheIDEonyourcomputer,issyncedsothatitrunsontheVagrantdevelopmentenvironment.

Vagrantusesproviderstointegratewiththethird-partyvirtualizationsoftware,whichprovidesthevirtualizedmachinesforourdevelopmentenvironment.ThedefaultproviderisforOracleVirtualBox;however,therearecommercialproviderstoworkwithVMwareFusionandalsopluginsforVagranttoworkwithAmazonWebServices.Theentireconfigurationisstoredinsimpleplaintextfiles.TheVagrantconfiguration(Vagrantfile),andtheconfigurationthatdefineshowourVagrantmachinesareconfigured(typicallyShellscripts,Ansibleplaybooks,ChefcookbooksorPuppetmanifeststhatVagranthasbuilt-insupportfor,asprovisioners)aresimplywrittenintextfiles.Thismeansthatwecaneasilysharetheconfigurationsandprojectswithcolleagues,usingversioncontrolsystemssuchasGit.

WhenusingVagrant,thenexttimeyouneedtogobacktoapreviousproject,youdon’tneedtoworryaboutanypotentialconflictswithchangesmadetoyourdevelopmentenvironment(forexample,ifyouhaveupgradedPHP,MySQL,orApacheonyourlocalenvironmentorwithintheVagrantenvironmentforanotherproject),asthedevelopmentenvironmentfortheseprojectsarecompletelyself-contained.Ifyoubringanewmemberintotheteam,theycanbeupandrunningwithyourprojectsinminutes.Vagrant,alongwithitsintegrationwithprovisioners,willtakecareofallthesoftwareandservicesneededtoruntheprojectontheirmachine.IfyouhaveoneprojectthatusesonewebserversuchasApache,andanotheronethatusesNginx,Vagrantletsyouruntheseprojectsindependently.Ifyourproject’sproductionenvironmentinvolvesmultipleservers(perhapsonefortheWebandoneforthedatabase),Vagrantletsyouemulatethatwithseparatevirtualserversonyourmachine.

WithVagrant:

YourdevelopmentenvironmentcanmimictheproductionenvironmentIntegratedprovisioningtoolssuchasPuppet,Chef,andAnsibleallowyoutostoretheconfigurationinastandardformat,whichcanalsobeusedtoupdateproductionenvironmentsEachprojectisseparateinitsownvirtualizedenvironment,soissuesasaresultofconfigurationandversiondifferencesfordependenciesondifferentprojectsarea

Page 47: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

thingofthepastNewteammemberscanbeonboardedtonewprojectsaseasyasgitclone&&vagrantup

“Itworksonmymachine”asaresponsetobugsisathingofthepastTheheadacheoflinkingcodethatyouwriteonyourownmachinetoyourvirtualizeddevelopmentenvironmentistakencareofthroughsyncedfoldersTheenvironmentcanactasifitwasyourlocalmachineandmapthewebserverport(80)ofyourdevelopmentmachinetoyourdevelopmentenvironmentifyouwish,oryoucanaccessitasyouwouldanothermachineonyournetworkYoucanletcolleaguesviewyourowndevelopmentenvironmentaswellaseasilysharethedevelopmentenvironmentYoucanshareaccesstoyourowndevelopmentenvironmentovertheInternettodemoyourprojectortogetsupportfromacolleagueYourlocalWAMPorMAMPinstallationswillbegatheringdust!

Inthischapter,wewillcoverthefollowingtopics:

DiscusstherequirementsandprerequisitesforVagrantInstallOracleVirtualBoxInstallVagrantVerifythatVagrantwassuccessfullyinstalled

OncewehaveVagrantanditsprerequisitesonourmachine,wecanthentakealookatusingitforourfirstproject.

Page 48: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 49: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RequirementsforVagrantVagrantcanbeinstalledonLinux,Windows,andMacOSX,andalthoughitusesRuby,thepackageincludesanembeddedRubyinterpreter.TheonlyotherrequirementisavirtualizationprovidersuchasOracleVirtualBoxorVMwareFusion.TheOracleVirtualBoxproviderisavailableforfreeandisthedefaultproviderforVagrant.So,wewilluseandinstallVirtualBoxinordertouseVagrantduringthecourseofthisbook.Otherprovidersareavailable,includingoneforVMwareFusionorWorkstation,whichisavailableasacommercialadd-on(http://www.vagrantup.com/vmware).

Page 50: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 51: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

GettingstartedNowthatweknowwhatsoftwareweneedinordertogetVagrantrunningonourmachine,let’sstartinstallingVirtualBoxandVagrantitself.

Page 52: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingVirtualBoxVirtualBox(https://www.virtualbox.org/)isanopensourcetoolsponsoredbyOraclethatletsyoucreate,manage,andusevirtualmachinesonyourowncomputer.

VirtualBoxisagraphicalprogramwithacommand-lineinterfacethatletsyouvisuallycreatevirtualmachines,allocateresources,loadexternalmediasuchasoperatingsystemCDs,andviewthescreenofthevirtualmachine.Vagrantwrapsontopofthisandprovidesanintuitivecommand-lineinterfacealongwiththeintegrationofadditionaltools(includingintegrationswithprovisionersandalsoHashiCorpAtlas(formerly,VagrantCloud)thatallowyoutofindanddistributebaseserverimagesandshareaccesstoyourVagrantenvironments),sothatwedon’tneedtoworryabouthowVirtualBoxworksorwhattodowithit;Vagranttakescareofthisforus.

ThefirststageistodownloadtheinstallerfromtheVirtualBoxdownloadspage(https://www.virtualbox.org/wiki/Downloads),asshowninthefollowingscreenshot.Weneedtoselecttheoptionthatisappropriateforourcomputer(OSX,Windows,Linux,orSolaris):

NoteAtthetimeofwritingthis,Vagrantsupportsversions4.0.xthrough4.3.xofVirtualBox;earlierversionsarenotsupported.

Oncedownloaded,let’sopenitandruntheinstaller.OnOSX,thisinvolvesclickingontheVirtualBox.pkgicon,asshowninthefollowingscreenshot.OnWindows,simplyopeningtheinstalleropenstheinstallationwizard.OnLinux,therearepackagesavailablethatcanbeinstalledthroughyourchosenpackagemanager,seehttps://www.virtualbox.org/wiki/Linux_Downloadsformoreinformation.

Page 53: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Beforetheinstallerruns,itfirstcheckswhetherthecomputeriscapableofhavingVirtualBoxinstalled.WeneedtoclickonContinuetobegintheinstallationprocess,asshowninthefollowingscreenshot.WhilethisprocesswillvaryfromOSXtoWindowstoLinux,theprocessisverysimilaracrossallplatforms.TherearefullydetailedinstallationinstructionsforallplatformsontheVirtualBoxwebsite(https://www.virtualbox.org/manual/ch02.html).

Page 54: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Thefirststepintheprocessprovidesuswithanintroductiontotheinstallationprocessandremindsusastowhatweareactuallyinstalling:

Next,theinstallerinformsusastohowmuchspaceitwilluseonourcomputer,andprovidesuswiththeoptiontocustomizetheinstallationifwewanttoChangeInstallLocation…,andinstallthesoftwareinanotherlocation(perhapsanotherdiskdriveifourdiskgetsfull).

Let’sleavethedefaultinstalllocationasitis,andclickontheInstallbuttontoinstallVirtualBoxonourcomputer:

Page 55: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Afterbeingpromptedtoprovideadministrativeprivileges,theinstallerthenautomaticallyinstallsVirtualBoxforus:

Page 56: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Oncetheinstallationhasfinished,weareshownaconfirmationscreenwiththeoptionofclickingonClosetoclosetheinstaller:

NowwehavesuccessfullyinstalledVirtualBox!

Page 57: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingVagrantNowthatwehavetheprerequisitesinstalledonourcomputer,wecanactuallyinstallVagrantitself.ThisprocessissimilartothatofinstallingVirtualBox.First,let’sdownloadtherelevantinstallerfromtheVagrantdownloadpage(http://www.vagrantup.com/downloads.html):

Let’sopentheinstallerandstarttheprocess.Again,onOSX,thefirststepistodouble-clickontheVagrant.pkgicon:

Page 58: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Wenowneedtofollowtheinstallationstepsthatareprovided;thisisverysimilartotheearlierstepsforVirtualBox,andformostofthesoftwarepackagesingeneral.Youmightbepromptedtoprovideyourcomputer’sadministrativeuserprivilegesforthesoftwaretobeinstalled.

Let’sverifythatVagranthasbeensuccessfullyinstalled.Wecandothisbyopeningaterminalwindow(cmdonWindows)andrunningthevagrantcommand:

Page 59: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

TheprecedingscreenshotshowsthatwehavesuccessfullyinstalledVagrant,andweareabletorunit.

Runningthevagrantcommandonitsownlistsarangeofcommonsubcommands,whichwecanrunwithinVagrant,aswellasinstructionsonhowtoaccessthehelpinformationonVagrantandanyofitssubcommands.WecanaccessthehelpinformationonVagrantanditssubcommandsbyaddingthehflag,-h,totheendofthecommandwhenwerunit.

Page 60: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 61: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,wediscussedthebenefitsofusingvirtualizeddevelopmentenvironmentsandspecifically,Vagrant.WetheninstalledOracleVirtualBox,whichisthevirtualizationproviderVagrantusesbydefault,andweinstalledVagrant.AfterinstallingVagrant,weranthevagrantcommandtocheckwhetheritwasinstalledcorrectly.

NowthatwehaveVagrantandaproviderinstalled,wecannowmoveontousingVagranttosetupandmanagesomeofourdevelopmentprojectsinavirtualdevelopmentenvironment.Inthenextchapter,wewillcreateourfirstproject,learnabouttheconfigurationfile,andmanageourVagrantcontrolledmachines.

Page 62: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 63: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter2.ManagingVagrantBoxesandProjectsInthischapter,wewilllearnthebasicsofusingVagrant.Wewilltakealookatinitializingprojects,importingbaseboxestobeusedasouroperatingsystem,andcontrollingthevirtualmachinebypoweringonandoff,suspendingandresuming,andconnectingtothebox.Finally,wewillalsolearnhowtoconfiguresomeofthekeyintegrationpointsbetweenourownmachineandourVagrant-controlledvirtualmachine,including:

PortforwardingFoldermappingNetworking

Page 64: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingourfirstVagrantprojectNowthatwehaveVagrantinstalledonourmachine,let’stakealookatcreatingVagrantprojects.AnyfoldercanactasaVagrantproject;itonlyrequiresaspecialconfigurationfile,calledtheVagrantfile,withinit.Vagrantusesthisfiletosetupthevirtualmachines(guests)andmanagetheirintegrationwithourcomputer(orhostmachine).

VagranthasacommandtocreateaVagrantfilewithinthecurrentdirectoryyouarein,withinyourcomputer’sterminal:thevagrantinitcommand.Tocreateanewproject,let’screateanewfolderanywhereinoursystem–ideally,somewhereeasilyreachable,thenweneedtogointothisfolder,andruntheinitcommand:

VagrantwillthencreateaVagrantfilewithinthatfolder,andshowusaconfirmationmessage:

IfwetakealookatthecontentsofthisVagrantfilefilethatwascreated,wewillseearangeofconfigurationoptions.Mostoftheoptionsarecommentedout(astheyareprefixedwitha#character)togiveusanideaofhowwecanconfiguretheproject.

Thereareonlyfourlinesofactualusableconfigurationinthefile,asshownhere:

VAGRANTFILE_API_VERSION="2"

Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|

config.vm.box="base"

end

TipDownloadingtheexamplecode

Page 65: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Youcandownloadtheexamplecodefilesfromyouraccountathttp://www.packtpub.comforallthePacktPublishingbooksyouhavepurchased.Ifyoupurchasedthisbookelsewhere,youcanvisithttp://www.packtpub.com/supportandregistertohavethefilese-maileddirectlytoyou.

DifferentversionsofVagrantusedifferentstructuresandinstructionswithintheirVagrantfilefiles.TomakeVagrantbackwardcompatible,thevarioussettingsarewrappedinablockofcodethatcontainstheversionoftheconfigurationtobeused.ConfigurationVersion2iscompatiblewithVagrantVersion1.1uptoVersion2.0.x(whenreleased).

Withinhere,wehaveasingleoption—thetypeofVagrantboxtouse.AVagrantboxisanimageofanoperatingsystemthatisconfiguredtoworkwithVagrant.

WecangenerateaVagrantfilewithouttheillustrativecommentsbyprovidingtheminimaloption(-mor--minimal,forexample,vagrantinit--minimal).

InordertobootourVagrantmachine,werunthevagrantupcommand:

AsVagrantdoesn’tknowwhattheboxbaseis,ourprojectisunabletoboot.WhileVagrantprojectshavetheirconfigurationstoredwithintheirprojectsfolder,boxesareinstalledgloballyonyourcomputerandcanbesharedacrossprojects.IfVagrantdoesn’thaveaboxinstalledwiththatname,itwilleithertrytodownloaditfromtheURLprovidedintheVagrantfile(ifthereisoneprovided)oritwilllookfortherelevantboxinHashiCorpAtlas(formerlyVagrantCloud).HashiCorp,thecompanybehindVagrant,providesofficialboxesforthelatestLongTermSupportversionofUbuntu(thisboxishostedonanddistributedthroughVagrantCloud;wewilldiscussthisinmoredetailinChapter9,HashiCorpAtlas).Tousethisbox,wesimplypassthenameoftheboxtotheinitcommand(orupdatetheVagrantfile).AswealreadyhaveaVagrantfileinplaceforourproject,weneedtousetheforceoptiontooverridetheexistingfile(-for--force):

vagrantinit--forcehashicorp/precise64

NowifwetrytorunourVagrantproject,Vagrantwilllookforthebox,andthistime,itwilldownloadtheprecise64boxfromVagrantCloud,foruseinourproject:

Page 66: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Typically,boxesareminimalinstallationsofoperatingsystemsthatcontainonlywhatisrequiredfortheoperatingsystemtofunction,toolstointegratewithVagrant,andaminimalamountofothertools.ThisprovidesgreaterflexibilitywhenitcomestousingVagranttomanageprojects,aswecandecideexactlywhatsoftwareweneedtorunonourvirtualmachineforourprojecttofunction,withouthavingtoworryabouttheconflictingversionsofthesaidsoftware.Someexistingboxesmayhavemoresoftwareinstalled,andofcourse,wemaywanttopackageaboxthatcontainssomeofthekeysoftwareourprojectsneed(particularly,usefulasabackupforuserswithunreliableInternetconnections).

NoteWhileweareusingcommandstoinitializeourVagrantprojectsinthischapter,thesearesimplyquickwaystocreateaVagrantfilefilewithsomeprepopulatedvalues.AVagrantfilefileistheconfigurationfilethatdefineshowVagrantshouldusetheproject(suchasoperatingsystemtobeused,virtualmachinestobootup,syncedfolders,forwardedports,andsoon).Wecan,ofcourse,createthisfileourselveswithinthefolderwewishtouseasourproject.

Page 67: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 68: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingVagrant-controlledguestmachinesThevirtualmachines,whichVagrantcontrolsforus,stillneedtobemanagedandworkedwith.WehaveseenthatwecanstartaVagrantprojectwithvagrantup.Let’slearnmoreaboutthiscommand,andseehowtoperformotheroperationsonournewvirtualmachine.

Page 69: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PoweringupaVagrant-controlledvirtualmachineAswehavejustseen,wecanpowerupavirtualmachineusingthevagrantupcommand.Withthiscommand,VagrantwillfirstcheckwhetheraVagrantenvironmenthasalreadybeensetup.Ifapreviouslysuspendedenvironmentisfound,itwillresumethatenvironment.

Iftheenvironmentwasnotpreviouslysuspended,Vagrantthencheckswhetherthebaseboxhasalreadybeendownloadedontothemachine.Ifithasn’t,itwilldownloadit,asitdidforuswhenwebootedourprojectwiththeprecise64boxforthefirsttime.

Vagrantwillthenperformthefollowingactions:

1. Copythebasebox(remember,baseboxesaremanagedgloballyonourhostcomputer,soittakesacopyforeachmachinemanagedbyit).

2. Createanewvirtualmachinewiththerelevantprovider(thedefaultbeingVirtualBox).

3. Forwardanyconfiguredports;bydefault,itwillforwardport22(theSSHport)onthevirtualmachinetoport2222onthehost.ThiswillallowustoconnecttothevirtualmachineoverSSH.

4. Boot(powerup)thevirtualmachine.5. Configureandenablenetworking,sothatwecancommunicatewiththevirtual

machine.6. Mapsharedfoldersbetweenthehostandtheguest(bydefault,itwillmapthefolder

thatcontainstheVagrantprojectto/vagrantontheguestmachine).7. RunanyprovisioningtoolsthataresetupsuchasPuppet,Chef,orSSHcommands

orscripts.

TheactionsperformedbyVagrantwilllooksomethinglikethis:

Page 70: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 71: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SuspendingavirtualmachineWecansavethecurrentstateofthevirtualmachinetothedisk(suspendit)sothatwecanresumeitlater.Ifwerunvagrantsuspend,itwillsuspendtheVMandstopitfromconsumingourmachine’sresources,exceptforthediskspaceitwilloccupy,readyforustouseagainlater:

Page 72: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ResumingavirtualmachineInordertouseapreviouslysuspendedvirtualmachine,wesimplyrunvagrantresume:

Page 73: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ShuttingdownavirtualmachineWecanshutdownarunningvirtualmachineusingthevagranthaltcommand.ThisinstructstheVMtostopallrunningprocessesandshutdown.Touseitagain,weneedtorunvagrantup,whichwillpoweronthemachine;provisioningistypicallyonlyranonthefirstvagrantupcommand.Toensurethatprovisioningrunswhenwebootupasavedmachinesubsequently,wecanusethe--provisionflag.

Page 74: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

StartingfromscratchSometimes,thingsgowrong.It’snotinconceivablethatwemightmakesomechangestoourvirtualmachine,andfindoutthatitnolongerworks.Thankfully,sincewehaveabasebox,configurationfile,andprovisioningfiles,whichareallstoredseparately,wecaninstructVagranttodestroyourvirtualmachine,andthencreateitagain,usingtheconfigurationstosetitup.Thisisdoneviathedestroycommand,andthenweneedtousetheupcommandtostartitagain:

vagrantdestroy

vagrantup

Ofcourse,ifweupdateourVagrantfile,provisioningmanifests,orapplicationcodethatcanalsobreakthings;soitisimportantthatweuseaversioncontrolsystemtoproperlymanageourproject’scodeandconfiguration,sothatwecanundothechangestheretoo;Vagrantcanonlydosomuchtohelpus!

Page 75: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UpdatingbasedonVagrantfilechangesIfwemakechangestoourVagrantfile,thesechangeswon’tapplyuntilwenextshutdownandpoweronourvirtualmachine.Asthisisn’tveryconvenient,thereisahandyreloadcommandthatwillshutdownthemachine,reloaditsconfigurationbasedontheVagrantfileasitcurrentlyis,andbootitupagain:

vagrantreload

Runningthiscommandyieldsthefollowingresult:

Page 76: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ConnectingtothevirtualmachineoverSSHIfwerunthevagrantsshcommand,VagrantwillthenconnecttothevirtualmachineoverSSH.Alternatively,wecanSSHtolocalhostwithport2222,andthiswilltunnelintothevirtualmachine,usingthedefaultforwardedSSHport.

IfwerunVagrantonaWindowsmachine,wewon’thaveabuilt-inSSHclient.WecanuseaclientsuchasPuTTYtoconnecttoVagrant.PuTTYcanbedownloadedfromhttp://www.chiark.greenend.org.uk/~sgtatham/putty/.MoreinformationonhowtoconfigurePuTTYtoworkwithVagrantisavailableontheVagrantwebsite(http://docs-v1.vagrantup.com/v1/docs/getting-started/ssh.html).

Page 77: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 78: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingintegrationbetweenhostandguestmachinesWithoutanyformofintegrationbetweenthehostmachineandtheguest,wewouldsimplyhaveabarebonesvirtualserverrunningontopofourownoperatingsystem,whichisnotparticularlyuseful.Weneedourownmachinetobecapableofintegratingtightlywiththeguest(virtualmachine).

Page 79: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PortforwardingAlthoughthevirtualmachineisrunningonourownmachine,becauseofvirtualization,itactsandbehaveslikeacompletelydifferentmachine.Sometimes,thisiswhatwewant;however,theremightbetimeswewanttohavethevirtualmachinebehavealmostasanextensionofourownmachine.Onewaytodothisisthroughportforwarding,wherewecantunnelaportfromthevirtualmachinetoaportonthehostmachine.If,forexample,wehaveawebserverrunningonourownmachine,weobviouslydon’twanttomapthewebserverportfromVagrantontothesameport;otherwise,therewouldbeaconflict.Instead,wecanmapittoanotherport.Ifwemapthewebserverportonthevirtualmachinetoport8888onthehost,thennavigatingtohttp://localhost:8888onourownmachinewouldshowusthewebservicewerunontheguest,despitethefactthatthelocalhostreferstoourhostmachine.

TheportforwardingisdonevialinesintheVagrantfilefile;wesimplyprovidetheguestandhostportswewishtomap:

config.vm.network:forwarded_port,guest:80,host:8888

IfwehaveotherVagrantmanagedvirtualmachinesonourcomputer,whichwewishtorunsimultaneously,wecanenableauto_correctonspecificports.Thisway,ifaconflictisfound(forexample,twovirtualmachinestryingtomaptothesameport),onevirtualmachinewilltryadifferentportinstead:

,auto_correct:true

Portsbelowacertainrangeneedelevatedorrootprivilegesonthehostmachine,soyoumaybeaskedforyouradministrativepassword.

Page 80: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SyncedfoldersSyncedfoldersallowustoshareafolderbetweenthehostandtheguest.Bydefault,VagrantsharesthefolderthatcontainstheVagrantproject/vagrantonthevirtualmachine.WecanusethefollowingcommandinourVagrantfiletosyncmorefoldersifwewish:

config.vm.synced_folder"/Users/michael/assets/""/var/www/assets"

ThefirstparameteristhepathtothefolderonourmachineandthesecondisthemountpointontheVM.Ifweusearelativepathonourmachine,itwouldberelativetotheprojectfolder.

Ifwewanttooverridethedefaultsyncedfolder,wecandothistoo:

config.vm.synced_folder".","/var/another/folder"

TheNetworkFileSystem(NFS)cangiveusbetterperformancewithsyncedfoldersthanthedefaultsettings.Thiswon’thaveanyeffectonWindowshosts,andonLinux/OSX,hostswillrequirerootprivileges.WecanenableNFSonapersyncedfolderbasisbyaddingthefollowingcommandtotheprecedingline:

,type:"nfs"

Page 81: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

NetworkingBydefault,ourVagrantvirtualmachinesareonlyaccessiblefromthemachineswerunVagranton,andothermachinesinournetworkwon’tbeabletoaccessthem.Ifwemapportstoourhost,thenwecansharetheservicesrunningonthevirtualmachinewithourcolleagueswithinournetwork.IfwewanttoallowourcolleaguestoaccessourVagrantmanagedvirtualmachinesdirectly,wecanattachthevirtualmachinetoourinternalnetwork,andVirtualBoxwillbridgethenetworkbetweenourmachineandthevirtualmachine,andtheinternalnetworkbetweenourmachineandtherestofthemachinesinourhomeoroffice.

config.vm.network"private_network",ip:"10.11.100.200"

ThisapproachisalsousefulwhenwantingtohavemultipleVagrantprojectsrunningatthesametime;iftheyarewebprojects,theycanallexposeport80,butondifferentIPaddresses,andifwewant,wecanmapthesetothehostnamesinourhostsfile.

NoteThehostsfileisafileonacomputerthatmapsadomainnametoanIPaddress.ThiscanbeusedtopreventthecomputerfromhavingtolookuptheIPaddressforadomainandisusefulforlocallyhostedsites,aswecanmanuallylinktheIPaddresstoadomainname,justforourlocalmachine.OnOSXandLinux,thehostsfileisstoredas/etc/hosts,andonWindowsitisstoredasC:\Windows\System32\Drivers\etc\hosts.

Ifwewanttoshareaccesstoourvirtualmachineordemosomethingrunningonit,wecanuseVagrantSharethroughVagrantCloud,whichwewilldiscussinChapter9,HashiCorpAtlas.

ItisalsopossibletohavetheIPaddressassignedviaDHCP(typically,thiswillmeanthatyournetwork’srouterwillassignitanIPaddress):

config.vm.network"private_network",type:"dhcp"

Page 82: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 83: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

AutorunningcommandsOneofthekeyconceptswithinVagrantisprovisioning.Thisinvolvesturningabasicvirtualmachinewithabaseoperatingsystemintoaserverthatisreadytorunforyourproject,meetingyourrequirements.Togofromthebaseoperatingsystemtoafullyfledgedmachine,weneedtouseaprovisioningtooltoinstallthesoftwareandconfigurethemachine.ThereareanumberofkeyprovisioningoptionswithinVagrant:

ShellPuppetAnsibleChef

Puppet,Ansible,andChefareallthird-partytoolsthatVagrantsupportsoutofthebox,andprovidespecificlanguagestoconfigureserversinanagnosticwaythatcanbeusedfordifferentoperatingsystems.Thenextthreechapterswilldiscussthesetoolsinmoredetail.Vagrantalsosupportssomeotherprovisioningtools,includingSalt,Docker,andCFEngine.

SSHprovisioninginvolvesrunningaseriesofcommandsonthevirtualmachineoverSSHwhenthemachineisfirstsetup.

TherearetwokeywaysinwhichwecanuseSSHprovisioning.WecaneitherdirectlyrunacommandfromourVagrantfileorwecanrunthecontentsofascript.

ThefollowinglineinourVagrantfilewillruntheinlinecommandprovided:

config.vm.provision"shell",inline:"sudoapt-getupdate"

Alternatively,wecantellVagranttorunaparticularshellscript(thelocationofthescriptspecifiedisrelativetoourprojectroot,thatis,/vagrant):

config.vm.provision"shell",path:"provision.sh"

Thisshellscriptcouldcontainallofthecommandsweneedtoconvertabaseboxintoabox,whichsupportsourprojectandapplication,perhapsinstallingwebanddatabaseservers.

Page 84: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 85: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingVagrantboxesWecanmanageVagrantboxesusingthevagrantboxcommand.Let’srunthiscommandwiththehelpflag(--help)andseewhatsubcommandsareavailable:

vagrantbox--help

Runningthiscommandgivesthefollowingresult:

Therearesixavailablebox-relatedsubcommands.Witheachofthese,wecanprovidethe--helpflagtoseewhatadditionalargumentsareavailable.Theavailablebox-relatedsubcommandsare:

add:Thiscommandaddsanewboxlist:Thiscommandlistsallboxesinstalledoutdated:Thiscommandcheckswhetheranyboxeshaveupdatesavailableremove:Thiscommandremovesaboxfromthehostrepackage:ThiscommandconvertsaVagrantenvironmentintoadistributableboxupdate:ThiscommandwillupdatetheboxbeingusedbythecurrentrunningVagrantenvironment

Page 86: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

AddingVagrantboxesTheaddsubcommandallowsustoaddanewbox.Ittakesasingleargumentandanumberofoptionalflags.Theargumentisaname,URL,orpathtoaboxfile.Ifanameisprovided,VagrantwilldownloadtheboxfromVagrantCloud.IfweprovideaURLorpathtoaboxstoredelsewhere,weneedtogiveVagrantanametouse.Thisisprovidedwiththe--nameoptionalflag.

Someotheroptionalflagsthatmightbeusefulinclude:--force,whichwilltellVagranttoremoveapre-existingboxwiththesamename;--clean,whichwilltellVagranttocleananytemporarydownloadedfiles;and--provider,whichallowsustospecifyanotherprovidertobackthebox(thedefaultproviderbeingVirtualBox,however,thereareprovidersavailableforVagrant,includingVMwareandAmazonEC2).

Thefollowingcommandwilladdanewpacktbox,andifanexistingoneisfound,itwilloverrideit:

vagrantboxadd––forcepackthttp://our-server.vagrant/packt.box

Theprocessofaddingaboxmaytakeawhile,asmostVagrantboxeswillbeatleast200MBbig.Oncedownloaded,theboxwillbeextractedandavailableforustouseinourVagrantprojects,asweobservedwhenwestartedourfirstprojectearlierinthechapter.

Page 87: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ListingVagrantboxesThelistsubcommandwilllisttheboxesinstalledwithinVagrant,alongwiththeproviderthatbacksthebox:

vagrantboxlist

Runningthiscommandgivesthefollowingoutput:

Page 88: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CheckingforupdatesBoxes,whichareprovidedbyVagrantCloud,mayberegularlyupdated;wecanusetheoutdatedsubcommandtoseewhetherthereareupdatesavailable:

Page 89: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RemovingVagrantboxesWecanremovetheboxwiththeremovesubcommand.Weneedtoprovidethenameoftheboxtoberemoved.Optionally,wecanalsospecifytheproviderandtheversionoftheboxtoberemovedwiththe--providerand--box-versionflags,respectively.Thefollowingexamplewillremoveourprecise64boxforVirtualBox:

vagrantboxremovehashicorp/precise64--providervirtualbox

Runningthiscommandgivesthefollowingoutput:

Page 90: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RepackagingaVagrantboxTherepackagesubcommandletsusconvertaVagrantenvironmentcompletewithanycustomizationswehavemadetoit,suchassoftwarewehaveinstalledonit,intoaboxthatwecanreuseanddistributetoothers.WewillusethiscommandinChapter8,CreatingYourOwnBox.

Page 91: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Updatingthecurrentenvironment’sboxWecanusetheupdatesubcommandtoupdatetheboxinuseonthecurrentVagrantenvironment:

vagrantboxupdate

Alternatively,wecanupdateaspecificbox,whichisn’ttiedtothecurrentenvironment,usingthe--boxflagtoprovidethenameofthebox(andthe--provideroptiontooifwewish).

Page 92: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 93: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ToomanyVagrants!OncewestartusingVagrantonarangeofprojects,thelackofaGUIcanmakeiteasytolosetrackofwhichprojectsarerunningorsuspendedonyourmachine.Thisisespeciallyannoyingwhenyouwanttobootupanewproject,butanexistingVagrantprojectiseithercausingaconflictorconsumingtoomanyresourcesonyourmachine.Thankfully,thereisnowacommandtolistallactiveVagrantenvironmentsonyourhost,forexample,

ThiscommandliststheIDs,names,providers,andstatesofourVagrantprojectsaswellasthedirectorytheyarerunningin:

WecanappendtheIDtotheendofthevagrantcommandtorunthecommandagainstthatmachine,withouthavingtogointothatfolder,forexample:

vagrantsuspend77e5115

Page 94: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 95: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,wecreatedprojectswithVagrant,importedabaseboxtouse,andbootedourVagrantenvironment.WealsolookedatthecommandsneededtomanagetheseboxesandtheVagrantvirtualmachines.WelookedathowwecanconfigureourVagrantenvironmentwithnetworking,syncedfolders,andforwardedports,andhowtoprovisionsoftwareonourvirtualmachinewithSSHcommands.WhenitbecomesaproblemtohavemultipleVagrantprojectsrunning,wenowknowhowtolocatetheserunningprojectswiththeglobal-statuscommand.

Inthenextchapter,wewilltakealookathowtousePuppet,oneoftheprovisioningtoolssupportedbyVagrant.Wewillcoverinstallingandconfiguringservices,managingfilesandfolders,runningcommands,andmanagingusersandscheduledtasks.

Page 96: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 97: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter3.ProvisioningwithPuppetVagrantisaverypowerfultoolprimarilybecauseofthefollowingkeyconceptsitcanmanageforus:

VirtualizationProvisioningBoxdistributionSharing

InChapter1,GettingStartedwithVagrantandChapter2,ManagingVagrantBoxesandProjects,welearnedtouseVagranttomanagevirtualmachinesforus.Whilethisisuseful,atthisstage,thesevirtualmachinesaredumb;theyhaveverylittlesoftwareinstalledforustouse,andtheyarecertainlynotconfiguredforourprojects.

TherearetwoapproacheswecanusetosetupaVagrant-managedvirtualmachinewithallthesoftwarerequiredforaproject:

UseabaseboxthatispreconfiguredwiththesoftwareordevelopmentstackthatwerequireProvisiontheexactsoftwareandconfigurationthatwerequireusingaprovisioningtool

Preconfiguredbaseboxesareusefulandhavetheirplace.IfwewerealwaysusingaspecificconfigurationorwewerecreatingaVagrantenvironmentforanopensourceprojectwewerereleasing,aconfiguredboxmightbethebestoption.Inthatinstance,aconfiguredbaseboxwillquicklygetusersupandrunningontheproject.Thedownsideisthatitisn’teasytochangetheconfigurationastheneedsoftheprojectchange,andcertainelementssuchascronjobsandbackgroundworkerswouldstillneedtobeconfiguredseparately.

Provisioning,however,automatestheprocessofturningabasemachineintoonethatisconfiguredforusewithaspecificproject.

Inthischapter,wewillquicklytakealookatthebasicsofPuppet,oneofthevariousprovisioningoptionsavailablewithinVagrant.Wewon’tlookatitwithinaVagrantcontextjustyet;wewillsimplytakealookathowaPuppetworks,andhowwecanuseit.InChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,wewilltakealookathowtoconnectwhatwewilllearninthischapterwithVagrantitself.Inthischapter,wewilllearnthefollowingtopics:

HowPuppetworksThebasicsbehindPuppetmodulesandmanifestsHowtousePuppettoperformthefollowingtasks:

InstallsoftwareManagefilesandfolderswithinthefilesystemManagecronjobsRuncommands

Page 98: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Manageusersandgroups

CreatingconfigurableclassesHowtousethird-partyPuppetmodulesandPuppetForgeHowtomanuallyrunPuppettoprovisionamachine

Puppetitselfisalargetopicandthesubjectofseveralbooks.ForamoredetailedlookatPuppet,PacktPublishinghassometitlesdedicatedtoit:

Puppet2.7Cookbook,JohnArundel(http://www.packtpub.com/puppet-2-7-for-reliable-secure-systems-cloud-computing-cookbook/book)Puppet3:BeginnersGuide,JohnArundel(http://www.packtpub.com/puppet-3-beginners-guide/book)

Page 99: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningWithinthiscontext,provisioningistheprocessofsettingupavirtualmachinesothatitcanbeusedforaspecificpurposeorproject.Typically,thisinvolvesinstallingsoftware,configuringthesoftware,managingservicesrunningonthemachine,andevensettingupusersandgroupsonthemachine.

Foraweb-basedsoftwareproject,provisioningwilllikelyentailtheinstallationofawebserver,aprogramminglanguage,andadatabasesystem.Configurationchangeswillbeneededtosetupadatabaseonthedatabasesystemandtoallowthewebservertowritetospecificfolders(todealwithuseruploads).

Withoutthisprovisioningprocess,wewouldhaveanalmostvanillainstallofanoperatingsystem,whichcontainsasyncedcopyofourprojectfolder;thisvanillainstallwouldn’tbeusableasadevelopmentenvironmentforourproject.Provisioningtakesustothenextlevelandgivesusafullyworkingenvironmentforourproject.

Page 100: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 101: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PuppetPuppetisaprovisioningtoolthatwecanusetosetupaserverforuseforaproject.TheconfigurationthatdetermineshowtheserverneedstobesetupcanbestoredwithinourVagrantprojectandcanbesharedwithteammatesthroughaversioncontrol,ensuringeveryonegetsanup-to-datecopyoftherequireddevelopmentenvironment.

Informationabouthowaservershouldbeconfigured,thatis,itssoftware,files,users,andgroups,iswrittenintofilesknownasthePuppetmanifests.ThesemanifestsarewrittenusingPuppet’sownlanguage,whichisaRubydomain-specificlanguage.Puppettakesthisinformationandcompilesitintoacatalogthatisspecificfortheoperatingsystemitisbeingappliedto.Thecatalogisthenappliedtothemachine.

Forourpurposes,wewillusePuppetinstandalonemode(thisisalsohowVagrantusesit).Standalonemodemeansthateverythingrunsfromonemachine.Puppetalsohasclient-servercapabilities,whereyoucandefinethePuppetmanifestsforalltheserversinyourinfrastructure,onacentralhost,anditkeepsyourindividualserversattherequiredlevelofconfiguration.

Puppetisidempotent,whichmeansrunningPuppetonamachinemultipletimeshasthesameeffectasrunningitonlyonce.Ineffect,Puppetensuresthatconditionsaremet,andiftheyarenot,itwillperformactionstoensurethattheyaremet,forexample,PuppetwouldinstallNginxifitwasn’talreadyinstalled.Ifitwasalreadyinstalled,itwoulddonothing.ThismeanswecanreprovisionwithPuppetmanytimeswithoutanydetrimentaleffect.ThisisusefulaswecanuseittokeeptheserverinsyncwithourPuppetmanifestsiftheyweretochange.

Page 102: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingmodulesandmanifestswithPuppetPuppetismadeupofamanifestfileandanumberofmodules(whichalsocontainmanifestsandotherresources).Thedefaultmanifestspecifieswhichmodulesaretobeused,anddependingonthemodule,providescustomizationoptionsforit(forexample,thePuppetmoduleforsupervisord(http://supervisord.org/),aprocesscontrolsystem,allowsustospecifyanynumberofprocessesthatshouldbemanagedusingsupervisordthroughthemoduleitself).

ModulesmakeuseofresourceswithinPuppettocontrolandconfigurethemachine,andthesemodulescanbeimportedtoruninaspecificsequence,throughstages.

PuppetclassesPuppetmodulestypicallyconsistofclasses,which,inturn,utilizeanumberofresourcetypes(inthisexample,thepackageresourcetype,toinstallasoftwarepackage)toachieveaspecificrequirementforourserver.Iteffectivelyallowsustobundleanumberoftheseresourcetypesinaway,whichmeanswecansimplyincludetheclassbyitsname,andhavealloftheinstructionsexecutedfromwithinit.

Aclassinitsmostbasicformisstructuredasfollows:

classnginx{

package{"nginx":

ensure=>present,

require=>Exec['apt-getupdate']

}

}

ForitsmostbasicusewithinVagrant,classessuchasthesewillbesavedasdefault.ppwithinthemodules/nginx/manifests/folder.Theclasscancontainmanyresourcetypestoachieveadesiredgoal(forinstance,installingtheApachepackageisn’tthesameaspreparingthewebserverfullyforaproject,relatedtaskscanbebundledintothesameclass).

DefaultPuppetmanifestsForagivenproject,Puppetmodulesaretypicallyalllocatedinaspecificmodulesdirectory.Manymodulescanbecustomizedwhentheyarerun,anexamplebeingthesupervisordmodule;itsimplyprovidesthestructureforustocustomizeforeachprocesswewantittomanage.

Becauseofthis,weneedtohaveadefaultPuppetmanifestthatincludesalistofmodulestoberunandanyconfigurationsforthem.BecausePuppetisawareofourmodulefolderlocationwhenwerunit(andwhenitisrunthroughVagrant),wejustlistthemodulestobeincludedandrun.

Abasicmanifestthatwillincludeandrunthenginxclasswewroteearlierwouldbeasfollows:

import"nginx"

Page 103: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

includenginx

Imentionedthesupervisormodule(https://github.com/plathrop/puppet-module-supervisor)afewtimesasamodulethatisdesignedtobeusedformultipledifferentthings,whichcanbecustomizedbythedeveloperusingit.

NoteSupervisordisthenameofthesoftware,however,thePuppetmodulewearegoingtousetomanagesupervisordiscalledsupervisor(no“d”)—sowatchoutforthat!

Supervisordisatoolthatmaintainsanumberofrunningprocesses,forexample,ifyouhaveabackgroundworkerinawebapplicationtoresizeimages,thesupervisormightberesponsibleforkeepingfiveworkersrunningatanyoneinstance,respawningthemwhenonehasfinished.ThefollowingisanexampleofhowthismodulewouldbeusedinadefaultPuppetmanifest:

supervisor::service{

'resize_images':

ensure=>present,

command=>'/usr/bin/php/vagrant/app/consoleimg:resize',

user=>'root',

group=>'root',

autorestart=>true,

startsecs=>0,

num_procs=>5,

require=>[Package['php5-cli'],Package['beanstalkd']];

}

supervisor::service{

email':

ensure=>present,

command=>'/usr/bin/php/vagrant/app/consoleemail',

user=>'root',

group=>'root',

autorestart=>true,

startsecs=>0,

num_procs=>5,

require=>[Package['php5-cli'],Package['beanstalkd']];

}

Here,weareinstructingPuppettousethesupervisordmoduletwicetosetupandmanagetwoworkersforus.Foreachoftheworkers,wehaveasetoffiveprocessestoberun,andwehavesettheuserandgrouptorunthem.WehavedefinedPHP’scommand-lineinterfaceandthebeanstalkdworkerqueueasrequirementsfortheworkers.ThisillustratesthepowerthatPuppetmoduleshave.

ResourcesPuppetprovidesarangeofresourcetypesthatwecanutilizetocreateourconfigurationfiles.Theseresourcetypesaretranslatedandcompileddependingontheoperatingsystembeingused.Forexample,ifweweretousethepackageresourcetypetoinstallsomesoftware,thiswoulduseapt-getonUbuntuandYumonFedoraoperatingsystems.A

Page 104: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

smallnumberofresourcetypesareoperatingsystemspecific,forexamplethescheduled_taskresourcetypeisdesignedspecificallyforWindows,andthecrontypeisdesignedforLinuxandUnix-basedsystems.

Resourcetypesavailableinclude:

Cron:ThisresourcetypeisusedtomanagecronjobsonLinux-andUnix-basedsystemsExec:Thisresourcetypeisusedtoruncommandsattheterminal/commandpromptFile:ThisresourcetypeisusedtomanageandmanipulatefilesandfoldersonthefilesystemGroup:ThisresourcetypeisusedtomanageusergroupsPackage:ThisresourcetypeisusedtoinstallsoftwareService:ThisresourcetypeisusedtomanagerunningservicesonthemachineUser:Thisresourcetypeisusedtomanageuseraccountsonthemachine

Whenresourcetypesareuseddirectly(forexample,weusethePackageresourcetypetoinstallsomesoftware),theyareusedinlowercase(package).However,whenwerefertoaresourcetypewehaveused,forexample,asarequirementforanotherPuppetaction,wereferencethemwithaleadingcapitalletter(Package).

Anexampleofthisisasfollows:

package{"nginx":

ensure=>present,

require=>Exec['apt-getupdate']

}

WetellPuppettoinstallthenginxpackage(lowercase“p”forpackage),butwhenwespecifytherequirementofapreviouslyexecutedexeccommand,weusealeadingcapitalletter.TheoptionswithinthisinstructionforPuppet(ensureandrequirekeywords)arecalledparameters.

AfulllistofresourcetypesisavailableonthePuppetwebsiteathttp://docs.puppetlabs.com/references/latest/type.html.

Whenusingaresourcetype,anameisprovided(intheprecedinginstance,thisisnginx),thisisoftendualpurpose,servingbothasawayforustoreferencetheaction(inthiscase,thepackagebeinginstalled)andalsoasaninstruction(inthiscase,whatpackagePuppetneedstoinstall).WhenitcomestotheExecresourcetype,thenameisthecommandwewishtorun.Bydefault,weneedtoprovidethefullpathtothecommandthatwerun.Wecanavoidthisbyprovidingthepathfromwhichthecommandshouldberunasaparameter.

Resourcerequirements

CertainthingsthatwedowithPuppetwillrequireotheractionstohavebeenperformedfirst.Thesecanbedefinedusingtherequireparameter,andwecanspecifymultiplerequirements.

IfweneedtorunorinstallsomethingafterboththeMySQLServerandtheMySQLclient

Page 105: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

packageshavebeeninstalled,wewillusetherequireparametertodefinethemasfollows:

require=>[Package['mysql-client'],Package['mysql-server']]

ThisdefinesanarrayofmultiplerequirementsasadependencyforourPuppetcode.

ResourceexecutionorderingSometimes,weneedtorunspecificblocksofthePuppetcodebeforeotherblocks.Inmostcases,wecanusetherequire,notify,andsubscribeparameterstogetaroundthisproblem.

Page 106: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Thenotify,subscribe,andrefreshonlyparametersSometimes,wewanttohaveaPuppetcommandrunmultipletimeswhenothercommandshavefinished.Oneexampleistorestartthenginxservice.Wewillperformthefollowingsteps:

1. Importanewconfigurationfile.2. Addnewvirtualhosts.

Wecanusethenotifyparametertoinstructonecommandtotriggeranothertoberun.Ineffect,thisnotifiesthenextcommandtotellitthattherehavebeenchangesmadeelsewhere,whichrequiresthatcommandtonowrun.

Inthefollowingexample,werequireaPuppetmanagedconfigurationfiletobecopiedtoourPuppetmanagedmachine.ThecoderequiresNginxtobeinstalledbeforeitisrun,andafterthefilehasbeencopiedacross,itwillnotifythenginxservicetoberestarted.Importantly,thisnotificationwillberuneachtimethefilechanges,butwon’tbetriggeredwhenitrunswherethefileisunchanged:

file{'/etc/nginx/sites-available/default':

source=>"puppet:///modules/nginx/default",

owner=>'root',

group=>'root',

notify=>Service['nginx'],

require=>Package['nginx']

}

Thiscanbealsobeachievedusingthesubscribeandrefreshonlyparameters,whichworkintheoppositewaytonotify.Thesubscribeparameterinstructsthecommandtoruneverytimeanyofthecommandsinthesubscribeoptionhavebeenrun.

Therefreshonlyparameter,whensettotrue,instructsthecommandtoonlyrunwhenoneofthecommandsitsubscribestohasrun(leavingthisoutwouldmeanthecommandisalsoruninitsownright):

service{"nginx":

refreshonly=>false,

subscribe=>File['/etc/nginx/sites-available/default'],

}

Here,thecommandtoreloadNginxwillonlyberunwhenthenewconfigurationfilehasbeenloaded.Wecan,ofcourse,extendthesubscribeparametertocontainotherthingssuchasmodulesandotherconfigurations,asdiscussedearlier.

TipOnlyservice,exec,andmountresourcetypescanberefreshed.

ExecutingresourcesinstagesWhererequire,notify,andsubscribearenotsuitableforourusecase,wecanusestages.Puppethasadefaultstage,withinwhichallcommandsrun.Wecancreateourown

Page 107: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

stages,whichrunbeforeorafterthisstagethatallowustoforcecommandstoberuninspecificorders.

WecandefinestageswithinourdefaultPuppetmanifestandtheninstructPuppettoruncertainclassesfromwithinthatstage.If,forexample,wewantedtorunourNginxclassbeforeanythingelse,wecancreateastagetorunfirst,andputtheNginxclasswithinthatstageasfollows:

stage{'first':before=>Stage[main]}

class{'nginx':stage=>first}

Thiscreatesastagecalledfirst,andanythingassignedtothisstagewillbeexecutedbeforethedefaultPuppetstage;next,itplacestheNginxclasswithinthatstage.Oncewehaveanamedstage,suchasfirst,wecanthencreateotherstages,whichcanrunbeforethisonetoo.

NoteStagesareusefulwhenyouneedtogrouptheorderingofcertaintasks,however,theycannormallybeavoidedthroughtheproperuseofrequire,notify,andsubscribe,whichshouldbeusedinsteadwherepossible.

Page 108: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingsoftwareLet’ssaywewanttoinstallNginxonourserver.Therearethreetypicalstepsinvolvedinthisprocess:

1. Updatingourpackagemanager.2. Installingthenginxpackage.3. Runningthenginxservice.

Becausethefirststepisdifferent,dependingontheoperatingsystemwearerunning,wewouldwanttoeithermovethisoutofPuppetatalaterstageorlookatusingamoduletoabstractitout,however,wewilluseitwithinPuppetforthetimebeing.Anyoperatingspecificcommands(suchasthese)arewrittenforUbuntu,whichistheoperatingsystemweareusingwithVagrant.IfyouarenotusingUbuntu,theExeccommandshouldberewrittentoupdatethepackagemanageronyoursystem.

NoteThisexampleispurelytoillustratetheprocessofputtingtogetherasimplemodule.TherearemanyexistingmodulesavailableonPuppetForge,whichwewillcometolater.

UpdatingourpackagemanagerInordertoupdateourpackagemanager,weneedtorunacommandontheserver.ThiscanbeachievedusingtheExecresourcewithinPuppet:

exec{'apt-getupdate':

command=>'/usr/bin/apt-getupdate',

timeout=>0

}

ThisinstructsPuppettoruntheapt-getupdatecommand.Wehavesetatimeoutofzerosothatifittakesawhile(andafterafreshinstallationofanoperatingsystemthroughVagrant,itmight),Puppetwillrunitforaslongasittakes,overridingthedefaulttimeout.

NoteThisisn’tthemostelegantofapproaches,especiallywithitbeingoperatingsystemspecificandsubsequentlyarequirementformostofourothercommands.InAppendix,ASampleLEMPStack,wewillbuildaLEMPserverprojectwithVagrantandPuppet,andintheexample,weuseVagrant’sSSHprovisioningoptionstoupdatethepackagemanagerbeforeweinstalltheothersoftware.Mostbaseboxesdon’thaveup-to-datepackagedetailstosavespaceandduetotheirage,soupdatingthepackagemanagerisrequired.

InstallingthenginxpackageWecanusethePackageresourcetoensurethatNginxisinstalled,andifitisn’t,itwillbeinstalledasfollows:

package{"nginx":

Page 109: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ensure=>present,

require=>Exec['apt-getupdate']

}

Here,wetoldPuppettoensurethatthenginxpackageispresent.Weaddedourapt-getupdatecommandasaprerequisite,soweknowthatourpackageswillbeuptodate.

RunningthenginxserviceFinally,tomakesurethatNginxisrunning,weusetheServiceresourcetoensurethatthenginxserviceisrunning.Obviously,thiscan’tberunifNginxisn’tinstalled,sothenginxpackageisaprerequisite:

service{"nginx":

ensure=>running,

require=>Package['nginx']

}

Page 110: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

FilemanagementWecanusetheFileresourcewithinPuppettomanagefilesandfolderswithinthefilesystem.Let’stakealookatsomeexamples,whichallowusto:

CopyfilesCreatesymlinksCreatefoldersCreatemultiplefoldersinonego

CopyingafileOnecommonfileoperationwemightwanttoperformwouldbetotakeaconfigurationfilefromourprojectandcopyitintoourvirtualmachine.OneparticularusecasewouldbeanNginxconfigurationfile;wemightwanttodefinesomevirtualhostsandothersettingsinafile,whichwecansharewithourcolleagues.

TipWhilethisworkswellandcangetusupandrunningquickly,therearemodulesouttherethatallowustoconfigureNginxandothertypesofsoftwaredirectlyfromPuppet.Thistypicallyworksbythemoduleofstoringatemplatefile(acopyoftheconfigurationfilewithplaceholdersinit)andthen,insertingdatathatwedefinewithinPuppetintothetemplate,andcopyingthefileontothemachine.However,forthesakeofthisintroductorychapter,wewilljustcopyafileacross.

Thefileresourcetypeallowsustocreatefiles,folders,andsymlinks.Inordertocreateafile(orreplacethecontentsofanexistingfilewithanotherfile),wesimplytellPuppetwhatfilewewanttocreateorreplace(thedestination),thesource(thatis,thefiletobecopiedandputintothedestination),andtheuserandgroupwhoshouldownthefile:

file{'/etc/nginx/sites-available/default':

source=>'puppet://modules/nginx/default',

owner=>'root',

group=>'root',

require=>Package['nginx']

}

AsthisisanNginxconfigurationfile,itisworthensuringthatNginxisalreadyinstalled;otherwise,Nginxwilloverridethiswhenitinstallsthefirsttimeandthiswouldn’tmaketheprocessidempotent.

TipHere’ssomethingtonoteaboutfilelocations:thesourcefileintheprecedingfileresourcecodeisheldwithinaVagrantenvironmentandthePuppetmoduleitself.Wecanprovidetwokindsoffilepaths:eitherthefullpathtothefileonthemachine,whichPuppetisrunningon(ourVagrantenvironment),suchas/vagrant/path/to/defaultorapathrelativetoPuppetmodules.ThesePuppetpathsarestructuredlikethis:puppet:///modules/nginx/default.Thedifferenceyouwillnoteisthatitautomaticallylooksforinthefiles/folderwithinthenginxfolder;wedon’tneedtospecifythat.

Page 111: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingasymlinkIfweomitthesourceparameterandinsteadaddanensureparameter,andsetthattolink,wecancreateasymlink.Atargetisusedtodefinewherethesymlinkshouldpointto,asshowninthefollowingcode:

file{'/var/www/vendor':

ensure=>'link',

target=>'/vagrant/vendor',

require=>Package['nginx']

}

CreatingfoldersSimilartotheprecedingsymlinkcode,thistime,wesimplyneedtosetensuretoadirectory.Thiswillthencreateadirectoryforusasfollows:

file{"/var/www/uploads":

ensure=>"directory",

owner=>"www-data",

group=>"www-data"

mode=>777,

}

Wecanusethemodeparametertosetthepermissionsofthefolder(thisalsocanbeusedforfileswecreateandmanage).

CreatingmultiplefoldersinonegoInmanywebprojects,wemightneedtocreateanumberoffolderswithinourserversorourVagrantvirtualmachines.Inparticular,wemightwanttocreateanumberofcachefoldersfordifferentpartsofourapplication,orwemightwanttocreatesomeuploadfolders.

Inordertodothis,wecansimplycreateanarraythatcontainsallofthefolderswewanttocreate.Wecanthenusethefileresourcetypeandpassthearraytocreatethemall,asfollows:

$cache_directories=[

"/var/www/cache/",

"/var/www/cache/pages",

"/var/www/cache/routes",

"/var/www/cache/templates",

]

file{$cache_directories:

ensure=>"directory",

owner=>"www-data",

group=>"www-data",

mode=>777,

}

Page 112: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CronmanagementThecronresourcetypeletsususePuppettomanagecronjobs,whichweneedtorunonthemachine.Weprovideaname,inthiscase,web_cron,thecommandtorun,theusertorunthecommandas,andthetimesatwhichtorunthecommand,asshowninthefollowingcode:

cron{web_cron:

command=>"/usr/bin/php/vagrant/cron.php",

user=>"root",

hour=>[1-4],

minute=>[0,30],

}

Puppetprovidesuswithdifferentconfigurationoptionstodefinethetimesatwhichacronshouldberun,whichincludesthefollowing:

Hour:Thisvalueisbetween0and23inclusiveMinute:Thisvalueisbetween0and59inclusiveMonth:Thisvalueisbetween1and12inclusiveMonthDay:Thisvalueisbetween1and31inclusiveWeekday:ThisvalueisSunday(7or0)toSaturday(6)

Ifoneoftheseisomittedfromtheconfiguration,thenPuppetrunsitforeachoneoftheavailableoptions(thatis,ifweomitmonth,itwillrunforJanuarythroughtoDecember).Whendefiningthedatesandtimes,wecaneitherprovidearange,forexample,[1-5]orspecifics,suchas[1,2,10,12].

Page 113: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RunningcommandsTheExecresourcetypeallowsustoruncommandsthroughtheterminalonthemachineweareprovisioning.OnecaveatwiththeexeccommandisthatifyoureprovisionwithPuppet,itwillrerunthecommand,whichcanbedamagingdependingonthecommand.WhatwecandowiththeExecresourcetypeissetthecreatesparameter.ThecreatesparametertellsPuppetthatafilewillbecreatedwhenthecommandisrun,andifPuppetfindsthatfile,itknowsthatithasalreadybeenrunandwon’trunitagain.

Takeforexample,thefollowingconfiguration;thiswillusethePHPcomposertooltodownloaddependencies.Thecommanditselfcreatesafilecalledcomposer.lock(wecan,ofcourse,usetheexeccommanditselftocreateafilemanually,perhapsusingthetouchcommand).Becauseofthelockfilethatiscreated,wecanusethecreatesparametertopreventthecommandfrombeingexecutedifithaspreviouslybeenexecutedandhascreatedthelockfile,asshowninthefollowingcode:

exec{"compose":

command=>'/bin/rm-rfv/var/www/repo/vendor/*&&/bin/rm–f

/var/www/repo/composer.lock&&/usr/bin/curl–s

http://getcomposer.org/installer|/usr/bin/php&&cd

/var/www/repo&&/usr/bin/php/var/www/repo/composer.

pharinstall',

require=>[Package['curl'],Package['git-core']],

creates=>"/var/www/repo/composer.lock",

timeout=>0

}

Page 114: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingusersandgroupsTheuserandgroupresourcetypesletuscreateandmanageusersandgroups.TherearedifferencesbetweendifferentoperatingsystemsastowhatPuppetcandowiththeusersandgroupsandhowthisworks.ThecodeinthefollowingsectionistestedonUbuntu,Linux.MoreinformationonthedifferencesforusersandgroupsondifferentplatformscanbefoundonthePuppetwebsiteathttp://docs.puppetlabs.com/references/latest/type.html#user.

CreatinggroupsThesimplestwaytocreateagroupissimplytosettheensureparametertopresent:

group{"wheel":

ensure=>"present",

}

CreatingusersTocreateauser,thebasicinformationweshouldprovideisasfollows:

TheusernameThefactthatwewanttheusertoexist(ensure=>present)Thegroup(gid)theusershouldbepartofTheshelltousefortheuserThehomedirectoryfortheuserIfwewantPuppettomanagethehomedirectoryfortheuser,inthiscase,itwillcreatethefolderforusifitdoesnotexistThepasswordfortheuserTherequirementsthatweneedthewheelgroupinplacefirst

Thecodethatwillthencreateouruserisasfollows:

user{"developer":

ensure=>"present",

gid=>"wheel",

shell=>"/bin/bash",

home=>"/home/developer",

managehome=>true,

password=>"passwordtest",

require=>Group["wheel"]

}

UpdatingthesudoersfileIt’sallwellandgoodbeingabletocreateusersandgroupsonourmachine,however,onethingthatwecan’tdousingtheuserandgroupresourcetypesisdefineauserorgroupashavingelevatedprivileges,unlesswemaketheuserapartoftherootgroup.

Wecanuseanexeccommandtopushsometexttotheendofoursudoersfile;thetextweneedtopushjusttellsthefilethatwewanttogivethewheelgroupthesudoprivileges,asshowninthefollowingcode:

Page 115: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

exec{"/bin/echo\"%wheelALL=(ALL)ALL\">>/etc/sudoers":

require=>Group["wheel"]

}

Thiscode,however,willcontinuallyaddthislinetothefileeachtimeitisrun,whichwedon’twant,however,aswelearnedearlier,wecaninstructtheexecresourcestoonlyrunatcertaintimes.Thankstosubscribeandrefreshonly:

exec{"/bin/echo\"%wheelALL=(ALL)ALL\">>/etc/sudoers":

subscribe=>Group["wheel"],

refreshonly=>true

}

Thisstillisn’tideal,asifwedecidetochangethewheelgroupthenthiswouldbererun,soideally,wemightkeepthesudoersfilewithinourPuppetconfigurationinsteadandusePuppettomanagethechangestoit.

Page 116: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingconfigurableclassesOnelimitationthatourentirePuppetcodehassofarinthischapteristhatwiththeexceptionofthecontentsofsomefiles,theconfigurationisallfixed.IfwewanttoreusesomeofthePuppetcodeonanotherproject,wemightneedtochangethingssuchasvariablenames,pathstofiles,orotherproject-orenvironment-specificproperties.

Tomakeourcodemoreflexible,wecanputthecodeintoaclass,anduseclassparameterstodynamicallyinjectvariablesintotheclass.

Aclassis,atitssimplestlevel,amethodofgroupingrelatedcodetogether;however,wecanusethemtobuildreusableandconfigurablemodules.IfwetakethecomposerExecresourceillustratedearlier,wecanstarttomakethatreusablebyputtingitintoaclass:

classcomposer{

exec{"compose":

command=>'/bin/rm-rfv/var/www/repo/vendor/*&&/bin/rm–f

/var/www/repo/composer.lock&&/usr/bin/curl–s

http://getcomposer.org/installer|/usr/bin/php&&cd/var/www/repo&&

/usr/bin/php/var/www/repo/composer.pharinstall',

require=>[Package['curl'],Package['git-core']],

creates=>"/var/www/repo/composer.lock",

timeout=>0

}

}

Whilewecannowmoreeasilypullthisintoanotherproject,wearelockeddownintermsofthelocationofthecomposerfileandalsootherparametersthatmightberequired.Wealsorequiretwopackages:wewouldeitherrequirethatthedeveloperaddstheseelsewheretotheirPuppetcode,orwewouldincludethemhere,however,includingthemherewouldconflictwithothermodulesandreducesflexibility.Classparametersletuspassinformationtoaclasswhenitisused;thisinformationcanbeusedtocontrolthecodewithintheclassandalsoconfigurepartsoftheclass.

ForourComposerclass,wemightwanttoletthedeveloperusingitdecidewhetherthedependenciesareinstalledbythemoduleornot,andalsoprovidepathstobeusedbythemodule.Classparametersaredefinedinbracketsimmediatelyaftertheclassnameandcanhavedefaultvalues,soiftheyareomitted,theirdefaultsareused.

Wecanthenusecontrolstatementstodecidewhetherthedependenciesshouldbeinstalled,andwecanpullinthecontentsofavariableusing${variable}:

classcomposer($install_deps=true,$path='/var/www/repo/',

$composer_home='/var/www/'){

if($install_deps==true){

package{"curl":

ensure=>present

}

package{"git-core":

ensure=>present

Page 117: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

}

}

exec{"compose":

command=>"/bin/rm-rfv${path}vendor/*&&cd${path}&&

/usr/bin/curl-shttp://getcomposer.org/installer|/usr/bin/php&&

COMPOSER_HOME=\"${composer_home}\"/usr/bin/php${path}composer.phar

install",

require=>[Package['curl'],Package['git-core'],Package['php5-

cli']],

creates=>"${path}composer.lock",

timeout=>0

}

}

Now,whenwewanttousethisclass,wepassthevaluesalongwiththesevariablenames.BecauseofthewayPuppetworks,theorderingoftheparametersdoesn’tmater;theyarepassedassociativelywiththeircorrespondingvariablenames,asshowninthefollowingcode:

class{

'composer':

install_deps=>true

}

Page 118: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PuppetmodulesTherearemanyexisting,well-written,reusablePuppetmodulesfreelyavailabletouse.PuppetForgeisacollectionofmodules,whichisavailableathttp://forge.puppetlabs.com/.Itisalwaysworthcheckingwhetherthereisanexistingmodulethatsolvesyourproblembeforewritingyourown.

Page 119: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingPuppettoprovisionserversWearegoingtotakealookathowtousePuppetwithVagrantinChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,however,Puppetcanalsoberunindependently.IfPuppetisinstalled(itwillbeonmostVagrantbaseboxes,butifyouwanttorunitonanothermachine,itmightnotbe,soinstallitfirst),youcanusetheapplysubcommand,passingwithitthelocationofthemodulesfolderandthedefaultmanifesttoapply,asfollows:

puppetapply--modulepath=/home/michael/provision/modules

/home/michael/provision/manifests/default.pp

Page 120: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 121: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,wehadawhirlwindtourofPuppetandexploredthevariouswaysinwhichwecoulduseittoprovisionaserverforourprojects.WeinstalledsoftwarewiththePackageresource,managedcronjobswiththeCronresource,managedusersandgroupswiththeUserandGroupresources,andrancommandswiththeExecresource.TomanageexecutionorderanddependencyrelationshipswithPuppet,welookedatusingRequire,Subscribe,Notify,andRefreshonly.Welookedathowmodules,classes,andstagesworkaswellashowtouseclassparameterstoconfigurereusablePuppetcode.Finally,welookedathowtousePuppettoprovisionamachine.

Inthenextchapter,wewilltakealookatAnsible,anotherprovisioningtoolthathassupportbuiltintoVagrant.

Page 122: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 123: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter4.UsingAnsibleAnsibleisanotherprovisioningtoolsupportedbyVagrantthatmakesiteasyforustotakeabaseoperatingsysteminstallationandturnitintoafull-fledgedserverthatsuitstheneedsofourproject.

Inthischapter,wewillquicklytakealookatthebasicsofAnsible.Wewon’tlookatitwithinaVagrantcontextjustyet;wewillsimplytakealookathowAnsibleworks,andhowwecanuseit.InChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,wewilltakealookathowtoconnectwhatwewilllearninthischapterwithVagrantitself.Inthischapter,wewilllearnthefollowingtopics:

HowAnsibleworksHowtouseAnsibletoperformthefollowingtasks:

InstallingsoftwareManagingfilesandfolderswithinthefilesystemManagingcronjobsRunningcommandsManagingusersandgroups

Howtousethird-partyAnsiblerolesHowtomanuallyrunAnsibletoprovisionamachine

Ansibleitselfisalargetopicandsubjectofseveralbooks.ForamoredetailedlookatAnsible,PacktPublishinghassometitlesdedicatedtoprovisioningwithAnsible:

LearningAnsible:https://www.packtpub.com/networking-and-servers/learning-ansibleAnsibleConfigurationManagement:https://www.packtpub.com/networking-and-servers/ansible-configuration-management

Page 124: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UnderstandingAnsibleAnsibleisanITautomationtoolthatprovidesprovision,orchestration,andconfigurationmanagementfeatures.UnlikewithPuppetandChef,Ansibledoesn’trequireanysoftwaretobepreinstalledontheserver,otherthananSSHservice,astheheavyliftingisdonebyourowncomputerthatconnectstoourAnsible-managedserversandinstructstheserveronhowitneedstochange.

LikePuppetandChef,Ansibleisalsoidempotent.ThismeanseachtimewerunAnsible,itwillonlyperformactionswhereachangeisrequired—soifweinstalltheNginxwebserver,thefirstrunofAnsiblewillinstallitandsubsequentrunswon’tbecauseitknowsNginxisalreadyinstalled.

AnsibleconfigurationiswritteninYamlAin’tMarkupLanguage(YAML),whichmakestheconfigurationeasytoreadandwrite.

Conceptually,Ansibleconfigurationismadeupofplaybooksthataremadeupofplays,whicharemadeupoftasks.Aplaybookistheconfigurationforanentiresystemorenvironment,whichismappedtospecificserversorhoststhroughplays—differentplayscanbeappliedtodifferentgroupsofserversatdifferenttimesfromthesameplaybook.Eachplaycontainsanumberoftasks,which,inturn,makecallstoAnsiblemodules.Inamoreadvancedcontext,wecanmakeuseofroleswithinAnsible(reusablefunctionality)suchthatourplaybooksmightsimplybeamappingofhoststoroles.However,forthepurposesofthischapter,wewillputtasksandmodulecallsdirectlyinourplaybook.

ModuleswithinAnsiblearesimilarincontexttoresourceswithinPuppet.Therearemodulestodealwithmanydifferentkindsofoperationsonaserver,whichwewilldiscussshortly.

Page 125: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingAnsibleBecauseAnsibledoesn’trequireanysoftwaretobeinstalledontheserverside,wecan’tsimplyconnecttoaVagrantvirtualmachineandtryoutAnsiblebecauseitisn’tinstalledonthere!Inordertouseit,bothtotryitout,andalsowhenitcomestointegratingwithVagrant,weneedtoinstallAnsibleonourowncomputer,whichisknownasthecontrolmachine.

AlthoughAnsiblecanbeusedtomanageWindowsServers,itcannotberunfromaWindowscontrolmachine.

PackagesareavailableformanyLinuxdistributions,andAnsiblecanbeinstalledonOSXusingHomebreworPython’spip.Completedetailsofthedifferentoperatingsystemsareavailableonlineathttp://docs.ansible.com/intro_installation.html.

NoteAlthoughwedon’tneedanythinginstalledontheserverbeingmanaged,wedoneedSSHaccesstothemachine,andunlessweinstalladditionalpluginsforAnsible,wewillalsoneedtohavepublicandprivatekeyssetup,sothatwecanconnecttothemachinewewishtomanageoverSSHwithoutapasswordfromourcontrolmachine.

Page 126: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatinganinventoryWhenwerunAnsibletoprovisionorconfigureamachine,Ansibletakesthehostswewanttoapplytheconfigurationto,fromourplaybooks.Itthenlooksupthesemachinesinitsinventory,whichspecifytheaddressesandconnectiondetailsforthesemachines,sothatAnsiblecanconnecttotheminordertochecktheirstatusandruntheprovisioningtasks.

Ataminimum,theinventoryneedstocontainanameandanIPaddressforeachserverthatwewantAnsibletomanage.However,thereareadditionalconfigurationswecanprovide,forexample,settingtheusertoconnectas,thepasswordtouse(whichrequiresadditionalconfiguration),theporttoconnectthrough,andifweneedtotunneltotheserverthroughanother.Anexampleofcreatinganinventoryisasfollows:

defaultansible_ssh_host=192.168.100.123

AlthoughwearelookingatAnsibleoutsidethecontextofVagrant,westillmightwanttouseAnsible,independently,toconnecttoandconfigureaVagrant-managedvirtualmachinesothatwecantestitinisolation.InChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,wewilllearnhowtodothiswithinVagrantitself.Ifwedothis,weneedtoprovidetheport.Wemightalsowishtochangetheuserthatweconnectastoroot,andensurethataspecificSSHkeyisusedfortheconnectionasfollows:

defaultansible_ssh_host=127.0.0.1ansible_ssh_port=2222

ansible_ssh_user=rootansible_ssh_private_key=~/.ssh/id_rsa

Thisinventoryfileissimplyaplaintextfilesavedsomewhereonourcontrolmachine.Bydefault,Ansiblewilllookforafilecalled/etc/ansible/hosts,whichitexpectstobetheinventory,however,whenwerunAnsible,wecanpointtoourowninventoryfileelsewhere,whichwewilldolaterinthischapter.

Page 127: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 128: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingAnsibleplaybooksAswediscussed,anAnsibleplaybookisaYAMLfile.ThefollowingexampleisasimpleplaybookthatcontainsinstructionstoupdatetheAptpackagemanagerclassonthemachinecalledbydefaultinourinventory:

---

-hosts:default

tasks:

-name:updateaptcache

apt:update_cache=yes

Wecanrunthisplaybookbyrunningtheansible-playbookour-playbook.yml-iour-inventory-filecommand.Ansiblewillthenlookupthatthisplaybookistobeappliedtothedefaultmachine,thedefaultmachine’sdetails,connecttoit,andifappropriate,runthecommand.Wewillwalkthroughtheexecutionprocessshortly.

Tasksareexecutedintheorderthattheyappearwithintheplaybook.However,wehavetheoptiontocallothertaskstoberunlateronceanactioniscompleted,throughtheuseofhandlers,whichwewilldiscussshortly.

NoteBecauseplaybooksarewritteninYAML,theformatandspacing/indentationinthesefilesiscritical.Incorrectindentationcancausefilestonotbeparsedcorrectly.

Page 129: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Modules–whatAnsiblecandoAnsiblemodulesaresimilartoPuppetresources,andwecanusethemtoinstallandmanagepackages,servers,users,files,cronjobs,andsoon.

Themodulesavailableinclude:

Apt:ThisisusedtomanageaptpackagesGit:ThisisusedtomanageanddeployfromgitrepositoriesService:ThisisusedtomanagerunningservicesontheserverCopy:Thisisusedtocopyfiles

Eachmodulecanbeconfiguredwithdifferentproperties,aswewilldiscussinthischapter.AcompletelistofthemodulesisavailablefromtheAnsiblewebsiteathttp://docs.ansible.com/list_of_all_modules.html.

Page 130: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingsoftwareLet’ssaywewanttoinstallNginxonourserver.Therearethreestepsinvolvedinthisprocess:

1. Updatingourpackagemanager.2. Installingthenginxpackage.3. Runningthenginxservice.

UpdatingourpackagemanagerWecanusetheaptmodule(http://docs.ansible.com/apt_module.html)toupdatetheaptpackagemanager’scache,whichistheequivalentofperforminganapt-getupdatecommand:

-name:updateaptcache

apt:update_cache=yes

Theupdate_cacheparametercanalsobeprovidedwhenwerunotherapt-relatedtasks,soinsteadofadedicatedtaskforit,wecaninsteadspecifythatwhenweinstallNginx,thepackagemanager’scachemustbeuptodate.

InstallingthenginxpackageWecanusetheaptmodule(http://docs.ansible.com/apt_module.html)toensurethatNginxisinstalled,andifitisn’t,itwillbeinstalledasfollows:

-name:ensurenginxisinstalled

apt:pkg=nginxstate=presentupdate_cache=yes

Here,wetoldAnsibletoensurethatthestateofthenginxpackageispresent,andthatweshouldupdatethepackagemanager’scachebeforeinstallingit.Therearedifferentstatesavailable,includingthelateststatestoensurethatwehavethelatestversionofapackagepresent,orabsenttoensurethatapackageisnotinstalledontheserver.

RunningthenginxserviceFinally,tomakesurethatNginxisrunning,weusetheservicemodule.WhileNginxwillautomaticallyrunwhenweinstallit,wecanconnecttoournewserverandaltersettingsorservicesbymistake.Ifthishappens,wecansimplyreruntheprovisioner,asNginxwillalreadybeinstalled,soitwon’treinstallit,buttheservicemodulewillforcetheservertostartthenginxservice.Wecanusetheenabledparametertoensurethattheserviceisconfiguredtostartautomaticallywhenthesystembootsnext:

-name:ensurenginxisrunning

service:name=nginxstate=startedenabled=yes

Page 131: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UnderstandingfilemanagementWecanusethefile,copy,andtemplatemoduleswithinAnsibletomanagefilesandfolderswithinthefilesystem.Let’stakealookatsomeexamples,whichallowustoperformthefollowing:

CopyfilesCreatesymlinksCreatefolders

CopyingafileOnecommonfileoperationwemightwanttoperformwouldbetotakeaconfigurationfilefromourprojectandcopyitintoourvirtualmachine.OneparticularusecasewouldbeanNginxconfigurationfile;wemightwanttodefinesomevirtualhostsandothersettingsinafile,whichwecansharewithourcolleagues.

TipWhilethisworkswellandcangetusupandrunningquickly,therearerolesouttherethatallowustoconfigureNginxandothersoftwaredirectlyfromAnsible.Thistypicallyworksbytheroleofstoringatemplatefile(acopyoftheconfigurationfilewithplaceholdersinit)andthen,insertingdatathatwedefinewithinourplaybookintothetemplate,andcopyingthefileontothemachine.However,forthesakeofthisintroductorychapter,wewilljustcopyafileacross.

Thetemplatemodule(http://docs.ansible.com/template_module.html)allowsustocopyafilefromourcontrolmachineontothemachinebeingprovisionedasfollows:

-name:writethenginxconfigfile

template:src=nginx-default-site.confdest=/etc/nginx/sites-

available/default.confowner=www-datagroup=www-data

AsthisisourNginxconfigurationfile,itmakessenseforustoreloadorrestartNginxwhenthisfilechangestoensurethattheupdatedconfigurationisappliedtotheserver.Wedothisintwostages:

1. Wesetourtasktonotifyahandleronceitisdone.2. Wecreateahandler,whichisonlyactivatedwhenitisnotified,torestartNginx.

Thefollowingisaplaybookthatupdatestheaptcache,installsNginx,ensuresthattheserviceisrunning,copiestheconfigurationfile,andthenensuresthatNginxisrestartedwhenthatfilechangesthroughanotifyoperationandahandler.Thenotifyandhandlerscodesectionsarehighlightedarefollows:

---

-hosts:default

tasks:

-name:updateaptcache

apt:update_cache=yes

-name:ensurenginxisinstalled

Page 132: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

apt:pkg=nginxstate=present

-name:writethenginxconfigfile

template:src=nginx-default-site.confdest=/etc/nginx/sites-

available/default.conf

notify:

-restartnginx

-name:ensurenginxisrunning

service:name=nginxstate=started

handlers:

-name:restartnginx

service:name=nginxstate=restarted

CreatingasymlinkThefilemodule(http://docs.ansible.com/file_module.html)allowsustocreatesymboliclinkstotheexistingfilesandfoldersinthefilesystem.If,forinstance,wewanttomapapublicfolderwithinourwebserversrootdirectorytoafolderwithinourVagrantsharedfolder,wecandothisasfollows:

-name:makeourVagrantsyncedfolderourwebroot

file:src=/vagrantdest=/var/www/siteowner=www-datagroup=www-data

state=link

CreatingfoldersWecanalsousethefileresourcetypetocreatefolders;thisisparticularlyusefulforscenariossuchasfolderstoholdfiles(avatars,attachments,andsoon)uploadedbyusersofawebapplication:

-name:createanuploadsfolder

file:path=/var/www/uploadsowner=www-datagroup=www-datamode=0777

state=directory

Wecanusethemodeparametertosetthepermissionsofthefolder,andtheownerandgroupparameterstosettheuserandgroupswhoownthedirectory(thesealsocanbeusedforfileswecreateandmanagetoo).Finally,thestateparameterisusedtoensurethatthepathprovidedisafolder.

Page 133: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingcronThecronmodule(http://docs.ansible.com/cron_module.html)letsususeAnsibletomanagecronjobs,whichweneedtorunonthemachine.Weprovideaname(whichisarequiredparameter),inthiscase,web_cron,thecommandtorun,theusertorunthecommand,andthetimesatwhichtorunthecommand,asshowninthefollowingcode:

-name:Runsomecron

cron:name="web_cron"hour="1-4"minute="0,30"job="/usr/bin/php

/vagrant/cron.php"

Ansibleprovidesuswithvariousdifferentconfigurationoptionstodefinethetimesatwhichacronshouldberun.Theseinclude:

Hour:Thisvalueisbetween0and23inclusiveMinute:Thisvalueisbetween0and59inclusiveMonth:Thisvalueisbetween1and12inclusiveDay:Thisvalueisbetween1and31inclusiveWeekday:ThisvalueisfromSunday(0)toSaturday(6)

Ifoneoftheseisomittedfromtheconfiguration,thenAnsiblerunsitforeachoneoftheavailableoptions(thatis,ifweomitmonth,itwillrunforJanuarythroughtoDecember),asithasadefaultvalueof*.Whendefiningthedatesandtimes,wecaneitherprovidearange,forexample,1-5orspecifics,suchas1,2,10,12.

Page 134: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RunningcommandsThecommandandshellmodulesallowustoruncommandsthroughtheterminalonthemachineweareprovisioning.Thedifferencebetweenthetwoisthattheshellmodulewillrunthecommandsthroughashellontheremotesystem.Soifweneedtoaccessenvironmentvariablesoroperators,suchas&,|,>,and<,thenweneedtousetheshellmodule.

Page 135: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingusersandgroupsTheuserandgroupmodules(http://docs.ansible.com/user_module.htmlandhttp://docs.ansible.com/group_module.html)letuscreateandmanageusersandgroups.

CreatinggroupsWesimplyprovideaname.Bydefault,thestateparameterissettopresent:

-name:createsomenewgroup

group:name=newgroupstate=present

CreatingusersTocreateauserwecanusetheusermodule(http://docs.ansible.com/user_module.html);theminimuminformationweneedagainistheusername.However,wecanalsospecifytheirgroup,password(providingacryptedhashasperhttp://docs.ansible.com/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module),andevenwhetheranSSHkeycanbegeneratediftheuserdoesnothaveone:

-name:createanewuser

user:name=ournewusergroup=newgroupstate=present

Page 136: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingAnsiblerolesTherearemanyexisting,well-written,reusableAnsiblerolesfreelyavailabletouse.Theserolestypicallymanagelargeaspectsofserverfunctionalityinonereusablebundle,forexample,thereisanNginxroletomanageNginxandconfiguresiteswithit.TherearemanyrolesavailableonAnsibleGalaxy(https://galaxy.ansible.com/),soitisworthcheckingtheseoutbeforewritingourowncode!

Page 137: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingAnsibletoprovisionserversOncewehaveaplaybookandinventoryfile,wecanruntheansible-playbookcommandtoanalyzeourplaybook,andensurethattheconfigurationforthematchingserversinourinventoryfileisupdated:

ansible-playbookour-playbook.yml-iour-inventory-file

Whenthiscommandwasrunforthefirsttimeagainstaparticularserver,theoutputwassomethinglikethis:

Let’swalkthroughthisscreenshottoseewhatisgoingon:

1. First,Ansiblepullsthefilestogetherandcheckswhethereverythingisvalid.2. Next,itgathersfactsabouttherelatedmachinesitneedstoconnectto.Thisisdone

byconnectingoverSSHandfindingoutinformation,suchasspecification,networkingdetails,andsoon.

3. Next,itrunsthroughthetasksinourplaybook.Wheninstallingandwritingourconfigurationfiles,Ansibleneedstomakeachange,becauseitisn’tinstalledandthefileisn’tthere.

4. Onceinstalled,Nginxautomaticallystarts,sothetasktoensurethatitisrunningdoesn’tdoanything(thiscomesbacktotheidempotencyofAnsible).AstheconfigurationfilewritingnotifiesthehandlertorestartNginx,Nginxisthenrestartedattheend.

5. Finally,weseearecap,threechangesweremade,andfivetasksresultedinanokresponse.

Onsubsequentruns,theoutputlookslikethis:

Page 138: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Therearetwodifferences:firstly,Ansibledoesn’tneedtodoanything,soeverythingisgreenandwegetfourokresults.Again,thisisbecauseAnsibleisidempotent,soitonlydoesthingswhenachangetothesystemisrequired.Secondly,becausewedidn’tnotifytheNginxrestarthandler,thehandlerwasn’tevenrunasatask,whichiswhyourrecapnumberhasdroppedto4,andtherewasnorelatedoutputforthehandler.

Page 139: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 140: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,welearnedaboutAnsible,theITautomationtool.Welookedathowitworks,andhowtocreateaninventoryfilesoAnsiblecanmanagedifferentservers,andhowtowriteplaybooks,whichcanworkwithsomeofthedifferentmodules.

Weinstalledthesoftwareandlearnedtoupdatethepackagemanagercachewiththeaptmodule,andpackagesthatareserviceswerethenstartedandmanagedwiththeservicemodule.ThetemplatemoduleallowedustocopyfilesfromourcontrolmachinetotheAnsible-managedmachine.Inordertotriggerservicereloads,welookedintonotifyinghandlersafterspecifictasksoccur.

Tocreateandmanage,files,folders,andsymlinksthefilemodulewasused,andweusedthecronmoduletocreateandmanagecronjobs.Usersandgroupswerecreatedandmanagedwiththeuserandgroupmodulesandfinallywelookedintorunningcommandswiththecommandandshellmodules.

Inthenextchapter,wewilltakealookatChef,thefinalprovisionerthatwewilldiscussinthisbook,andalsodiscusshowwecanuseittoprovisionservers.

Page 141: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 142: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter5.UsingChefChefisanotherprovisioningtoolsupportedbyVagrantthatmakesiteasyforustotakeabaseoperatingsysteminstallationandturnitintoafull-fledgedserversuitedtotheneedsofourproject.

Inthischapter,wewillquicklytakealookatthebasicsofChef.Wewon’tlookatitwithinaVagrantcontextjustyet;wewillsimplytakealookathowChefworks,andhowwecanuseit.InChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,wewilltakealookathowtoconnectwhatwewilllearninthischapterwithVagrantitself.Inthischapter,wewilllearnthefollowingtopics:

HowChefworksThebasicsbehindChefcookbooksandrecipesHowtouseCheftoperformthefollowingtasks:

InstallingsoftwareManagingfilesandfolderswithinthefilesystemManagingcronjobsRunningcommandsManagingusersandgroups

Howtousethird-partyChefcookbooksandrecipesHowtomanuallyrunCheftoprovisionamachine

Chefitselfisalargetopicandthesubjectofseveralbooks.ForamoredetailedlookatChef,PacktPublishinghassometitlesdedicatedtoprovisioningwithChef:

ChefInfrastructureAutomationCookbook,http://www.packtpub.com/chef-infrastructure-automation-cookbook/bookInstantChefStarter,http://www.packtpub.com/chef-starter/book

Page 143: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

KnowingaboutChefChefisaprovisioningtoolthatwecanusetosetupaserverforuseforaproject.Theconfiguration,whichdetermineshowtheserverneedstobesetup,canbestoredwithinourVagrantprojectandcanbesharedwithteammatesthroughversioncontrol,ensuringthateveryonegetsanup-to-datecopyoftherequireddevelopmentenvironment.

Informationabouthowaservershouldbeconfigured,thatis,itssoftware,files,users,andgroups,iswrittenintofilesknownasChefrecipes.TheserecipesarewrittenasRubyfiles.Cheftakesthisinformationandmatchesittoprovidersthatareusedtoexecutetheconfigurationonthemachineinacompatibleway.

Forourpurposes,wewilluseChef-solo,whichisitsstandalonemode(thisisalsohowVagrantusesit).Chef-solomeansthateverythingrunsfromonemachine.Chefalsohasclient-servercapabilities,whereyoucandefinetheChefcookbooksandrolesforalltheserversinyourinfrastructureonacentralhost,anditkeepsyourindividualserversattherequiredlevelofconfiguration.

AswithPuppet,Chefisalsoidempotent,whichmeansrunningChefonamachinemultipletimeshasthesameeffectasrunningitonlyonce.

Page 144: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 145: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingcookbooksandrecipeswithChefChefinstructionsarerecipesthatarebundledtogetherincookbooks.Acookbookcontainsatleastonerecipe,whichperformssomeactions.Cookbookscancontainmultiplerecipesandotherresourcessuchastemplatesandfiles.

Atitsmostbasiclevel,acookbookisafolder(namedasthenameofthecookbook)thatcontainsatleastarecipesfolder,whichcontainsatleastadefaultrecipefile,default.rb.Filesaretypicallystoredinafilesfolderwithinthecookbookfolderandtemplatefileswithinthetemplatesfolder.

NoteWhilebothPuppetandChefuseRuby,Puppetisadomain-specificlanguage,whichmakesitlookandfeellikeitsownlanguage,whereasChefisstructuredmorelikeRubyitself.

Page 146: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Resources–whatChefcandoChefusesresourcestodefinetheactionsandoperationsthatcanbeperformedagainstthesystem.ResourcesaremappedtoaChefcode,whichvariesdependingontheplatform/operatingsystembeingused.Forexample,onanUbuntumachine,thepackageresourceismappedtoapt-get.Someofthesesystem-specificinstructionscanalsobeaccesseddirectlyviatheirownresources,apt_package.Forexample,thisisusedtomanagepackagesonUbuntu-andDebian-basedsystems,whereasusingthepackageresource,Chefwillworkoutwhichpackagemanagertousebasedontheoperatingsystem.

Resourcetypesavailableinclude:

cron:ThisresourcetypeisusedtomanagecronjobsonLinux-andUnix-basedsystemsexecute:Thisresourcetypeisusedtoruncommandsattheterminal/commandpromptfile:Thisresourcetypeisusedtomanageandmanipulatefilesandfoldersonthefilesystemgroup:Thisresourcetypeisusedtomanageusergroupspackage:Thisresourcetypeisusedtoinstallsoftwareservice:Thisresourcetypeisusedtomanagerunningservicesonthemachinetemplate:ThisresourcetypeisusedtomanagefilecontentswithanembeddedRubytemplateuser:Thisresourcetypeisusedtomanageuseraccountsonthemachine

Eachresourcecanbeconfiguredwithdifferentattributes,aswewilldiscussinthischapter.AcompletelistoftheresourcetypesisavailableontheOpscodewebsite(OpscodeisthecompanybehindChef)athttp://docs.opscode.com/resource.html.

Page 147: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingsoftwareLet’ssaywewanttoinstallNginxonourserver.Therearethreestepsinvolvedinthisprocess:

1. Updatingourpackagemanager.2. Installingthenginxpackage.3. Runningthenginxservice.

Becausethefirststepisdifferentdependingontheoperatingsystemwearerunning,wemightwanttomovethisoutofChefatalaterstage;however,wewilluseitwithinChefforthetimebeing.Anyoperatingspecificcommands(suchasthis)arewrittenforUbuntu,whichistheoperatingsystemweareusingwithVagrant.

UpdatingourpackagemanagerInordertoupdateourpackagemanager,weneedtorunacommandontheserver.ThiscanbeachievedusingtheexecuteresourcewithinChefasfollows:

execute"apt-getupdate"do

ignore_failuretrue

end

ThisinstructsCheftoruntheapt-getupdatecommand.Asthenameoftheresource(thepartprovidedinquotesafterthenameoftheresource)isthecommandwewanttorun,thiswillbeexecuted.Ifweuseafriendlynameinstead,thenwewouldneedtoprovideacommandattributeasfollows:

execute"update-package-manager"do

command"apt-getupdate"

ignore_failuretrue

end

Bydefault,theexecuteresourceshaveatimeoutof3,600seconds,however,thiscanbeoverriddenbygivingatimeoutattributetotheresourceandatimevalue,forexample:

execute"apt-getupdate"do

ignore_failuretrue

timeout6000

end

InstallingthenginxpackageWecanusethepackageresourcetoensurethatNginxisinstalled,andifitisn’t,itwillbeinstalledasfollows:

package"nginx"do

action:install

end

Here,wetoldCheftoensurethatthenginxpackageisinstalled.Providedthatwehaveincludedtherecipeorcookbookthatcontainstheapt-getupdatecommandbeforetheprecedingcode,ourpackagemanagerwillbeuptodate.

Page 148: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RunningthenginxserviceFinally,tomakesurethatNginxisrunning,weusetheserviceresource.AswellasensuringNginxrunswhenitisfirstinstalled,thisalsoensuresthatifwemakeanychangestoourserver(andaccidentallystopNginx),wecansimplyreruntheprovisioner.AsNginxwillalreadybeinstalleditwon’treinstallit,buttheserviceresourcewillforceCheftoenablethenginxservice(soitautomaticallystartsonsystemboot),andstarttheservicewhenthecommandisrun,asfollows:

service"nginx"do

supports:status=>true,:restart=>true,:reload=>true

action[:enable,:start]

end

ThesupportspropertyisalistofattributesthatinstructChefonhowtomanageaparticularservice.Theactionensuresthatweenabletheservice(tohaveitrunwhenthemachinebootsup)andruntheservice(so,wedon’thavetowaitforarestart).

Page 149: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UnderstandingfilemanagementWecanusecookbook_file,directory,link,andtemplateresourceswithinCheftomanagefilesandfolderswithinthefilesystem.Let’stakealookatsomeexamples,whichallowusto:

CopyfilesCreatesymlinksCreatefoldersCreatemultiplefoldersinonego

CopyingafileOnecommonfileoperationwemightwanttoperformwouldbetotakeaconfigurationfilefromourprojectandcopyitintoourvirtualmachine.OneparticularusecasewouldbeanNginxconfigurationfile;wemightwanttodefinesomevirtualhostsandothersettingsinafile,whichwecansharewithourcolleagues.

TipWhilethisworkswellandcangetusupandrunningquickly,therearemodulesouttherethatallowustoconfigureNginxandothersoftwaredirectlyfromChef.Thistypicallyworksbythemodulestoringatemplatefile(acopyoftheconfigurationfilewithplaceholdersinit)andtheninsertingdatawedefinewithinChefintothetemplateaswellascopyingthefileontothemachine.However,forthesakeofthisintroductorychapter,wewilljustcopyafileacross.

Thecookbook_fileresourceallowsustocopyafilefromaChefcookbookontothemachineasfollows:

cookbook_file"/etc/nginx/sites-available/default"do

backupfalse

action:create_if_missing

end

Becauseweomittedthesourceandpathattributes,Chefmakessomeassumptions.Ittakesthebasename(ineffect,thelastelement)ofthenameandusesthisasthesource(thebasenameof/etc/nginx/sites-available/defaultbeingthedefault)andusesthenameasthepath(destination).Thesourcefileshouldbelocatedinthefilesfolderwithinthecookbook’sownfolder.

AsthisisanNginxconfigurationfile,itisworthensuringthatNginxisalreadyinstalled;otherwise,Nginxwilloverridethiswhenitinstallsthefirsttime,andthiswouldn’tmaketheprocessidempotent.Wecandothisbynotifyingthenginxservice,forexample:

cookbook_file"/etc/nginx/sites-available/default"do

backupfalse

action:create_if_missing

notifies:restart,"service[nginx]",:delayed

end

Page 150: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ThedelayedoptionallowsalloftheserestartrequeststobequeuedupandexecutedattheendofChef’sexecution;theoppositeofthisbeingimmediately.

CreatingasymlinkThelinkresourceallowsustocreatesymboliclinkstotheexistingfilesandfoldersonthefilesystem.If,forinstance,wewanttomapapublicfolderwithinourwebserver’srootdirectorytoafolderwithinourVagrantsharedfolder,wecandothisasfollows:

link"/var/www/public"do

to"/vagrant/src/public"

end

CreatingfoldersWecanusethedirectoryresourcetocreatefolders;thisisparticularlyusefulforscenariossuchasfolderstoholdfiles(avatars,attachments,andsoon)uploadedbyusersofawebapplication:

directory"/var/www/uploads"do

owner"root"

group"root"

mode00777

action:create

end

Wecanusethemodeparametertosetthepermissionsofthefolder,andtheownerandgroupparameterstosettheuserandgroupswhoownthedirectory(thesealsocanbeusedforfileswecreateandmanagetoo).Finally,the:createactionisusedtoensurethatthefolderiscreated.

CreatingmultiplefoldersinasingleprocesswithloopingInmanywebprojects,wemightneedtocreateanumberoffolderswithinourserversorourVagrantvirtualmachines.Inparticular,wemightwanttocreateanumberofcachefoldersfordifferentpartsofourapplication,orwemightwanttocreatesomeuploadfolders.

Inordertodothis,wecansimplycreateanarraythatcontainsallofthefolderswewanttocreate.Wecanthenusethedirectoryresourcetypeandloopthroughalistofdirectorynames:

%w{dir1dir2dir3}.eachdo|dir|

directory"/tmp/mydirs/#{dir}"do

mode00777

owner"www-data"

group"www-data"

action:create

end

end

Page 151: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingcronThecronresourcetypeletsususeCheftomanagecronjobsthatweneedtorunonthemachine.Weprovideaname,inthiscase,web_cron,thecommandtorun,theusertorunthecommand,andthetimesatwhichtorunthecommand,asshowninthefollowingcode:

cron"web_cron"do

action:create

command"/usr/bin/php/vagrant/cron.php"

user"root"

hour"1-4"

minute"0,30"

end

Chefprovidesuswithvariousdifferentconfigurationoptionstodefinethetimesatwhichacronshouldberun;theseinclude:

hour:Thisvalueisbetween0and23inclusiveminute:Thisvalueisbetween0and59inclusivemonth:Thisvalueisbetween1and12inclusiveday:Thisvalueisbetween1and31inclusiveweekday:ThisvalueisSunday(0)-Saturday(6)

Ifoneoftheseisomittedfromtheconfiguration,thenChefrunsitforeachoneoftheavailableoptions(thatis,ifweomitmonth,itwillrunfromJanuarythroughtoDecember).Whendefiningthedatesandtimes,wecaneitherprovidearange,forexample,1-5,orspecifics,suchas1,2,10,12.Wecanalsoprovideanemailtopropertytoe-mailtheresultingoutputfromthecrontoane-mailaddressofourchoice.

Page 152: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

RunningcommandsTheexecuteresourceallowsustoruncommandsthroughtheterminalonthemachineweareprovisioning.OnecaveatwiththeexeccommandisthatifyoureprovisionwithChefitwillrerunthecommand,whichcanbedamagingdependingonthecommand.Whatwecandowiththeexecuteresourceissetthecreatesparameter.ThecreatesparametertellsChefthatafilewillbecreatedwhenthecommandisrun;ifCheffindsthatfile,itknowsthatithasalreadybeenrun,anditwon’trunitagain.

Take,forexample,thefollowingconfiguration;thiswouldusethePHPcomposertooltodownloaddependencies.Thecommanditselfcreatesafilecalledcomposer.lock(wecan,ofcourse,usetheexeccommanditselftocreateafilemanually,perhapsusingthetouchcommand).Becauseofthelockfilethatiscreated,wecanusethecreatesparametertopreventthecommandfrombeingexecutedmultipletimeswhenalockfileisfound:

execute"compose"do

command"/bin/rm-rfv/var/www/repo/vendor/*&&/bin/rm–f

/var/www/repo/composer.lock&&/usr/bin/curl–s

http://getcomposer.org/installer|/usr/bin/php&&cd

/var/www/repo&&/usr/bin/php/var/www/repo/composer

.pharinstall"

creates"/var/www/repo/composer.lock"

timeout6000

end

Page 153: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ManagingusersandgroupsTheuserandgroupresourcetypesletuscreateandmanageusersandgroups.TherearedifferencesbetweendifferentoperatingsystemsastowhatChefcandowiththeusersandgroupsandhowthisworks.

CreatinggroupsThesimplestwaytocreateagroupissimplytosettheactionto:create,asfollows:

group"wheel"do

action:create

end

CreatingusersTocreateauser,weshouldprovidethefollowingbasicinformation:

TheusernameThefactthatwewanttocreatetheuserThegroup(gid)theusershouldbepartofTheshelltousefortheuserThehomedirectoryfortheuserWhetherwewantCheftomanagethehomedirectoryfortheuser;inthiscase,itwillcreatethefolderforusifitdoesnotexistThepasswordfortheuser

Thecodethatwillthencreateouruserisasfollows:

user"developer"do

action:create

gid"wheel"

shell"/bin/bash"

home"/home/developer"

supports{:manage_home=>true}

password"passwordtest"

end

UpdatingthesudoersfileIt’sallwellandgoodbeingabletocreateusersandgroupsonourmachine,however,onethingthatwecan’tdousingtheuserandgroupresourcetypesisdefineauserorgroupashavingelevatedprivileges,unlesswemaketheuserapartoftherootgroup.

Wecanuseanexeccommandthroughtheexecuteresourcetopushsometexttotheendofoursuodersfile;thetextweneedtopushsimplytellsthefilethatwewanttogivethewheelgroupsudoprivileges.Thecommandwewillneedtoexecuteisasfollows:

/bin/echo\"%wheelALL=(ALL)ALL\">>/etc/sudoers

Page 154: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

KnowingcommonresourcefunctionalitiesThereisalsoasetofcommonfunctionalityavailabletoallresources.Thiscommonfunctionalityincludes:

Theabilitytodonothingwiththe:nothingactionSharedattributesavailabletoallresources:ignore_failure,provider,retries,retry_delay,andsupportsThenot_ifandonly_ifconditionstoensurethatactionsonlyrunwhencertainconditionsaremet;thesearecommandsthatarerunanddependingontheirreturnvalue,recipes,andresourcescanbeignoredTherearenotificationstoinstructotherresourcesthatanotheractionhasbeencompleted,orforaresourcetotakeactionifanotherresourcechanges(subscribes)

Page 155: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingChefcookbooksTherearemanyexisting,well-written,reusableChefcookbooksfreelyavailabletouse.TheOpscodecommunitysitecontainsacollectionofthemathttp://community.opscode.com/cookbooks.Itisalwaysworthcheckingwhetherthereisanexistingcookbookthatsolvesyourproblembeforewritingyourown.

Page 156: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingCheftoprovisionserversWewilltakealookathowtouseChefwithVagrantinChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef;however,Chefcanalsoberuninitsownright.ProvidedChefisinstalled(itwillbeonmostVagrantbaseboxes,butifyouwanttorunitonanothermachine,itmightnotbe,soinstallitfirst),youcanusethechef-solocommand,passingwithitthelocationofaconfigurationfiletouse,andaJSONfilethatcontainsattributeswewishtouse(thisshouldincludetherublist,whichisthelistofrecipesandcookbookswewishtouse),asfollows:

chef-solo–c/home/michael/chefconfig.rb–j

/home/michael/attributes.json

Therearesomeusefullinksinthislistyoucanrefertoformoreinformation

Chef-soloconfiguration:http://docs.opscode.com/config_rb_solo.htmlApplyrecipestorunlists:http://docs.chef.io/recipes.html#apply-to-run-listsAnatomyofaChefrun:https://github.com/jhotta/chef-fundamentals-ja/blob/master/slides/anatomy-of-a-chef-run/01_slide.mdCheftutorial:http://www.mechanicalfish.net/configure-a-server-with-chef-solo-in-five-minutes/

Page 157: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 158: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,wehadawhirlwindtourofChefandexploredthevariouswayswecoulduseittoprovisionaserverforourprojects.Thisincludedhowtoinstallsoftwarepackageswiththepackageresourceandrunserviceswiththeserviceresource.Wealsomanagedcronjobswiththecronresource,managedusersandgroups,andrancommands.Finally,welookedathowrecipesandcookbookswork,andhowwecanuseCheftoprovisionaserver.

Inthenextchapter,wewilltakealookathowtousebothChefandPuppettoprovisionamachinewithinthecontextofVagrant.

Page 159: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 160: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter6.ProvisioningVagrantMachineswithPuppet,Ansible,andChefInChapter3,ProvisioningwithPuppet,Chapter4,UsingAnsible,andChapter5,UsingChef,wehadanintroductiontoPuppet,Chef,andAnsible,whichareprovisioningtoolswithsupportbuiltintoVagrant.However,weonlylookedathowthetoolsworkedinageneralway;wedidn’tlookathowtousethemwithVagrant.

Inthischapter,youwilllearnthefollowingtopics:

UsingPuppetwithinVagrantUsingChefwithinVagrantUsingAnsiblewithinVagrantRecappinghowtoprovisionwiththebuilt-inSSHprovisionerWorkingwithmultipleprovisionersHowwecanoverridetheprovisioningtoolsthroughthecommandline

Page 161: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningwithinVagrantVagrantreliesonbaseboxesfortheguestvirtualmachines;thesearespecificallypreconfiguredvirtualmachineimagesthathavecertainsoftwarepackagespreinstalledandpreconfigured.PuppetandChefaretwosuchsoftwarepackagesthatarepreinstalled(AnsibleiscontrolledbyVagrantitselfonthehostmachine,soitisn’tinstalledonthevirtualmachine,butrequiresanSSHconnectiontothevirtualmachine).Vagranthasitsowninterfacethroughtothesepackagesfromthehostmachine.ThismeanswecanprovidesomeconfigurationinourVagrantfile,andVagrantwillpassthisinformationtotherelevantprovisionersontheguestVM.

Page 162: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 163: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningwithPuppetonVagrantVagrantsupportstwomethodsofusingPuppet:

PuppetinstandalonemodeusingthepuppetapplycommandontheVMPuppetinclient/servermode,wherebytheVM(usingthePuppetagent)willbeconfiguredfromacentralserver

Let’stakealookathowtoconfigureVagrantwithPuppetusingthesetwodifferentmethods.

Page 164: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingPuppetinstandalonemodePuppetstandaloneisthesimplestwaytousePuppetwithVagrant.WesimplytellVagrantwherewehaveputourPuppetmanifestsandmodules,andwhatmanifestshouldberun.ThesmallestamountofconfigurationweneedwithinourVagrantfileinordertousePuppetisthis:

config.vm.provision"puppet"do|puppet|

end

ThisshouldgowithintheVagrant.configure("2")do|config|…endblockofcodewithintheVagrantfile.

Alongwiththisconfiguration,wewillneedaPuppetmanifestcalleddefault.ppinthemanifestsfolderofourprojectroot.Vagrantwillthenusethistoprovisionthemachine.

ThiswillinstructVagranttorunthePuppetprovisionereitherwhenthemachinebootsupforthefirsttimeorifwerunthevagrantprovisioncommand.ThedefaultVagrantPuppetsetupwillmakethefollowingassumptions,unlessweoverridethesettings:

ManifestswillbelocatedinthemanifestsfolderModuleswillalsobelocatedinthemanifestsfolder(wemaywanttopointtheseelsewhere,especiallyifweareusingthird-partymodules,tokeepthemseparate)Themanifestfiletousewillbedefault.pp(Itwillobviouslybewithinthemanifestsfolder;itcanbeusefultooverridethisifweusePuppetmodulesandmanifestswithinthesameprojectformultipleenvironments.WemayhaveamanifestforourVagrantVM,oneforourproductionenvironmentandoneforauseracceptancetestingplatform,forexample.)

Wecanmodifytheseoptionsbyprovisioningconfigurationoptions,asopposedtojusttellingVagranttoprovisionwithPuppet.WhencreatingprojectsthataremanagedbyVagrant,Iliketoputallmyprovision-relatedfileswithintheprovisionfolder.Inordertooverridethese,withinthePuppetconfigurationforVagrant,wecanthenspecifyoptionsforthelocationofthePuppetmanifests(puppet.manifests_path),thenameofthePuppetmanifesttoapply(puppet.manifests_file),andthelocationofanyPuppetmodules,whichwemayreferencewithinourPuppetmanifest(puppet.module_path).Thefollowingcustomizestheseoptions:

config.vm.provision"puppet"do|puppet|

puppet.manifests_path="provision/puppet/manifests"

puppet.manifest_file="default.pp"

puppet.module_path="provision/puppet/modules"

end

Itisimportantforustohavetheabilitytoatleastchangethemanifestfile,asVagrantalsosupportsamulti-VMenvironment,whereasingleprojectcanhaveanumberofvirtualmachines(forexample,awebserverandadatabaseserver).Withthissetup,wewillneedtotellVagrantwhichmanifestfiletouseforeachofthemachines,sothatthewebservercanbeproperlyconfiguredasawebserverandthedatabaseserverasadatabaseserver.

Page 165: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PuppetprovisioninginactionWiththeknowledgewegainedofcreatingPuppetmodulesandmanifestsfromChapter3,ProvisioningwithPuppet,wecannowpointourVagrantconfigurationatthesefiles,andseeitinaction.IfwerunaVagrantfileonaproject,whichissuitablyconfigured,wewillseetheoutputofPuppetapplyingitssettingstoourvirtualmachineintheterminalwindow,asshowninthefollowingscreenshot:

TheconsoleoutputhighlightsdetailsofeachPuppetinstructionthatisrun,including:

Thestagetheinstructioniswithin(thisisthePuppetstage,aswediscussedinChapter3,ProvisioningwithPuppet,whichallowsustogroupclassestogethertocontroltheorderingofcertainactions)ThemoduleTheresourcetypeTheresourcenameWhethertheinstructionwasexecutedsuccessfully

Page 166: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingPuppetinclient/servermodeAsdiscussedearlier,wecanalsorunPuppetwithinourVagrantenvironmentinclient/servermodeusingthePuppetagentonthevirtualmachine.Theconfigurationrequiredforthisisminimal;wesimplytellVagranttheaddressofthePuppetserverweareusingandthenameofournode(thevirtualmachinewearesettingup):

config.vm.provision"puppet_server"do|puppet|

puppet.puppet_server="puppet.internal.michaelpeacock.co.uk"

puppet.puppet_node="vm.internal.michaelpeacock.co.uk"

end

ThenodenameisthereferenceforthemachinewithinthePuppetserver,sothePuppetserverknowshowourVMshouldbeconfigured.ThenodenameisalsousedtogenerateanSSLcertificatesothattheVMcanauthenticatewiththePuppetserver(moredetailsonthisareavailableonthePuppetwebsite,https://puppetlabs.com,andthePuppetblog,https://puppetlabs.com/blog/deploying-puppet-in-client-server-standalone-and-massively-scaled-environments.

Page 167: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 168: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningwithAnsibleonVagrantInordertouseAnsiblewithinaVagrantproject,weneedtotellVagrantwheretheplaybookandinventoryfilesare:

config.vm.provision"ansible"do|ansible|

ansible.playbook="provision/ansible/playbook.yml"

end

Ansibleneedstoknowwhichmachinestoprovision;unlikewithotherprovisioners,wherethisisexplicitlyknownfromtheVagrantfileconfiguration,Ansibleusesaninventoryfiletoconfigurethis.TheinventoryfilecontainsalistofenvironmentnamesandIPaddresses;weusethisfiletorestrictwhichcommandsAnsiblerunsonspecificenvironments.Wecanomitthisfile,andVagrantwillgenerateoneforallofthevirtualmachinesitmanagesforusinthecurrentproject.

Wecanalsocreateourowninventoryfile.Ataminimum,itneedstoknowthenameofthevirtualmachine(fromourVagrantfile)andtheIPaddress.Toprovideonlythesetwopiecesofinformation,thisrequiresthevirtualmachinetoberunningonitsownIPaddress(pertheVagrantfilenetworkingconfiguration):

defaultansible_ssh_host=10.11.100.123

Alternatively,wecanprovidetheSSHporttouse,sothatAnsiblecanconnectfromourhostmachinetoourvirtualmachine:

defaultansible_ssh_host=127.0.0.1ansible_ssh_port=2222

TotellVagrantandAnsibleaboutourowncustominventoryfile,weneedtoaddittoourVagrantfileasfollows:

config.vm.provision"ansible"do|ansible|

ansible.playbook="provision/ansible/playbook.yml"

ansible.inventory_file="provision/ansible/inventory"

end

Page 169: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 170: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningwithChefonVagrantVagrantalsosupportstwomethodsofusingChef:

Chef-soloChefinclient/servermodewithChefclient

Let’stakealookathowtoconfigureVagrantwithChefusingthesetwodifferentmethods.

Page 171: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingChef-soloChef-soloistheChefequivalentofPuppetstandalone.

ThesimplestwaytousethiswithinourprojectissimplytoprovideaChefrunlisttoVagrant;thistellsVagrantwhichcookbooksshouldbeapplied.ThefollowingisanexampleoftellingVagranttousethePHPcookbook:

config.vm.provision"chef_solo"do|chef|

chef.add_recipe"php"

end

ThistakesthePHPcookbookfromthedefaultcookbooksfolderandappliesittothevirtualmachine.

AswithPuppet,Vagrantmakessomeassumptionsbydefault;theyareasfollows:

Cookbooksarestoredinthecookbooksfolderwithintheprojectroot.Thechef.cookbooks_pathsettingallowsustooverridethecookbooksfolderlocation.Wecaneitherprovideasinglepathoranarrayofpaths(wrappedinsquarebrackets,separatedwithcommas)ifwewantVagrantandCheftolookinarangeoffoldersforourcookbooks.ThefollowingcodewillgointoourVagrantfiletotellVagranttooverridethecookbooksfolderwithprovision/cookbooks:

config.vm.provision"chef_solo"do|chef|

chef.cookbooks_path="provision/cookbooks"

end

WecanalsouseChefrolesbyproviding:

ThelocationoftherolesfolderTheroleswewishtoaddtotheVM

MoreinformationonChefrolescanbefoundontheOpscodewebsite(http://docs.opscode.com/essentials_roles.html).

ThefollowingcodeinourVagrantfilewillsetupourprojecttouseChefroles:

config.vm.provision"chef_solo"do|chef|

chef.roles_path="provision/roles"

chef.add_role("web")

end

Page 172: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingChefinclient/servermodeLikePuppet,Chefalsohasaclient/servermethodtoprovisionmachinesusingChefclientontheVM.TouseChefclient,weneedtotellVagrantwheretheChefserverislocated(throughthechef.chef_server_urlsetting),andprovidetheauthorizationkeythatwillbeusedtoauthenticatetheVMwiththeserver(throughthechef.validation_key_pathsetting).

ThefollowingcodeinourVagrantfilewillinstructVagranttoprovisionfromaChefserver:

config.vm.provision"chef_client"do|chef|

chef.chef_server_url="http://chef.internal.michaelpeacock.

co.uk:4000/"chef.validation_key_path="key.pem"

end

WecanalsooverridetherunlistthattheChefserverprovidesforourVMbymanuallyaddingrolesandrecipestothisconfiguration.IfwehavespecifieddifferentenvironmentsonourChefserver,wecanspecifywhichenvironmentwewanttousewiththechef.environmentconfiguration.

TipVagrantVMsthatuseChefserverwillhavethecorrespondingnodeandcliententriesontheChefserver,whichisnamedwiththehostnameoftheVM.IfwedestroytheVMandrecreateit,Chefwillgenerateanerrorbecausetheclientandnodeentriesarealreadypresentontheserver.WeneedtoremovethesefromtheChefserverwhenwedestroyaVM.ThiscanbedoneusingtheknifetoolfromChef,knifenodedeleteour-vm-hostname&&knifeclientdeleteour-vm-hostname.

Page 173: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 174: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningwithSSH–arecapAswediscussedinChapter2,ManagingVagrantBoxesandProjects,wecaninstructVagranttorunaseriesofSSHcommandsontheVM.Thiscanbeusedtoprovisiontheserver.

TherearetwowaystouseSSHprovisioning:

Path:ThisprovidesafiletoexecuteInline:Thisisusedtoprovidespecificcommandstorun

Bothoftheseareshownasfollows:

config.vm.provision"shell",path:"provision/setup.sh"

config.vm.provision"shell",inline:"apt-getinstallapache2"

Page 175: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 176: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingmultipleprovisionersonasingleprojectWecanusemultipleprovisionerswithinasingleprojectifwewish;wesimplyneedtoputthemintheorderwewishforthemtobeexecutedwithinourVagrantfile.ThefollowingcommandwillfirstrunanSSHcommandbeforeprovisioningwithPuppet:

Vagrant.configure("2")do|config|

Config.vm.box="ubuntu/trusty64"

config.vm.provision"shell",inline:"apt-getupdate"

config.vm.provision"puppet"do|puppet|

puppet.manifests_path="provision/puppet/manifests"

puppet.manifest_file="default.pp"

puppet.module_path="provision/puppet/modules"

end

end

Usingmultipleprovisionerscanbeuseful,especiallyifoneismoresuitedatspecifictasksthananother,orifwerequiresomeprerequisites.Forexample,whenusingPuppetandChefinclient/servermode,theyneedtohaveanSSHkeytocommunicatewiththeserver.WecanuseashellprovisionertoinstructtheVMtodownloadthekeyswepreparedfromasecurelocation,beforethePuppetorChefprovisionerskickin.

Page 177: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 178: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

OverridingprovisioningviathecommandlineTheremaybeinstanceswherewewanttorestrictorenforcetheexecutionofprovisioningorevenaspecificprovisionerwithinaproject.Thefollowingcommandsareallexecutedfromthehostmachine:

WecancancelarunningprovisionbypressingCMD+Cattheterminal.WecaninstructVagranttorerunprovisioningonaVMusingthevagrantprovisioncommand.Wecanalsoadd--no-provisiontotheupcommandtoinstructVagranttonotruntheprovisioningtoolswhenperformingtheupaction,forexample,vagrantup--no-provision.Bydefault,Vagrantwillonlyprovisionwhenitfirstbootsamachine.Forsubsequentbootsofanexistingmachine,Vagrantknowsthatthemachinesareconfigured,anditwillnotprovisionthem.Wecanoverridethiswiththe--provisionoption,forexample,vagrantup--provision.Wecanalsoprovisionwithjustaspecificprovisionershouldwewish,forexample,ifwewanttoinstructVagranttojustrunPuppetinstandalonemode(inaprojectthathasmultipleprovisionersconfigured),weneedtorunvagrantprovision--provision-with=puppet.

Page 179: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 180: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,welearnedhowwecanapplyourknowledgeofPuppetandCheffromChapter3,ProvisioningwithPuppet,Chapter4,UsingAnsible,andChapter5,UsingChef,andconfigureVagranttousethesetoolstoprovisionourvirtualmachines.

WestartedoffbylearningtousePuppetinstandalonemodethatusesthepuppetapplycommandtoapplylocallystoredmanifestsandmodulesontothemachine.Then,wecontinuedusingPuppetintheclient/servermodethatusesthePuppetagenttoretrievetheconfigurationfromacentralservertoprovisionthemachine.

WethenlearnedhowtouseAnsibletorunplaybooksonspecificmachinesalongwiththefundamentalsofChef-solo,whichapplieslocallystoredcookbooksandrecipestothemachine.ItalsoincludedtheusageofChefinclient/servermode,whichusestheChefclienttoretrievetheconfigurationfromacentralservertoprovisionthemachine.

OtherstandardprovisionerswerealsocheckedusingSSHprovisioningandmultipleprovisioningoptionsforthesameproject.Finally,weroundedoffbyrunningmultipleprovisionerswithinasingleproject,overridingprovisioningonthecommandlineandrerunningtheprovisioningtoolswithvagrantprovision.

Now,wehavefullymasteredhowtosetup,use,andmanageVagrantalongwiththeprovisioningtoolstoworkonasinglemachineproject.InChapter7,WorkingwithMultipleMachines,wewilltakealookathowtouseVagrantandourknowledgeofprovisionerstomanageamultimachineproject,withprovisionersconfiguringdifferentmachinesfordifferentpurposesforusewithintheproject,forexample,awebserverandadatabaseserver.

Page 181: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 182: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter7.WorkingwithMultipleMachinesSofar,wehaveseenhowwecangetVagranttoastagewhereourdevelopmentenvironmentiscontainedinVagrant-managedvirtualmachines,withoneofthekeyaspectsbeingthatthesevirtualmachinesmimicourproductionenvironment.Itgivesustheflexibilityofbeingabletoencapsulatethedevelopmentenvironmentfordifferentprojectssothatwecaneasilyswitchfromonetoanotherwithouthavingtomodifythesoftwareonourownmachines.

Inmanycases,thefeatureswelearnedsofarareenough.However,webprojectsarebecomingmoreandmorecomplex,withdeveloperscontinuallyimproving,havingtodealwithmultiplemachinesintheirarchitecturetohelpwithscalabilityandstability.Whileitcanbesaidthatscalabilityandstabilityissueswon’timpactourdevelopmentenvironment(aswewon’thavehugeamountsoftrafficcomingtoourdevelopmentenvironment,unlessweload-testit),wewanttoensurethatthecouplingbetweenserverswithinourcode(suchasapplicationcodeconnectingtoaremotedatabase)worksinourdevelopmentenvironmentbeforeweputtheprojectonline.

Thankfully,Vagranthassupportforrunningmultiplevirtualmachinesatthesametimewithinthesameproject.Wecanusethistotestmultimachinearchitecturesanddistributedsystemsonourownlocalmachinebeforeweshareourchangeswithcolleaguesinastagingenvironment,andbeforetheprojectgoeslive.Replicatingamultimachineenvironmentindevelopmentgreatlyhelpsusimprovethereliabilityofourprojectsandinstillsconfidenceintheworkthatwedo.

Inthischapter,wewilllearnthefollowingtopics:

HowtorunmultiplevirtualmachineswithinasingleVagrantprojectHowtoprovidedifferentdistinctconfigurationtothesevirtualmachines,includingthefollowing:

NamesIPaddressesonaprivatenetworksothattheycancommunicatewithoneanotherBaseboxesProvisioningSharedfolders

HowtoconnecttothedifferentvirtualmachinesoverSSHwithouthavingtoknoworremembertheirIPaddresses

Page 183: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UsingmultiplemachineswithVagrantInordertousemultiplevirtualmachineswithinourproject,weneedtotellVagrantaboutthem,andweneedtoprovideadditionalconfigurationfortheindividualvirtualmachines.

Page 184: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

DefiningmultiplevirtualmachinesWithinthestandardVagrantprojectconfigurationfile,wecantellVagrantthatwewishtoassignanametoavirtualmachinebeingmanagedbytheproject.Withinthissubconfiguration,weprovidetheinformationVagrantneedsthatisspecifictothatVM.

Thesyntaxforthesubconfigurationsisasfollows:

config.vm.define:name_of_the_vmdo|name_of_the_vm|

#configurationspecifictothevirtualmachine

end

Thisisappliedtoaprojectthatrequirestwovirtualmachines,namedserver1andserver2,bothrunningtheprecise64box:

#-*-mode:ruby-*-

#vi:setft=ruby:

VAGRANTFILE_API_VERSION="2"

Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|

config.vm.define:server1do|server1|

server1.vm.box="hashicorp/precise64"

end

config.vm.define:server2do|server2|

server2.vm.box="hashicorp/precise64"

end

end

Page 185: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 186: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ConnectingtothemultiplevirtualmachinesoverSSHWhenourmultiplemachinesbootupinourmultimachineproject,VagrantautomaticallymapsdifferentportsfromourhostmachinetotheSSHportsonthevariousguestmachines.

Let’stakealookattheconsoleoutputwhenbootingaVagrantprojectwithtwovirtualmachineswithinit:

Page 187: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Asshownintheprecedingscreenshot,VagrantmapstheSSHportonthevirtualmachinedesignatedserver1toport2222onthehostmachine,andtheSSHportofthemachinedesignatedserver2totheport2200.

ThisgivesustheopportunityofsimplyusingthestandardSSHcommandfromaterminal(orthelikesofPuTTYonaWindowsmachine),toconnecttolocalhostwiththeportnumberthatVagrantassignstoeachmachine.

Toconnecttothemachinethatismappedtoport2200,wesimplyrunthesshvagrant@localhost–p2200command.The–p2200optiontellsthecommandtouseanonstandardport,andspecifiestheportwewishtouse,inthiscase2200.

Alternatively,wecanusethevagrantsshcommandtoconnecttothevirtualmachines.Thedifferenceisthatinamultivirtualmachineenvironment,wemustalsoprovidethenameofthevirtualmachine.Forexample,vagrantsshserver1.Thisisthemostcommonusageofconnectingtoamachine,ratherthandirectlyconnectingtothevirtualmachineviaitsIPaddress.

Page 188: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

NetworkingthemultiplevirtualmachinesInasinglevirtualmachineproject,theIPaddressofthevirtualmachineisn’tthatimportant.Inamultivirtualmachineproject,however,itismorelikelythatwewantthetwomachinestocommunicatewithoneanotherdirectly;inordertodothis,weneedtobeawareoftheirIPaddresses,orweneedtoforwardnonconflictingportstothelocalhostinstead.AswewanttohaveourVagrantprojectsdistributedtoourteammembers,andsomeoftheseteammembersmaybewithinthesameoffice,weneedto:

PredefinetheIPaddresssothatanyofourprojectscodethatneedstocommunicatewiththeothervirtualmachinecandoso,withouttheotherteammembersneedingtochangeconfigurationsEnsurethatthevirtualmachinesarerunningonaprivatenetworkonlythatareattachedtothemachineoftheuserrunningit;thiswillpreventtheIPaddressconflictswithinthenetwork

Inordertodothis,wesimplyusethenetworkingoptions,whichwelearnedinChapter2,ManagingVagrantBoxesandProjects.Becausewewantthevirtualmachinestoruninaprivatenetwork,itmakessensetousearangeofprivateIPaddresses,whicharedifferenttoyourownnetwork.Forexample,mynetworkrangeis192.168.1.xxx,soIwillusetherange10.11.1.xxxformyvirtualmachinenetwork(theIPaddressrangesareasubsetoftherangeofaddressespreassignedforinternalnetworks),asshowninthefollowingcode:

#-*-mode:ruby-*-

#vi:setft=ruby:

VAGRANTFILE_API_VERSION="2"

Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|

config.vm.define:server1do|server1|

server1.vm.box="hashicorp/precise64"

server1.vm.network"private_network",ip:"10.11.1.100"

end

config.vm.define:server2do|server2|

server2.vm.box="hashicorp/precise64"

server2.vm.network:private_network,ip:"10.11.1.101"

end

end

Let’stestthisoutandtestwhetherwecanconnectfromonemachinetotheother:

1. Poweruptheproject(vagrantup).2. Connecttoserver1(vagrantsshserver1).3. Pingserver2fromserver1(ping10.11.1.101).

Theoutputshowsthatweareabletocommunicateoverthenetworkfromserver1toserver2asfollows:

Page 189: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 190: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisioningthemachinesseparatelyAsthevirtualmachinesinourprojectsaregoingtobeusedfordifferentpurposes,weneedtousedifferentprovisioningforthemachines,sotheybothhaveonlythesoftwareandconfigurationsneededtodotheirjob.

Wetaketheprovisioningcode,whichwelearnedinChapter3,ProvisioningwithPuppet,andChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,andweplacetherelevantcodewithinthevirtualmachine’ssubconfiguration.Therearesomekeychangesthatweneedtomake:

TheopeninglineoftheprovisioncodemustreferencetheservernameofthevirtualmachineitrelatestoForPuppet,weshoulduseadifferentmanifestfileforthetwovirtualmachinesForChef,wewillapplydifferentrolestothedifferentmachines

ThefollowingcodeprovisionsboththemachinesusingPuppet.TheybothrelyonthesamesetofPuppetmodules,thesamepaththatpointstothemanifestsfolder,however,theybothusedifferentmanifeststosetuptheprojects(alternatively,wecanconfigurethemachinesandidentifythemasnodestoaPuppetmastertoretrievetheappropriateconfiguration):

#-*-mode:ruby-*-

#vi:setft=ruby:

VAGRANTFILE_API_VERSION="2"

Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|

config.vm.define:server1do|server1|

server1.vm.box="hashicorp/precise64"

server1.vm.network"private_network",ip:"10.11.1.100"

server1.vm.provision:puppetdo|puppet|

puppet.manifests_path="provision/puppet/manifests"

puppet.manifest_file="server1.pp"

puppet.module_path="provision/puppet/modules"

end

end

config.vm.define:server2do|server2|

server2.vm.box="hashicorp/precise64"

server2.vm.network:private_network,ip:"10.11.1.101"

server2.vm.provision:puppetdo|puppet|

puppet.manifests_path="provision/puppet/manifests"

puppet.manifest_file="server2.pp"

puppet.module_path="provision/puppet/modules"

end

end

Page 191: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

end

Withintheprovisionsforeachmachine,wewouldneedtoensurethatweallowboththemachinestocommunicatewithoneanother.Forexample,bydefault,aMySQLServerwon’tacceptconnectionsfromaremoteserver,sowewouldneedtomodify(orreplace)theconfigurationfilewithonethatallowsthis,orwewouldneedtouseaPuppetmoduleorChefcookbookthatallowsustomodifythisconfigurationvalue.

TipYoushouldcheckthedocumentationforanysoftwareyouarecommunicatingwithoverthenetworktoseehowitneedstobeconfigured.WithMySQL,youneedtoeditthemy.cnffile,andsetthebindaddressto0.0.0.0.

Page 192: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 193: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

DestroyingamultimachineprojectIfwewanttocompletelyremovethevirtualmachinesforourprojectfromourhostmachine,wecanusethevagrantdestroycommand,aswithnormalprojects.ThedifferencebeingthatVagrantwillaskustoconfirmtheremovalofeachmachinewithintheproject:

Page 194: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 195: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,wesetupaVagrantprojectthatusesmultiplevirtualmachines.Duringthecourseofthischapter,welearnedhowtocreatemultiplevirtualmachineswithinasingleproject.Inordertoachievethis,wealsolookedathowtoassignspecificnamestotheseindividualmachines,howtoconnecttotheindividualmachinesoverSSH(aspreviouslyvagrantsshwouldtakeustojustasinglemachine),andhowtoconfiguretheindividualmachines,specifyingIPaddresses,baseboxes,anddifferentprovisioningoptionsforthem.

Now,welearnedthevastmajorityofVagrant’sfunctionalityandhowtouseitwithindifferentprojectscenarios.Inthenextchapter,wewilltakealookathowtobuildourowncustombaseboxtousewithourprojects,configuringablankoperatingsysteminstallationintoacompatiblebaseimage.

Page 196: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 197: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter8.CreatingYourOwnBoxSofar,wehaveusedVagrantwiththefreelyavailablebaseboxes.TherearealsomanyotherexistingbaseVagrantboxesoutthere.InChapter9,HashiCorpAtlas,wewilltalkabouthowwecandiscoveranddistributebaseboxes.WhenwediscussedVagrantboxesinitiallyinChapter1,GettingStartedwithVagrant,andChapter2,ManagingVagrantBoxesandProjects,welearnedabouthowwecanexportaVagrantenvironmentintoanewbasebox.

Creatinganewbaseboxinvolvesuseithertakinganexistingbox,makingchangestoit,andexportingit,orcreatinganewvirtualmachineentirely,installingtheoperatingsystemandbuildingupabaseboxforexport.Inthischapter,wewilltakealookathowwecantakeaLinuxinstallationdiskandturnitintoaworkingVagrantbasebox,whichwecanfurthercustomizeasmuchaswelike.

Inthischapter,youwilllearnaboutthefollowingtopics:

HowtocreateanewVirtualBoxmachine,suitablyconfiguredforVagrantHowtoinstalltheVirtualBoxGuestAdditionsHowtosetuptheLinuxinstallationtoletVagrantloginHowtoinstallPuppetHowtoinstallChefHowtocleanuptheboxHowtoexporttheVMintoabasebox

Page 198: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

GettingstartedInordertocreateanewbasebox,weneedtodownloadacopyoftheoperatingsystemwewanttouse(wewilluseUbuntuServerVersion13.0464-bitfromhttp://releases.ubuntu.com/raring/).WethenneedtouseVirtualBoxtocreateavirtualmachine,poweredbytheoperatingsystemwehavedownloaded.Next,weneedtoconfigurethevirtualmachineforVagrant.Finally,weneedtoexportthevirtualmachineintoaVagrantbasebox.

TipYoucanalsouseotherdistributionsofLinuxorevenWindowsifyouwish.Specificswillvarywiththeoperatingsystemused,soyouwillneedtoconsulttherelevantdocumentation.

TherequirementsforanewbaseboxaredetailedontheVagrantwebsite(https://docs.vagrantup.com/v2/boxes/base.html).

Page 199: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 200: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

PreparingtheVirtualBoxmachineInordertocreatethevirtualmachinewithVirtualBox,weneedtoopentheVirtualBoxandclickontheNewbuttonintheupper-leftcorneroftheVirtualBoxtostarttheprocess:

Let’snamethemachinevagrant-ubuntu-raring.ThisistheformatrecommendedbyVagrant.SelectLinuxintheTypedropdownandVersionasUbuntu(64bit):

Vagrantrecommendssettingamemoryallocationof360MB.Thisistypicallysufficientforabaseinstallation,anduserscanoverridethiswithintheirVagrantfileiftheyneedmoreresources:

Page 201: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Weneedourvirtualmachinetohavesomestorageallocation,solet’sselectCreateavirtualharddrivenow:

WeneedtoselectVMDK(VirtualMachineDisk)asthedisktype:

Page 202: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Weneedtocreateadrive,whichisdynamicallyallocated:

Page 203: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Let’sgivethedriveamaximumlimitof40.00GB;theVagrantdocumentationsuggeststhatthisistypicallysufficientformanyprojects:

ClickingonCreatewillthensavethevirtualmachinewithinVirtualBox.Weneedtomakesomeadditionalconfigurationchanges,whicharenotapartofthecreationwizard,solet’sclickontheVMontheleft-handsideofthescreen,andthenclickontheSettingsbutton:

ThefirstadditionalchangeisAudio,solet’sturnthisoff:

Page 204: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

WeneedtoensurethatthenetworkadapterconfiguredwithinVirtualBoxisenabledandusesNAT.Withoutthis,Vagrantwon’twork:

Finally,let’sturnoffUSBsupport,asthisisgenerallynotrequired:

Page 205: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Nowweneedtoswitchonthevirtualmachine.Whenitpowerson,itasksustoselectastartupdisk,whichcontainstheoperatingsystemwewishtoinstall.Clickingonthefoldericononthisscreenletsusselectacustomfile;inourcase,thiswillbeourubuntu-13.04-server-amd64.isofile.

Thevirtualmachinewillthenbootfromtheimagefileandtakeustotheinstallationprocess.Weshouldfollowthisprocesstosetupthemachine.

TherearesomespecificvaluesforsomethingsthatVagrantexpects,sowhereverappropriateweshouldensurethatwesetthemasfollows:

Byconvention,theoperatingsystem’shostnameshouldbeofthevagrant-operating-system-nameformat,forexample,vagrant-ubuntu-raringThedomainisvagrantup.comTherootpasswordisvagrantThemainaccountusernameisvagrantThemainaccountpasswordisvagrant

Inmostothercases,thedefaultoptionswillbefine,aswewillconfigureotheraspectslater.Whenpromptedastoanypackagestoinstallbydefault,weshouldselecttoinstallopenssh-server.

Page 206: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 207: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

VirtualBoxGuestAdditionsFirst,let’slogintoournewvirtualmachinewithinVirtualBox.Onceloggedin,attheterminal,weshouldrunapt-getupdatetoupdateourpackagemanager.

VagranthasasetoftoolscalledGuestAdditionsthatprovidesomekeyintegrationpointsbetweenthevirtualmachineandVirtualBox;thisincludessupportforsharedfoldersandnetworkingintegration.

Toinstallthesetools,oncetheVMisrunning,weshouldclickontheDevicesmenuwithinVirtualBoxandclickonInstallGuestAdditions…(Host+D):

ThissimplybootsavirtualCDwithinthevirtualmachine;westillneedtoactuallyinstalltheGuestAdditions,asfollows:

1. Thefirststepistoinstallaprerequisite,whicharetheLinuxheaders:

sudoapt-getinstalllinux-headers-$(uname-r)build-essential

2. Next,wewillmountthevirtualCD,whichVirtualBoxhasloadedupintoafolderwithintheVM:

sudomount/dev/cdrom/media/cdrom

3. Finally,wewillruntheinstallationcommand:

sudosh/media/cdrom/VBoxLinuxAdditions.run

Page 208: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 209: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

VagrantauthenticationVagrantcommunicateswithbaseboxesoverSSH.Vagrantitselfhasaprivatekey,forwhichweneedtoinstallthecorrespondingpublickeyintothevirtualmachine.Vagrantexpectsaspecificuserwithapredefinedpasswordtoalsobewithinthemachine,andtheuserneedstobeconfiguredsothatitisn’tpromptedforthepasswordwhenattemptingtoperformactionsthatrequireelevatedprivileges(sudo).

Page 210: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

VagrantuserandadmingroupProvidedwecreatedtheVagrantuserduringtheinstallationprocess(asperthemainaccountuserandpasswordmentionedearlier),wethenneedtocreateanadmingroupandaddtheVagrantusertothisgroup.

First,weneedtocreatethegroup:

Sudogroupaddadmin

ToaddtheVagrantusertothisgroup,runthefollowingcommand:

Sudousermod-a-Gadminvagrant

Page 211: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ThesudoersfileInordertostopthevirtualmachineaskingfortheuser’spasswordwhenrunningelevatedactions,weneedtomodifythesudoersfile.Thisisafilethattellstheoperatingsystemwhichuserscanperformelevatedactionsandthesettingsaroundthem.Moreinformationcanbefoundathttps://help.ubuntu.com/community/Sudoers.Weneedtoaddaconfigurationlinetothisfile,whichtellstheoperatingsystemnottopromptforthepassword.Becausethefileisveryimportant,andanincorrectconfigurationwouldbreaktheoperatingsystem,thereisaprogrambuiltintoUbuntu,whichwon’tsaveifthefileisnoteditedcorrectly.

First,let’srunthisprogram:

visudo

Atthebottomofthefile,let’saddthislinetopreventtheoperatingsystemfrompromptingforthepassword:

%adminALL=(ALL)NOPASSWD:ALL

AnotherrequirementofVagrantisthatweaddthefollowinglinenearthetopofthefile:

Defaultsenv_keep="SSH_AUTH_SOCK"

Wealsoneedtodisablerequirettyinthesudoersfilebycommentingouttheappropriatelineasfollows:

#Defaultrequiretty

Tiprequirettyisanoptionthatrequiresuserstohaveaphysicalconnectiontoaserverinordertorunthesudocommands.

Page 212: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Insecurepublic/privatekeypairTheinsecurepublicandprivatekeypairispubliclyavailableathttps://github.com/mitchellh/vagrant/tree/master/keys/.

TipAnupcomingversionofVagrantwillchangehowVagrantworkswiththeseinsecurekeys.Whendetected,theywillbereplacedwithnewkeysforyourmachine;however,attimeofwriting,thishasnotyetbeenreleased.

Weneedtocopythecontentsofthepublickeyandpasteitintotheauthorized_hostsfile.ProvidedweareloggedinastheVagrantuser,wecanrunthefollowingcommandtoletuseditthisfile:

nano~/.ssh/authorized_hosts

Ifthe.sshfolderdoesnotalreadyexist,wefirstneedtocreateitusingthemkdircommand.Alternatively,wecandownloadthefilecontentsandputitstraightintotheauthorized_hostsfile:

wget

https://raw.github.com/mitchellh/vagrant/master/keys/

vagrant.pub–O~/.ssh/authorized_hosts

NoteThe.sshdirectoryneedstohavepermissionsof0700,andtheauthorized_hostsfileneedstohavepermissionsof0644(chmod0644~/.ssh/authorized_keys).

Page 213: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 214: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ProvisionersBecauseVagrantprovidessupportforprovisioners,weshouldinstalltheseintothevirtualmachinesothatVagrantcantellthemtoprovisionourenvironments.

Page 215: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingPuppetPuppetisinstalledusingthebuilt-inpackagemanager:

sudoapt-getinstallpuppet

TipTheversionofPuppetinthevariousoperatingsystemrepositoriesmaybeslightlydated.PuppetcanalsobeinstalledmanuallyorviatherepositorysiteprovidedbyPuppetLabs.MoreinformationisavailableonthePuppetlabswebsiteathttp://docs.puppetlabs.com/guides/installation.html.

Page 216: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingChefAspertheChefdocumentationathttps://www.chef.io/download-chef-client/,wecangetasinglecommandtoinstallChefforus:

curl-Lhttps://www.chef.io/chef/install.sh|sudobash

Page 217: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 218: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CleaninguptheVMBeforewepackageupthevirtualmachineintoaVagrantbasebox,let’scleanupsomeofthefilesweused.Wemadeuseofthetmpfolder,solet’semptythis.Weshouldalsocleanupourpackagemanager’scache,asthisusesadditionalspacewhenthebaseboxispackaged:

rm–rf/tmp/*

sudoapt-getclean

Page 219: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 220: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

ExportFinally,weuseVagrant’spackagesubcommandonthehostmachine(nottheguest)topackageupthebox:

vagrantpackage--basevagrant-ubuntu-raring

ThecompletedetailsofthepackagesubcommandareavailableontheVagrantwebsite(http://docs.vagrantup.com/v2/cli/package.html).

Page 221: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 222: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,welearnedhowtocreate,abaseboxforourVagrantprojectsfromscratch.Thiscanbeusedtocreatebaseboxesfromoperatingsystems,whichdon’tnecessarilyhaveboxesavailabletodownload.

Now,weknowhowtocreate,manage,distribute,andevenbuilddevelopmentenvironmentsfromscratchforourprojects!

Next,wewilltakealookatVagrantCloud,whichletsussearchforanddiscoverdifferentbaseboxesaswellaslettingusdistributeourownbox—eithertothepublicortoaprivateteam.VagrantCloudalsohasthefunctionalitytoallowustoshareourVagrantdevelopmentenvironmentwithothers—bethattodemonstratethefunctionalitywehavebuiltonaVagrant-supportedproject,ortoprovideSSHaccesstoateammemberwhocanhelpuswithsupportissues.

Page 223: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 224: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chapter9.HashiCorpAtlasHashiCorpAtlas(https://atlas.hashicorp.com),formerlyVagrantCloud,isasuiteofonlineservicesprovidedbyHashiCorp(thecommercialcompanybehindVagrant),whichaddsadditionalcapabilitiestoVagrantandbringstogethermanyoftheiropensourcecomponents.Primarily,Atlassupportstwofeatures:

VagrantShare:TheabilitytoshareaccesstoyourVagrantenvironmentandtoallowotherstoremotelyconnecttoitVagrantboxdiscoveryandsharing:TheabilitytoshareVagrantboxeswithothers,hostingthemetadataforboxes,theirversions,andfacilitatingboxupdates

Thesefeaturesareavailablefreeofcharge,thoughpayingcustomerscangainaccesstoadditionalfunctionality,includingthefollowing:

CustomandstaticdomainnamesforVagrantSharePrivateboxesthatcanbesharedwithspecificteamsprivatelyBoxhosting:VagrantCloudwillactuallystoretheboxfileontheirplatformaswellasthemetadataSupportforWindowsandMacVagrantboxesGranularsupportforuseraccesscontrols

Inthischapter,youwilllearnaboutthefollowingtopics:

HowtodiscoveranduseboxesprovidedonAtlasHowtodistributeyourownboxesonAtlasHowtoallowotherstoconnecttoyourVagrantmachinethroughAtlasHowtoshareyourVagrantmachinethroughAtlas

Page 225: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

DiscoveringboxesTheAtlaswebsitecontainsadirectoryofpublicboxesforVagrant(https://atlas.hashicorp.com/boxes/search).Withinthisdirectory,wecansearchforthespecificoperatingsystemordistributionversionthatweareinterestedin:

Foreachresult,wecanseetheboxname,whichisformattedasthenameofthedistributorfollowedbyaslash,followedbythenameordistributionname.Inthefollowingcase,wehavetheUbuntu12.04LTSreleasethatHashiCorphasprovided(namedhashicorp/precise64):

Ifweclickinabox,wecanseewhichproviderstheboxsupports.Inthiscase,wecanusetheboxwithVirtualBox,VMwareFusion,andHyper-V.Itisimportanttouseboxesthatsupporttheproviderweareusing—notallboxessupportallproviders.

Page 226: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingnewboxesToinstallapublicbox,weusethevagrantboxaddcommand,andpassthenameofthebox:

vagrantboxaddhashicorp/precise64

ThenameoftheboxcaneitherbeaURLorfilepathtoanexistingboxfile(forexample,ifwehaveonestoredonournetworkthatwewishtouse)oranAtlasboxname,likeintheprecedingcommand.

Page 227: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

UpdatingexistingboxesOneofthekeybenefitsofusingAtlasforboxdiscoveryisthatchangesandversionsoftheseboxescanbemanaged.Ifaparticularboxcontainsabugorsecurityvulnerability,distributorsmayupdatetheirboxestofixtheseissues,orcontainnewfunctionality.Thiscanbeuseful,asitsavesustheneedtoupdateourprovisioningconfigurationtomaketheseupdates.

WheninaVagrantprojectsfolder,wecanrunthefollowingcommandtocheckforupdatesfortheprojectsboxandupdateit:

vagrantboxupdate

Thiswilldownloadthenewbox;however,wewon’tseetheeffectofthenewboxunlesswedestroyourVagrantmachineandrebuilditfromtheupdatedbox.

Ifwewanttoupdateaspecificbox,asopposedtotheonethatistiedtotheprojectwearein,wecanusetheboxflagtoprovidethenameoftheboxwewanttoupdate:

vagrantboxupdate--boxthe-box/name

Page 228: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CheckingforoutdatedboxesWecanquicklychecktoseewhetheranyoftheboxesweinstalledareoutofdate,byrunningthefollowingcommand:

vagrantboxoutdated--global

Ifweomittheglobalflag,thenthecommandisonlywithinthecontextofthecurrentVagrantprojectwiththeflagitrelatestoallboxesinstalled:

Page 229: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 230: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

DistributingboxesTodistributeboxeswithAtlas,weneedtocreateanaccountandlogintotheAtlaswebsite(https://atlas.hashicorp.com/account/new).Theusernamethatweselectwhenregisteringisusedastheprefixforboxeswedistribute—unless,ofcourse,wegoontoapaidplan,whichhasorganizationalsupport,orwecollaboratewithothersonabox.Onceloggedin,weneedtoclickontheCreateBoxlinktogototheboxcreationform(https://atlas.hashicorp.com/boxes/new).

Onthispage,weneedtoprovideanameanddescriptionforourbox.Asweareonthefreeplan,wecannotmakethisaprivatebox,soitwillbemadepublic:

AstheboxesdistributedthroughAtlascanbeversioned,toletusrolloutnewupdatestousersofthebox,weneedtocreateaninitialversionforthebox,alongwithadescriptionofwhattheversioncontains:

Page 231: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Next,weneedtoclickonCreatenewprovidertoaddanewproviderthatissupportedbythisversionofthebox:

Finally,wespecifytheprovider,andprovideaURLtowheretheboxcanbedownloaded.WiththefreeversionofAtlas,weneedtoprovidealinktothebox,asthereisnostorageallowanceforVagrantCloudtohostthefileforus:

Page 232: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Onceaboxhasbeencreatedandpublished,itcanbediscoveredandinstalled,aswediscussedintheDiscoveringboxessection,bythepublic,orbyususingthenameoftheboxinourVagrantfile,forexample,mkpeacock/testbox.

Page 233: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 234: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SharingandconnectingwithAtlasWithAtlas,therearethreenewVagrantcommandsatourdisposal,whichareasfollows:

vagrantconnect

vagrantshare

vagrantlogin

Page 235: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

LoggingVagrantintoVagrantCloudInordertoshareourVagrantenvironment,weneedtoconnectourVagrantinstallationtoourVagrantCloudaccount.Wecanchecktoseewhetherthisisalreadythecasebyrunningthefollowingcommand:

vagrantlogin--check

Thiswillchecktoseewhetherwearealreadyloggedin:

Aswearenotloggedin,weneedtorunvagrantlogininordertologin.First,wearepromptedforourusernameore-mailaddressfromAtlas,andthenforourpassword:

Onceloggedin,wecanusethelogoutflagtologoutofAtlas:

vagrantlogin--logout

Page 236: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SharingaVagrantvirtualmachineoverHTTP(S)InordertosharethewebinterfacewithaVagrantvirtualmachine,thevirtualmachinemusteitherhaveitsownIPaddressonourlocalnetworkoranHTTP(S)portforwardedfromtheguesttothehostmachine.Vagrantrequiresthissothatyourhostmachinecanconnecttotherelevantportonyourvirtualmachine.

ProvidedwehaveeithergiventhevirtualmachineitsownnetworkaddressorforwardaporttoarecognizableHTTP(S)port,thenwecanrunthevagrantsharecommandtocreateapublicURLforthismachine.WecanalsospecifytheHTTPandHTTPSportsthatweareusingonthevirtualmachineifVagrantdoesn’tdetectthemwiththe--httpand--httpsflags:

Afterrunningvagrantshare,VagrantwillgenerateanameandURLtoaccessthesharefrom.Asweareonafreeplan,wecannotcustomizeorreserveURLs.Ourterminalsessionisnowlockedtorunthissharingsession,soweneedtoleavethisrunning.IfwevisittheURLinabrowser,weshouldbeabletoseewhateverwebservicewearerunningonourvirtualmachine:

Tostopsharing,weneedtoclosetheterminalorstopthevagrantsharecommandfromrunning:

Page 237: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 238: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SharingandconnectingtoaVagrantvirtualmachineBydefault,vagrantshareonlysharesHTTP(S)traffic.Wecan,however,passthe--sshflagtoalsoshareSSHaccessthatwillallowotherVagrantuserstoconnecttothemachine:

vagrantshare--ssh

Afterrunningthiscommand,wewillbepromptedtoprovideandconfirmapasswordtobeusedtoencrypttheSSHkeysothattheusersarerequiredtoprovideapasswordwhentheyconnect.Ifrequired,thiscanbeomittedwiththe--ssh-no-passwordflaginsteadof--ssh.WecanalsomakeasingleuseSSHconnectionwith--ssh-once,asshowninthefollowingscreenshot:

Aswitharegularsharecommand,wegetaURLandaname.WecanpreventHTTP(S)frombeingsharedbypassingthe--disable-httpflag.

Oncethesharingprocessisrunning,wecanprovidethenameandpasswordtowhomeverwewanttobeabletoconnecttothemachine.Theysimplyrunthevagrantconnect--sshdifficult-elephant-4464command(wherethelastparameteristhenameoftheconnectiongeneratedbyAtlas)tostartaconnectionwiththemachine,andprovidethepasswordwhenprompted:

Page 239: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

TheuseristhenloggedintotheVagrantmachine!

Page 240: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 241: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,welearnedabouttheextrafunctionalityofferedbytheVagrantCloudservice.

Wediscoveredhowtofindthird-partyVagrantboxesforusewithourprojects,howtocheckforupdatesforboxesthatuseAtlas,andhowtodistributeourownbaseboxesthroughAtlas.Finally,welookedatauthenticatingwithAtlastoshareourVagrantenvironmentwithourcolleagues.

NowthatweknowmoreaboutthefunctionalityofferedbyVagrant,wecanuseiteffectivelyinourprojects!

Page 242: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 243: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

AppendixA.ASampleLEMPStackNowthatwehaveagoodknowledgeofusingVagranttomanagesoftwaredevelopmentprojectsandhowtousethePuppetprovisioningtool,let’stakealookathowtousethesetoolstobuildaLinux,Nginx,MySQL,andPHP(LEMP)developmentenvironmentwithVagrant.

Inthisappendix,youwilllearnthefollowingtopics:

HowtoupdatethepackagemanagerHowtocreateaLEMP-baseddevelopmentenvironmentinVagrant,includingthefollowing:

HowtoinstalltheNginxwebserverHowtocustomizetheNginxconfigurationfileHowtoinstallPHPHowtoinstallandconfigureMySQLHowtoinstalle-mailsendingservices

WiththeexceptionofMySQL,wewillcreatesimplePuppetmodulestoinstallandmanagethesoftwarerequired.ForMySQL,wewillusetheofficialPuppetmodulefromPuppetLabs;thismodulemakesitveryeasyforustoinstallandconfigureallaspectsofMySQL.

Page 244: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingtheVagrantprojectFirst,wewanttocreateanewproject,solet’screateanewfoldercalledlemp-stackandinitializeanewubuntu/trusty64Vagrantprojectwithinitbyexecutingthefollowingcommands:

mkdirlemp-stack

cdlemp-stack

vagrantinitubuntu/trusty64ub

TheeasiestwayforustopullintheMySQLPuppetmoduleistosimplyadditasagitsubmoduletoourproject.Inordertoaddagitsubmodule,ourprojectneedstobeagitrepository,solet’sinitializeitasagitrepositorynowtosavetimelater:

gitinit

Tomakethevirtualmachinereflectiveofareal-worldproductionserver,insteadofforwardingthewebserverportonthevirtualmachinetoanotherportonourhostmachine,wewillinsteadnetworkthevirtualmachine.Thismeansthatwewouldbeabletoaccessthewebserverviaport80(whichistypicalonaproductionwebserver)byconnectingdirectlytothevirtualmachine.

InordertoensureafixedIPaddresstowhichwecanallocateahostnameonournetwork,weneedtouncommentthefollowinglinefromourVagrantfilebyremovingthe#fromthestartoftheline:

#config.vm.network"private_network",ip:"192.168.33.10"

TheIPaddresscanbechangeddependingontheneedsofourproject.

AsthisisasampleLEMPstackdesignedforweb-basedprojects,let’sconfigureourprojectsdirectorytoarelevantwebfolderonthevirtualmachine:

config.vm.synced_folder".","/var/www/project",type:"nfs"

Wewillstillneedtoconfigureourwebservertopointtothisfolder;however,itismoreappropriatethanthedefaultmappinglocationof/vagrant.

BeforewerunourPuppetprovisionertoinstallourLEMPstack,weshouldinstructVagranttoruntheapt-getupdatecommandonthevirtualmachine.Withoutthis,itisn’talwayspossibletoinstallnewpackages.So,let’saddthefollowinglinetoourVagrantfilewithinthe|config|block:

config.vm.provision"shell",inline:"apt-getupdate"

AswewillputourPuppetmodulesandmanifestsinaprovisionfolder,weneedtoconfigureVagranttousethecorrectfoldersforourPuppetmanifestsandmodulesaswellasthedefaultmanifestfile.AddingthefollowingcodetoourVagrantfilewilldothisforus:

config.vm.provision:puppetdo|puppet|

puppet.manifests_path="provision/manifests"

Page 245: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

puppet.module_path="provision/modules"

puppet.manifest_file="vagrant.pp"

end

Page 246: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 247: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CreatingthePuppetmanifestsLet’sstartbycreatingsomefoldersforourPuppetmodulesandmanifestsbyexecutingthefollowingcommands:

mkdirprovision

cdprovision

mkdirmodules

mkdirmanifests

Foreachofthemoduleswewanttocreate,weneedtocreateafolderwithintheprovision/modulesfolderforthemodule.Withinthisfolder,weneedtocreateamanifestsfolder,andwithinthis,ourPuppetmanifestfile,init.pp.Structurally,thislookssomethinglikethefollowing:

|--provision

||--manifests

||`--vagrant.pp

|`--modules

||--ourmodule

||--manifests

|`--init.pp

`--Vagrantfile

Page 248: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingNginxLet’stakealookatwhatisinvolvedtoinstallNginxthroughamoduleandmanifestfileprovision/modules/nginx/manifests/init.pp.First,wedefineourclass,passinginavariablesothatwecanchangetheconfigurationfileweuseforNginx(usefulforusingthesamemodulefordifferentprojectsordifferentenvironmentssuchasstagingandproductionenvironments),thenweneedtoensurethatthenginxpackageisinstalled:

classnginx($file='default'){

package{"nginx":

ensure=>present

}

TipNotethatwehavenotclosedthecurlybracketforthenginxclass.Thatisbecausethisisjustthefirstsnippetofthefile;wewillcloseitattheend.

BecausewewanttochangeourdefaultNginxconfigurationfile,weshouldupdatethecontentsoftheNginxconfigurationfilewithoneofourown(thiswillneedtobeplacedintheprovision/modules/nginx/filesfolder;unlessthefileparameterispassedtotheclass,thefiledefaultwillbeused):

file{'/etc/nginx/sites-available/default':

source=>"puppet:///modules/nginx/${file}",

owner=>'root',

group=>'root',

notify=>Service['nginx'],

require=>Package['nginx']

}

Finally,weneedtoensurethatthenginxserviceisactuallyrunningonceithasbeeninstalled:

service{"nginx":

ensure=>running,

require=>Package["nginx"]

}

}

Thiscompletesthemanifest.Wedostill,however,needtocreateadefaultconfigurationfileforNginx,whichissavedasprovision/modules/nginx/files/default.Thiswillbeusedunlesswepassafileparametertothenginxclasswhenusingthemodule.Thesamplefilehereisabasicconfigurationfile,pointingtothepublicfolderwithinoursyncedfolder.Theservernameoflemp-stack.localmeansthatNginxwilllistenforrequestsonthathostnameandwillservecontentfromourprojectsfolder:

server{

listen80;

root/var/www/project/public;

indexindex.phpindex.htmlindex.htm;

Page 249: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

server_namelemp-stack.local;

location/{

try_files$uri$uri//index.php?$query_string;

}

location~\.php${

try_files$uri=404;

fastcgi_split_path_info^(.+\.php)(/.+)$;

#fastcgi_pass127.0.0.1:9000;

fastcgi_paramSERVER_NAME$host;

fastcgi_passunix:/var/run/php5-fpm.sock;

fastcgi_indexindex.php;

fastcgi_intercept_errorson;

includefastcgi_params;

}

location~/\.ht{

denyall;

}

location~*\.(jpg|jpeg|gif|css|png|js|ico|html)${

access_logoff;

expiresmax;

}

location~*\.svgz{

add_headerContent-Encoding"gzip";

}

}

TipBecausethisconfigurationfilelistensforrequestsonlemp-stack.local,weneedtoaddarecordtothehostsfileonourhostmachine,whichwillredirecttrafficfromlemp-stack.localtotheIPaddressofourvirtualmachine.

Page 250: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingPHPToinstallPHP,weneedtoinstallarangeofrelatedpackages,includingtheNginxPHPmodule.Thiswouldbeinthefileprovision/modules/php/manifests/init.pp.

Onmorerecent(withinthepastfewyears)LinuxandPHPinstallations,PHPusesahandlercalledphp-fpmasabridgebetweenPHPandthewebserverbeingused.ThismeansthatwhennewPHPmodulesareinstalledorPHPconfigurationsarechanged,weneedtorestartthephp-fpmserviceforthesechangestotakeeffect,whereasinthepast,itwasoftenthewebserversthatneededtoberestartedorreloaded.

TomakeoursimplePHPPuppetmoduleflexible,weneedtoinstallthephp5-fpmpackageandrestartitwhenothermodulesareinstalled,butonlywhenweuseNginxonourserver.Toachievethis,wecanuseaclassparameter,whichdefaultstotrue.Thisletsususethesamemoduleinserversthatdon’thaveawebserver,andwherewedon’twanttohavetheoverheadoftheFPMservice,suchasaserverthatrunsbackgroundjobsorprocessing:

classphp($nginx=true){

Ifthenginxparameteristrue,thenweneedtoinstallphp5-fpm.Sincethispackageisonlyinstalledwhentheflagissettotrue,wecannothavePHPanditsmodulesrequiringornotifyingthephp-fpmpackage,asitmaynotbeinstalled;soinsteadweneedtohavethephp5-fpmpackagesubscribetothesepackages:

if($nginx){

package{"php5-fpm":

ensure=>present,

subscribe=>[Package['php5-dev'],Package['php5-curl'],

Package['php5-gd'],Package['php5-imagick'],Package['php5-mcrypt'],

Package['php5-mhash'],Package['php5-pspell'],Package['php5-json'],

Package['php5-xmlrpc'],Package['php5-xsl'],Package['php5-mysql']]

}

}

TherestofthemanifestcanthensimplybetheinstallationofthevariousPHPmodulesthatarerequiredforatypicalLEMPsetup:

package{"php5-dev":

ensure=>present

}

package{"php5-curl":

ensure=>present

}

package{"php5-gd":

ensure=>present

}

package{"php5-imagick":

ensure=>present

}

Page 251: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

package{"php5-mcrypt":

ensure=>present

}

package{"php5-mhash":

ensure=>present

}

package{"php5-pspell":

ensure=>present

}

package{"php5-xmlrpc":

ensure=>present

}

package{"php5-xsl":

ensure=>present

}

package{"php5-cli":

ensure=>present

}

package{"php5-json":

ensure=>present

}

}

Page 252: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 253: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingtheMySQLmoduleBecausewearegoingtousethePuppetmoduleforMySQLprovidedbyPuppetLabs,installingthemoduleisverystraightforward;wesimplyadditasagitsubmoduletoourprojectwiththefollowingcommand:

gitsubmoduleaddhttps://github.com/puppetlabs/puppetlabs-mysql.git

provision/modules/mysql

TipYoumightwanttouseaspecificreleaseforthismodule,asthecodechangesonasemi-regularbasis.Astablereleaseisavailableathttps://github.com/puppetlabs/puppetlabs-mysql/releases/tag/3.1.0.

Page 254: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 255: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

DefaultmanifestFinally,weneedtopullthesemodulestogether,andinstallthemwhenourmachineisprovisioned.Todothis,wesimplyaddthefollowingmodulestoourvagrant.ppmanifestfileintheprovision/manifestsfolder.

Page 256: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

InstallingNginxandPHPWeneedtoincludeournginxclassandoptionallyprovideafilenamefortheconfigurationfile;ifwedon’tprovideone,thedefaultwillbeused:

class{

'nginx':

file=>'default'

}

SimilarlyforPHP,weneedtoincludetheclassandinthiscase,passannginxparametertoensurethatitinstallsPHP5-FPMtoo:

class{

'php':

nginx=>true

}

Page 257: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

HostnameconfigurationWeshouldtellourVagrantvirtualmachinewhatitshostnameisbyaddingahostresourcetoourmanifest:

host{'lemp-stack.local':

ip=>'127.0.0.1',

host_aliases=>'localhost',

}

Page 258: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

E-mailsendingservicesBecausesomeofourprojectsmightinvolvesendinge-mails,weshouldinstalle-mailsendingservicesonourvirtualmachine.Asthesearesimplytwopackages,itmakesmoresensetoincludetheminourVagrantmanifest,asopposedtotheirownmodules:

package{"postfix":

ensure=>present

}

package{"mailutils":

ensure=>present

}

Page 259: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

MySQLconfigurationBecausetheMySQLmoduleisveryflexibleandmanagesallaspectsofMySQL,thereisquiteabitforustoconfigure.Weneedtoperformthefollowingsteps:

1. Createadatabase.2. Createauser.3. Givetheuserpermissiontousethedatabase(grants).4. ConfiguretheMySQLrootpassword.5. InstalltheMySQLclient.6. InstalltheMySQLclientbindingsforPHP.

TheMySQLserverclasshasarangeofparametersthatcanbepassedtoconfigureit,includingdatabases,users,andgrants.So,first,weneedtodefinewhatthedatabases,users,andgrantsarethatwewanttobeconfigured:

$databases={

'lemp'=>{

ensure=>'present',

charset=>'utf8'

},

}

$users={

'lemp@localhost'=>{

ensure=>'present',

max_connections_per_hour=>'0',

max_queries_per_hour=>'0',

max_updates_per_hour=>'0',

max_user_connections=>'0',

password_hash=>'MySQL-Password-Hash',

},

}

NoteThepassword_hashparameterhereisforahashgeneratedbyMySQL.YoucangenerateapasswordhashbyconnectingtoanexistingMySQLinstanceandrunningaquerysuchasSELECTPASSWORD('password').

Thegrantmapsouruseranddatabaseandspecifieswhatpermissionstheusercanperformonthatdatabasewhenconnectingfromaparticularhost(inthiscase,localhost—sofromthevirtualmachineitself):

$grants={

'lemp@localhost/lemp.*'=>{

ensure=>'present',

options=>['GRANT'],

privileges=>['ALL'],

table=>'lemp.*',

user=>'lemp@localhost',

},

Page 260: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

}

WethenpassthesevaluestotheMySQLserverclass.WealsoprovidearootpasswordforMySQL(unlikeearlier,thisisprovidedinplaintext),andwecanoverridetheoptionsfromtheMySQLconfigurationfile.ThisisunlikeourownNginxmodulethatprovidesafullfile—inthisinstance,theMySQLmoduleprovidesatemplateconfigurationfileandthechangesarereplacedinthattemplatetocreateaconfigurationfile:

class{'::mysql::server':

root_password=>'lemp-root-password',

override_options=>{'mysqld'=>{'max_connections'=>'1024'}},

databases=>$databases,

users=>$users,

grants=>$grants,

restart=>true

}

Aswewillhaveawebserverrunningonthismachine,whichneedstoconnecttothisdatabaseserver,wealsoneedtheclientlibraryandtheclientbindingsforPHP,sothatwecanincludethemtoo:

include'::mysql::client'

class{'::mysql::bindings':

php_enable=>true

}

Page 261: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 262: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

LaunchingthevirtualmachineInordertolaunchournewvirtualmachine,wesimplyneedtorunthefollowingcommand:

Vagrantup

AsperChapter6,ProvisioningVagrantMachineswithPuppet,Ansible,andChef,weshouldnowseeourVMbootandthevariousPuppetphasesexecute.Ifallgoeswell,weshouldseenoerrorsinthisprocess.

Page 263: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author
Page 264: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

SummaryInthischapter,welearnedaboutthestepsinvolvedincreatingabrandnewVagrantproject,configuringittointegratewithourhostmachine,andsettingupastandardLEMPstackusingthePuppetprovisioningtool.NowyoushouldhaveabasicunderstandingofVagrantandhowtouseittoensurethatyoursoftwareprojectsaremanagedmoreeffectively!

Page 265: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

IndexA

Ansibleabout/UnderstandingAnsibleplaybooks/UnderstandingAnsibleinstalling/InstallingAnsibleoperatingsystems,URL/InstallingAnsibleinventory,creating/Creatinganinventoryplaybooks,creating/CreatingAnsibleplaybooksmodules/Modules–whatAnsiblecandosoftware,installing/Installingsoftwarecronmodule/Managingcroncommands,running/Runningcommandsusermodule,URL/Managingusersandgroupsgroupmodule,URL/Managingusersandgroupsroles,using/UsingAnsiblerolesGalaxy,URL/UsingAnsiblerolesused,forserverprovision/UsingAnsibletoprovisionserversprovisioningwith/ProvisioningwithAnsibleonVagrant

Ansible,filemanagementabout/Understandingfilemanagementfile,copying/Copyingafilesymlink,creating/Creatingasymlinkfolders,creating/Creatingfoldersgroupmodule,creating/Creatinggroupsusermodule,creating/Creatingusers

Ansiblesoftware,installingpackagemanager,updating/Updatingourpackagemanagernginxpackage,installing/Installingthenginxpackagenginxservice,running/Runningthenginxservice

aptmoduleURL/Updatingourpackagemanager,Installingthenginxpackage

Atlasboxes,discovering/Discoveringboxesboxes,URL/Discoveringboxesnewboxes,installing/Installingnewboxesexistingboxes,updating/Updatingexistingboxesoutdatedboxes,checkingfor/Checkingforoutdatedboxesaccount,URL/Distributingboxesboxes,distributing/Distributingboxesboxcreationform,URL/Distributingboxessharingwith/SharingandconnectingwithAtlas

Page 266: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

connectingwith/SharingandconnectingwithAtlasVagrant,connectingtoVagrantCloud/LoggingVagrantintoVagrantCloudVagrantvirtualmachine,sharingoverHTTP(S)/SharingaVagrantvirtualmachineoverHTTP(S)Vagrantvirtualmachine,connectingto/SharingandconnectingtoaVagrantvirtualmachineVagrantvirtualmachine,sharingto/SharingandconnectingtoaVagrantvirtualmachine

authentication,Vagrantabout/Vagrantauthenticationadmingroup/Vagrantuserandadmingroupusergroup/Vagrantuserandadmingroupsudoersfile/Thesudoersfile

Page 267: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

CChef

about/KnowingaboutChefcookbooks,creatingwith/CreatingcookbooksandrecipeswithChefrecipes,creatingwith/CreatingcookbooksandrecipeswithChefresources/Resources–whatChefcandocronresourcetype,managing/Managingcroncommands,running/Runningcommandsusers,managing/Managingusersandgroupsgroups,managing/Managingusersandgroupsgroups,creating/Creatinggroupsusers,creating/Creatinguserssudoersfile,updating/Updatingthesudoersfilecommonresourcefunctionalities/Knowingcommonresourcefunctionalitiesused,forprovisioningservers/UsingCheftoprovisionserversURL,fortutorial/UsingCheftoprovisionserversonVagrant,provisioningwith/ProvisioningwithChefonVagrant-solousing/UsingChef-soloroles,URL/UsingChef-solousing,inclient/servermode/UsingChefinclient/servermodeinstalling/InstallingChef

Chef,filemanagementabout/Understandingfilemanagementfile,copying/Copyingafilesymlink,creating/Creatingasymlinkfolders,creating/Creatingfoldersmultiplefolders,creatinginsingleprocesswithlooping/Creatingmultiplefoldersinasingleprocesswithlooping

Chef,resourcetypescron/Resources–whatChefcandoexecute/Resources–whatChefcandofile/Resources–whatChefcandogroup/Resources–whatChefcandopackage/Resources–whatChefcandoservice/Resources–whatChefcandotemplate/Resources–whatChefcandouser/Resources–whatChefcandoURL/Resources–whatChefcando

Chef,softwareinstallingabout/Installingsoftwarepackagemanager,updating/Updatingourpackagemanagernginxpackage,installing/Installingthenginxpackagenginxservice,running/Runningthenginxservice

Page 268: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Chef-solousing/UsingChef-solo

Chef-soloconfigurationURL/UsingCheftoprovisionservers

Chefcookbooksusing/UsingChefcookbooks

Chefrun,anatomyURL/UsingCheftoprovisionservers

client/servermodePuppetusing/UsingPuppetinclient/servermode

commandsautorunning/Autorunningcommands

configurableclasses,Puppetcreating/Creatingconfigurableclasses

cookbookscreating,withChef/CreatingcookbooksandrecipeswithChef

cronmoduleURL/Managingcron

Page 269: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Ffilemodule

URL/Creatingasymlink

Page 270: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Ggroupmodule

URL/ManagingusersandgroupsGuestAdditions,VirtualBox

about/VirtualBoxGuestAdditions

Page 271: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Hhostandguestmachineinteraction,managing

about/Managingintegrationbetweenhostandguestmachinesportforwarding/Portforwardingsyncedfolders/Syncedfoldersnetworking/Networking

hostnameconfiguring/Hostnameconfiguration

Page 272: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Iinventory,Ansible

creating/Creatinganinventory

Page 273: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

LLEMPserver/Updatingourpackagemanager

Page 274: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Mmanifests

about/Defaultmanifestnginx,installing/InstallingNginxandPHPPHP,installing/InstallingNginxandPHPhostnameconfiguration/Hostnameconfiguratione-mailsendingservices/E-mailsendingservicesMySQLconfiguration/MySQLconfiguration

modules,Ansibleabout/Modules–whatAnsiblecandoApt/Modules–whatAnsiblecandoGit/Modules–whatAnsiblecandoService/Modules–whatAnsiblecandoCopy/Modules–whatAnsiblecandoURL/Modules–whatAnsiblecando

multimachineprojectdestroying/Destroyingamultimachineproject

multipleprovisionersusing,onsingleproject/Usingmultipleprovisionersonasingleproject

multiplevirtualmachinesusing,withVagrant/UsingmultiplemachineswithVagrantdefining/Definingmultiplevirtualmachinesconnecting,overSSH/ConnectingtothemultiplevirtualmachinesoverSSHnetworking/Networkingthemultiplevirtualmachinesprovisioning/Provisioningthemachinesseparately

MySQLinstalling/InstallingtheMySQLmoduleconfiguring/MySQLconfiguration

Page 275: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

NNetworkFileSystem(NFS)/Syncedfoldersnetworking

about/Networkingnginx

installing/InstallingNginx,InstallingNginxandPHPnotifyparameter/Thenotify,subscribe,andrefreshonlyparameters

Page 276: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

OOpscodecommunitysite,Chefcookbooks

URL/UsingChefcookbooks

Page 277: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Ppackagesubcommand/Exportparameters/ResourcesPHP

installing/InstallingPHP,InstallingNginxandPHPportforwarding

about/Portforwardingproject

creating/CreatingourfirstVagrantprojectprovisioners

about/ProvisionersPuppet,installing/InstallingPuppetChef,installing/InstallingChef

provisioningabout/ProvisioningwithVagrant/ProvisioningwithinVagrantwithPuppet,onVagrant/ProvisioningwithPuppetonVagrantwithAnsible,onVagrant/ProvisioningwithAnsibleonVagrantwithAnsible,onVagrant/ProvisioningwithAnsibleonVagrantwithChef,onVagrant/ProvisioningwithChefonVagrantwithSSH/ProvisioningwithSSH–arecapoverriding,viacommandline/Overridingprovisioningviathecommandline

provisioning,optionsShell/AutorunningcommandsPuppet/AutorunningcommandsAnsible/AutorunningcommandsChef/Autorunningcommands

Puppetabout/Puppetidempotentfeature/Puppetused,forcreatingmodules/CreatingmodulesandmanifestswithPuppetused,forcreatingmanifests/CreatingmodulesandmanifestswithPuppetclasses/Puppetclassesdefaultmanifests/DefaultPuppetmanifestsresource/Resourcesnotifyparameter/Thenotify,subscribe,andrefreshonlyparameterssubscribeparameter/Thenotify,subscribe,andrefreshonlyparametersrefreshonlyparameter/Thenotify,subscribe,andrefreshonlyparametersfilemanagement/Filemanagementcronresourcetype,managing/Cronmanagementcommands,running/Runningcommandsuserresourcetype,managing/Managingusersandgroups,Creatingusersgroupresourcetype,managing/Managingusersandgroups

Page 278: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

userresourcetype,URL/Managingusersandgroupssudoersfile,updating/Updatingthesudoersfileconfigurableclasses,creating/Creatingconfigurableclassesmodules/Puppetmodulesused,forserverprovision/UsingPuppettoprovisionserversprovisioningwith,onVagrant/ProvisioningwithPuppetonVagrantusing,instandalonemode/UsingPuppetinstandalonemodeprovisioning,inaction/Puppetprovisioninginactionusing,inclient/servermode/UsingPuppetinclient/servermodeURL/UsingPuppetinclient/servermodeblog,URL/UsingPuppetinclient/servermodeinstalling/InstallingPuppet

Puppet,filemanagementabout/Filemanagementfile,copying/Copyingafilesymlink,creating/Creatingasymlinkfolders,creating/Creatingfoldersmultiplefolders,creating/Creatingmultiplefoldersinonego

Puppet,resourcetypes/Resourcestypes,URL/Resourcesrequisites/Resourcerequirementsexecution,ordering/Resourceexecutionorderingexecuting,instages/Executingresourcesinstages

Puppet,resourcetypescron/Resourcesexec/Resourcesfile/Resourcesgroup/Resourcespackage/Resourcesservice/Resourcesuser/Resources

PuppetForgeURL/Puppetmodules

Puppetmanifestscreating/CreatingthePuppetmanifestsnginx,installing/InstallingNginxPHP,installing/InstallingPHPMySQLmodule,installing/InstallingtheMySQLmoduleURL/InstallingtheMySQLmodule

Puppetsoftware,installingabout/Installingsoftwarepackagemanager,updating/Updatingourpackagemanagernginxpackage,installing/Installingthenginxpackage

Page 279: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

nginxservice,running/RunningthenginxservicePuTTY

URL/ConnectingtothevirtualmachineoverSSH

Page 280: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Rrecipes

creating,withChef/CreatingcookbooksandrecipeswithChefrecipes,Chef

URL/UsingCheftoprovisionserversrefreshonlyparameter/Thenotify,subscribe,andrefreshonlyparameters

Page 281: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Sservers

provisioning,Chefused/UsingCheftoprovisionserversSSH

provisioningwith/ProvisioningwithSSH–arecapmultiplevirtualmachines,connectingto/ConnectingtothemultiplevirtualmachinesoverSSH

SSHprovisioningabout/Autorunningcommands

standalonemodePuppetusing/UsingPuppetinstandalonemode

subscribeparameter/Thenotify,subscribe,andrefreshonlyparameterssudoersfile

URL/Thesudoersfileabout/Thesudoersfile

/Thesudoersfilesupervisord

URL/CreatingmodulesandmanifestswithPuppetabout/DefaultPuppetmanifests

supervisormoduleURL/DefaultPuppetmanifests

symlinkcreating/Creatingasymlink

syncedfoldersabout/Syncedfolders

Page 282: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Ttemplatemodule

URL/Copyingafile

Page 283: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Uusermodule

URL/Managingusersandgroups,Creatingusers

Page 284: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

VVagrant

about/IntroducingVagrant,ToomanyVagrants!URL/IntroducingVagrant,RequirementsforVagrant,ConnectingtothevirtualmachineoverSSHconfigurationfile/IntroducingVagrantrequisites/RequirementsforVagrantVirtualBox,installing/InstallingVirtualBoxinstalling/InstallingVagrantdownloadpage/InstallingVagrantproject,creating/CreatingourfirstVagrantproject,CreatingtheVagrantproject-controlledguestmachines,controlling/ManagingVagrant-controlledguestmachinesvirtualmachine,poweringup/PoweringupaVagrant-controlledvirtualmachinevirtualmachine,suspending/Suspendingavirtualmachinevirtualmachine,resuming/Resumingavirtualmachinevirtualmachine,shuttingdown/Shuttingdownavirtualmachineprovisioning,options/Autorunningcommandsprovisioningwith/ProvisioningwithinVagrantprovisioning,withChef/ProvisioningwithChefonVagrantmultiplevirtualmachines,using/UsingmultiplemachineswithVagrantdocumentation,URL/Gettingstartednewbasebox,URL/Gettingstartedauthentication/Vagrantauthentication

Vagrantauthenticationinsecurepublickeypair/Insecurepublic/privatekeypairinsecureprivatekeypair/Insecurepublic/privatekeypair

Vagrantboxesmanaging/ManagingVagrantboxesaddsubcommand/ManagingVagrantboxes,AddingVagrantboxeslistsubcommand/ManagingVagrantboxes,ListingVagrantboxesoutdatedsubcommand/ManagingVagrantboxes,Checkingforupdatesremovesubcommand/ManagingVagrantboxes,RemovingVagrantboxesrepackagesubcommand/ManagingVagrantboxes,RepackagingaVagrantboxupdatesubcommand/ManagingVagrantboxes,Updatingthecurrentenvironment’sboxadding/AddingVagrantboxeslisting/ListingVagrantboxesupdates,checkingfor/Checkingforupdatesremoving/RemovingVagrantboxesrepackaging/RepackagingaVagrantboxcurrentenvironmentsbox,updating/Updatingthecurrentenvironment’sbox

Page 285: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

Vagrantboxes,Atlasdiscovering/Discoveringboxesnewboxes,installing/Installingnewboxesexistingboxes,updating/Updatingexistingboxesoutdatedboxes,checkingfor/Checkingforoutdatedboxesdistributing/Distributingboxes

Vagrantvirtualmachine,Atlassharing,overHTTP(S)/SharingaVagrantvirtualmachineoverHTTP(S)

VirtualBoxinstalling/InstallingVirtualBox

VirtualBoxmachinepreparing/PreparingtheVirtualBoxmachine

virtualmachinecleaningup/CleaninguptheVMlaunching/Launchingthevirtualmachine

virtualmachine,Vagrant-controlledmanaging/ManagingVagrant-controlledguestmachinespoweringup/PoweringupaVagrant-controlledvirtualmachinesuspending/Suspendingavirtualmachineresuming/Resumingavirtualmachineshuttingdown/Shuttingdownavirtualmachinestarting,fromscratch/StartingfromscratchoffVagrantfilechanges,updating/UpdatingbasedonVagrantfilechangesconnectingto,overSSH/ConnectingtothevirtualmachineoverSSH

Page 286: Creating Development Environments with - DropPDF3.droppdf.com/files/i2sVq/packt-publishing... · Creating Development Environments with Vagrant Second Edition Credits About the Author

YYamlAin’tMarkupLanguage(YAML)

about/UnderstandingAnsible