Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Vagrant beyond the basics

#1
Vagrant beyond the basics

<div><p>There are, like most things in the Unix/Linux world, many ways of doing things with Vagrant, but here are some examples of ways to grow your Vagrantfile portfolio and increase your knowledge and use.</p>
<p> <span id="more-32249"></span> </p>
<p>If you have not yet installed vagrant you can follow the first part of this series.</p>
<figure class="wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-fedora-magazine">
<div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="z6YO8k3AL4"><p><a href="https://fedoramagazine.org/vagrant-qemukvm-fedora-devops-sysadmin/">Installing and running Vagrant using qemu-kvm</a></p></blockquote>
</div>
</figure>
<h3>Some Vagrantfile basics</h3>
<p>All Vagrantfiles start with “<strong>Vagrant.configure(“2”) do |config|</strong>” and finish with a corresponding “<strong>end</strong>”:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">Vagrant.configure(&quot;2&quot;) do |config|<br />
&nbsp; ...<br />
&nbsp; ...<br />
end</div></div> </pre>
<p>The “2” represents the version of Vagrant, and is currently either 1 or 2. Unless you need to use the older version simply stick with the latest.</p>
<p>The config structure is broken down into namespaces:</p>
<p><strong>config.vm</strong> – modify the configuration of the machine(s) that Vagrant manages.</p>
<p><strong>config.ssh</strong> – for configuring how Vagrant will access your machine over SSH.</p>
<p><strong>config.winrm</strong> – configuring how Vagrant will access your Windows guest over WinRM.</p>
<p><strong>config.winssh</strong> – the WinSSH communicator is built specifically for the Windows native port of OpenSSH.</p>
<p><strong>config.vagrant</strong> – modify the behavior of Vagrant itself.</p>
<p>Each line in a namespace begins with the word ‘config’:</p>
<p><strong>config.vm.box = “fedora/32-cloud-base”<br />config.vm.network “private_network”</strong></p>
<p>There are many options here, and a read of the documentation pages is strongly recommended. They can be found at <a href="https://www.vagrantup.com/docs/vagrantfile">https://www.vagrantup.com/docs/vagrantfile</a></p>
<p>Also in this section you can configure provider-specific options. In this case the provider is libvirt, and the specific config looks like this:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.provider :libvirt do |libvirt|<br />
&nbsp; libvirt.cpus = 1<br />
&nbsp; libvirt.memory = 512</div></div> </pre>
<p>In the example above, all libvirt VMs will be created with a single CPU and 512Mb of memory unless specifically overridden.</p>
<p>The VM namespace is where you define all machines you want this Vagrantfile to build. Notice that this is still a part of the config section, and lines should therefore begin with ‘config’. All sections or parts of sections have an ‘end’ statement to close them off.</p>
<h3>Creating multiple machines at once</h3>
<p>Depending on what you need to achieve, this can be a simple loop or multiple machine definitions. To create any number of machines in a series, with the same settings but perhaps different names and/or IP addresses, you can just provide a range as shown here:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">(1..5).each do |i|<br />
&nbsp; config.vm.define &quot;server#{i}&quot; do |server|<br />
&nbsp; &nbsp; server.vm.hostname = &quot;server#{i}.example.com&quot;<br />
&nbsp; end<br />
end</div></div> </pre>
<p>This will create 5 servers, named server1, server2, server3 etc.</p>
<p>Of note, using Ruby style “<strong>for i in 1..3 do</strong>” doesn’t work despite Vagrantfile syntax actually being Ruby, so use the method from the example above.</p>
<p>If you need servers with different hostnames, different hardware etc then you’ll need to specify them individually, or at least in groups if the situation lends itself to that. Let’s say you need to create a typical web/db/load balancer infrastructure, with 2 web servers, a single database server and a load balancer for the web traffic. Ignoring the specific software setup for this, to simply create the virtual machines ready for provisioning you could use something like this:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># Load Balancer<br />
config.vm.define &quot;loadbal&quot;, primary: true do |loadbal|<br />
&nbsp; loadbal.vm.hostname = &quot;loadbal&quot;<br />
end<br />
<br />
# Database<br />
config.vm.define &quot;db&quot;, primary: true do |db|<br />
&nbsp; db.vm.hostname = &quot;db&quot;<br />
end<br />
<br />
# Web Servers x2<br />
(1..2).each do |i|<br />
&nbsp; config.vm.define &quot;web#{i}&quot; do |web|<br />
&nbsp; &nbsp; web.vm.hostname = &quot;web#{i}&quot;<br />
&nbsp; end<br />
end</div></div> </pre>
<p>This uses a combination of multiple machine calls and a small loop to build 4 VMs with a single ‘vagrant up’ command.</p>
<h3>Networking</h3>
<p>Vagrant generally creates its own network for VM access, and you use this with ‘vagrant ssh’. If you create more than one VM then you must use the VM name to identify which one you wish to connect to – <strong>vagrant ssh </strong><strong><em>vmname</em></strong>.</p>
<p>There are a number of configuration options available which allow you to interact with your VMs in various ways.</p>
<p>The vagrant-libvirt plugin creates a network for the guests to use. This is automated and will always be present even if you define your own networks. The network is named “vagrant-libvirt” and can be seen either in the Virtual Networks tab of virt-manager’s connection details or by issuing a <strong>sudo virsh net-list</strong> command.</p>
<p>If you use dhcp for your guests, you can find the individual IP addresses with the virsh net-dhcp-list command: <strong>sudo virsh net-dhcp-leases vagrant-libvirt</strong></p>
<h4>Port Forwarding</h4>
<p>The simplest change to default networking is port forwarding. This uses a simple format like most Vagrant config: <strong>config.vm.network “forwarded_port”, guest: 80, host: 8080</strong></p>
<p>This listens to port 8080 on your local machine and forwards connections to port 80 on the Vagrant machine. If you need to use a UDP port, simply add <strong>, protocol: “udp”</strong> to the end of that line (notice that comma which should come immediately after the second port number).</p>
<p>Obviously for more complex configurations this might not be ideal, as you need to specify every single port you want to forward. If you then add multiple machines the complexity can really become too much.</p>
<p>In addition to this, anyone on your network can access these ports if they know your IP address, so that’s something you should be aware of.</p>
<h4>Public Network</h4>
<p>This creates a network card for the Vagrant VM which connects to your host network, and will therefore be visible to all machines on that network. As Vagrant is not designed to be secure, you should be aware of any vulnerabilities and take steps to protect against them.</p>
<p>To configure a public network, add <strong>config.vm.network “public_network”</strong> to your Vagrantfile. This will use DHCP to obtain a network address.</p>
<p>If you wish to assign a static IP address, you can add one to the end of the network declaration: <strong>config.vm.network “public_network”, ip: “192.168.0.1”</strong></p>
<p>If you’re creating multiple guests you can put the network configuration in the vm namespace, and even allocate IPs based on iteration too:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">Vagrant.configure(&quot;2&quot;) do |config|<br />
&nbsp; config.vm.box = &quot;centos/8&quot;<br />
&nbsp; config.vm.provider :libvirt do |libvirt|<br />
&nbsp; &nbsp; libvirt.qemu_use_session = false<br />
&nbsp; end<br />
<br />
&nbsp; # Servers x2<br />
&nbsp; (1..2).each do |i|<br />
&nbsp; &nbsp; config.vm.define &quot;server#{i}&quot; do |server|<br />
&nbsp; &nbsp; &nbsp; server.vm.hostname = &quot;server#{i}&quot;<br />
&nbsp; &nbsp; &nbsp; server.vm.network &quot;public_network&quot;, ip: &quot;192.168.122.20#{i}&quot;<br />
&nbsp; &nbsp; end<br />
&nbsp; end<br />
end</div></div> </pre>
<h4>Private Network</h4>
<p>This works very much like the Public Network option, only the network is only available to the host machine and the Vagrant guests. The syntax is almost identical too: <strong>config.vm.network “private_network”, type: “dhcp”</strong></p>
<p>&nbsp;To use a static IP address, simply add it<strong>:</strong></p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.network &quot;private_network&quot;, ip: &quot;192.168.50.4&quot;</div></div> </pre>
<p>This will create a new network in libvirt, usually named something like “vagrant-private-dhcp” – you can see this with the command <strong>sudo virsh net-list</strong> while the VM is running. This network is created and destroyed along with the vagrant guests.</p>
<p>Again, the network config can be specified for all guests, or per guest as shown in the public network example above.</p>
<h3>Provisioning</h3>
<p>Once you have your VMs defined, you can obviously then do whatever you want with them, but as soon as you issue a ‘vagrant destroy’ command any changes will be lost. This is where automated provisioning comes in.</p>
<p>You can use several methods to provision your machines, from simple file copies to shell scripts, Ansible, Chef and Puppet. Many of the main methods can be used, but I’ll cover the simple ones here – if you need to use something else please read the documentation as it’s all covered.</p>
<h4>File uploads</h4>
<p>To copy a file to the Vagrant guest, add a line to the Vagrantfile like this:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.provision &quot;file&quot;, source: &quot;~/myfile&quot;, destination: &quot;myfile&quot;</div></div> </pre>
<p>You can copy directories too:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.provision &quot;file&quot;, source: &quot;~/path/to/host/folder&quot;, destination: &quot;$HOME/remote/newfolder&quot;</div></div> </pre>
<p>The directory structure should already exist on the Vagrant host, and will be copied in its entirety, including subdirectories and files.</p>
<p>Note: If you add a trailing slash to the destination path, the source path will be placed under this so make sure you only do this if you want that outcome. For example, if the above destination was <strong>“$HOME/remote/newfolder/”,</strong> then the result would see “$HOME/remote/newfolder/folder” created with the contents of the source placed here.</p>
<h4>Shell commands</h4>
<p>You can include individual commands, inline scripts or external scripts to perform provisioning tasks.</p>
<p>A single command would take this form, and any valid command line command can be used here: <strong>config.vm.provision “shell”, inline: “sudo dnf update -y”</strong></p>
<p>An inline script is less common, and declared at the top of the Vagrantfile then called during provisioning:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$script = &amp;lt;&amp;lt;-SCRIPT<br />
echo I am provisioning...<br />
date &gt; /etc/vagrant_provisioned_at<br />
SCRIPT<br />
<br />
Vagrant.configure(&quot;2&quot;) do |config|<br />
&nbsp; config.vm.provision &quot;shell&quot;, inline: $script<br />
end</div></div> </pre>
<p>More common is the external shell script, which gives more flexibility and makes code more modular. Vagrant uploads the file to the guest then executes it. Simply call the script in the provisioning line:</p>
<p><strong>config.vm.provision “shell”, path: “script.sh”</strong></p>
<p>The file need not be local to the Vagrant host either:</p>
<p><strong>config.vm.provision “shell”, path: “https://example.com/provisioner.sh”</strong></p>
<h4>Ansible</h4>
<p>To use Ansible to provision your VMs you must have it installed on the Vagrant host; see <a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-rhel-centos-or-fedora">https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-rhel-centos-or-fedora</a>.</p>
<p>You specify an Ansible playbook to provision your VM in the following way:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.provision &quot;ansible&quot; do |ansible|<br />
&nbsp; ansible.playbook = &quot;playbook.yml&quot;<br />
end</div></div> </pre>
<p>This then calls the playbook, which will run as any externally-run ansible playbook would.</p>
<p>If you’re building multiple VMs with your Vagrantfile then it’s likely you want different configurations for some of them, and in this case you should provision within the definition of each VM, as shown here:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># Web Servers x2<br />
(1..2).each do |i|<br />
&nbsp; config.vm.define &quot;web#{i}&quot; do |web|<br />
&nbsp; &nbsp; web.vm.hostname = &quot;web#{i}&quot;<br />
&nbsp; &nbsp; web.vm.provision &quot;ansible&quot; do |ansible|<br />
&nbsp; &nbsp; &nbsp; ansible.playbook = &quot;web.yml&quot;<br />
&nbsp; &nbsp; end<br />
&nbsp; end<br />
end</div></div> </pre>
<p>Ansible provisioners come in two formats – ansible and ansible_local. The ansible provisioner requires that Ansible is installed on the Vagrant host, and will connect remotely to your guest VMs to provision them. This means all necessary ssh authentication must be in place for it to work. The ansible_local provisioner executes playbooks directly on the guest VMs, which therefore requires Ansible be installed on each of the guests you want to provision. Vagrant will try to install Ansible on the guests in order to do this, (This can be controlled with the <strong>install</strong> option, but is enabled by default). On RHEL-style systems like Fedora, Ansible is installed from the EPEL repository. Simply use either ansible or ansible_local in the <strong>config_vm_provision</strong> command to choose the style you need.</p>
<h3>Synced Folders</h3>
<p>Vagrant allows you to sync folders between your Vagrant host and your guests, allowing access to configuration files, data etc. By default, the folder containing the Vagrant file is shared and mounted under /vagrant on each guest.</p>
<p>To configure additional synced folders, use the <strong>config.vm.synced.folder</strong> command:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.synced_folder &quot;src/&quot;, &quot;/srv/website&quot;</div></div> </pre>
<p>The two parameters are the source folder on the Vagrant host and the mount directory on the guest. The destination folder will be created if it does not exist, recursively if necessary.</p>
<p>Options for synced folders allow you to configure them better, including the option to disable them completely. Other options allow you to specify a group owner of the folder (group), the folder owner (owner), plus mount options. There are others but these are the main ones.</p>
<p>You can disable the default share with the following command:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.synced_folder &quot;.&quot;, &quot;/vagrant&quot;, disabled: true</div></div> </pre>
<p>Other options are configured as follows:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.synced_folder &quot;src/&quot;, &quot;/srv/website&quot;,<br />
&nbsp; owner: &quot;apache&quot;, group: &quot;apache&quot;</div></div> </pre>
<h4>NFS synced folders</h4>
<p>When using Vagrant on a Linux host, synced folders use NFS (with the exception of the default share which uses rsync; see below) so you must have NFS installed on the Vagrant host, and the guests also need NFS support installation. To use NFS with non-Linux hosts, simply specify the folder type as ‘nfs’:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.synced_folder &quot;.&quot;, &quot;/vagrant&quot;, type: &quot;nfs&quot;</div></div> </pre>
<h4>RSync synced folders</h4>
<p>These are the easiest to use as they usually work without any intervention on a Linux host. This is a one-way sync from host to guest performed at startup (<strong>vagrant up</strong>) or after a <strong>vagrant reload</strong> command is issued. The default share of the Vagrant project directory is done with rsync. To configure a synced folder with rsync, specify the type as ‘rsync’:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">config.vm.synced_folder &quot;.&quot;, &quot;/vagrant&quot;, type: &quot;rsync&quot;</div></div> </pre></p>
</div>


https://www.sickgaming.net/blog/2020/12/...he-basics/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016