<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Vagrant</title><link>https://jwheel.org/tags/vagrant/</link><description>Homepage of Justin Wheeler, an Open Source contributor and Free Software advocate from Georgia, USA.</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><managingEditor>Justin Wheeler</managingEditor><lastBuildDate>Tue, 18 Sep 2018 00:00:00 +0000</lastBuildDate><atom:link href="https://jwheel.org/rss/tags/vagrant/index.xml" rel="self" type="application/rss+xml"/><item><title>How to fix missing Python for Ansible in Fedora Vagrant</title><link>https://jwheel.org/blog/2018/09/how-to-fix-missing-python-for-ansible-in-fedora-vagrant/</link><pubDate>Tue, 18 Sep 2018 00:00:00 +0000</pubDate><guid>https://jwheel.org/blog/2018/09/how-to-fix-missing-python-for-ansible-in-fedora-vagrant/</guid><description><![CDATA[<p>Recently, I started to use Vagrant to test Ansible playbooks on Fedora machines. I&rsquo;m using the Fedora 28 cloud base image. However, when I tried to provision my Vagrant box, I was warned the Python binary is missing.</p>
<pre tabindex="0"><code>$ vagrant provision
==&gt; default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [default]: FAILED! =&gt; {&#34;changed&#34;: false, &#34;module_stderr&#34;: &#34;Shared connection to 192.168.121.3 closed.\r\n&#34;, &#34;module_stdout&#34;: &#34;\r\n/bin/sh: /usr/bin/python: No such file or directory\r\n&#34;, &#34;msg&#34;: &#34;MODULE FAILURE&#34;, &#34;rc&#34;: 127}
	to retry, use: --limit @playbook.retry
</code></pre>
<h2 id="problem-python-3-by-default">Problem: Python 3 by default&nbsp;<a class="hanchor" href="#problem-python-3-by-default" aria-label="Anchor link for: Problem: Python 3 by default">🔗</a></h2>
<p>This error appears because Fedora 28 does not provide a Python 2 binary by default. Only Python 3 is provided on the base cloud image. I verified this by SSHing into the Vagrant box.</p>
<pre tabindex="0"><code>[jflory@vagrant-host vagrant]$ vagrant ssh
[vagrant@localhost ~]$ dnf list installed | grep -i python
</code></pre><p>Annoyingly, I must install Python 2 manually in the box each time it fails to provision. Surely, there is an easier way? Fortunately, StackOverflow came <a href="https://stackoverflow.com/questions/47423488/vagrant-ansible-python3">to the rescue</a>.</p>

<h2 id="solution-ansibleextra_vars">Solution: <code>ansible.extra_vars</code>&nbsp;<a class="hanchor" href="#solution-ansibleextra_vars" aria-label="Anchor link for: Solution: ansible.extra_vars">🔗</a></h2>
<p>It&rsquo;s possible to tell Vagrant where the Python binary is located. You can pass the path to the <code>python3</code> binary manually in your Vagrantfile.</p>
<pre tabindex="0"><code># Provisioning configuration for Ansible.
config.vm.provision :ansible do |ansible|
  ansible.playbook = &#34;playbook.yml&#34;
  ansible.extra_vars = { ansible_python_interpreter:&#34;/usr/bin/python3&#34; }
end
</code></pre><p>Adding these changes to your Vagrantfile allows Ansible to successfully run on the Fedora Vagrant guest. Python is successfully located.</p>
<p>This is an annoying workaround, but it solves the issue and lets you successfully test and iterate changes on Fedora systems. Here&rsquo;s hoping the Fedora cloud image maintainers add a default binary for <code>/usr/bin/python</code> to point to <code>/usr/bin/python3</code> in the future.</p>]]></description></item><item><title>Setting up Vagrant for testing Ansible</title><link>https://jwheel.org/blog/2016/06/setting-vagrant-testing-ansible/</link><pubDate>Fri, 10 Jun 2016 00:00:00 +0000</pubDate><guid>https://jwheel.org/blog/2016/06/setting-vagrant-testing-ansible/</guid><description><![CDATA[<p>As part of my <a href="https://summerofcode.withgoogle.com/">Google Summer of Code</a> project proposal for the <a href="https://fedoraproject.org/wiki/Overview">Fedora Project</a>, I&rsquo;ve spent a lot of time learning about the ins and outs of Ansible. <a href="https://www.ansible.com/">Ansible</a> is a handy task and configuration automation utility. In the Fedora Project, Ansible is <a href="https://infrastructure.fedoraproject.org/cgit/ansible.git/">used extensively</a> in Fedora&rsquo;s infrastructure. But if you&rsquo;re first starting to learn Ansible, it might be tricky to test and play with it if you don&rsquo;t have production or development servers you can use. This is where Vagrant comes in.</p>

<h2 id="what-is-vagrant">What is Vagrant?&nbsp;<a class="hanchor" href="#what-is-vagrant" aria-label="Anchor link for: What is Vagrant?">🔗</a></h2>
<p>
<figure>
  <img src="/blog/2016/06/Vagrant.png" alt="Together, Vagrant and Ansible are a powerful combination." loading="lazy">
  <figcaption>Together, Vagrant and Ansible are a powerful combination.</figcaption>
</figure>
</p>
<p>Many people in the tech industry are already familiar with virtual machines (VMs) and using them for testing. If using a virtual machine is useful for testing and experimentation, Vagrant takes that idea and makes it a thousand times for powerful. <a href="https://www.vagrantup.com/">Vagrant</a> creates and configures a single virtual machine or several groups of inter-connected virtual machines. For someone trying to learn configuration management software like Ansible (or Puppet, or Chef, or Salt…), it features tight integration for creating virtual machines from playbooks.</p>
<p>Using Vagrant allows you to make quick, simple, and easy changes in a safe, local environment. Vagrant is also incredibly easy to set up, and in my experiences, it also ran well on a laptop. My trusty Toshiba Satellite with 8GB of RAM and an Intel i3 chip was able to handle three CentOS 7 virtual machines at once, and still manage to do other regular tasks.</p>

<h2 id="installing-vagrant-for-fedora">Installing Vagrant for Fedora&nbsp;<a class="hanchor" href="#installing-vagrant-for-fedora" aria-label="Anchor link for: Installing Vagrant for Fedora">🔗</a></h2>
<p>Since I&rsquo;m working with Fedora on my hardware while working on the Fedora Project over the summer, it would make sense for this guide to cover how to install and set up Vagrant inside of Fedora. However, I imagine it&rsquo;s similar for most other distributions, so try adapting these commands for your own distribution.</p>
<p>The Fedora repositories have a Vagrant package available. To install it, run the following command.</p>
<pre tabindex="0"><code>$ sudo dnf install vagrant
</code></pre><p>This will pull down Vagrant and all the dependencies it needs to run. However, what it won&rsquo;t do is pull down some of the many providers that it might need to use a virtual machine.</p>

<h3 id="vagrant-and-providers">Vagrant and providers&nbsp;<a class="hanchor" href="#vagrant-and-providers" aria-label="Anchor link for: Vagrant and providers">🔗</a></h3>
<p>For my testing, I used a <a href="https://atlas.hashicorp.com/geerlingguy/boxes/centos7">centos7 box image</a> from geerlingguy. This image creates a current, updated CentOS 7 virtual machine. In order to use it, you must have one of the two providers available: VMware or VirtualBox. Seeing as how VirtualBox is easier for me to install and use on my system, I chose to use VirtualBox as the &ldquo;provider&rdquo; for building and simulating the CentOS 7 box within Vagrant.</p>
<p>It took a bit of figuring out at first, but I found a current and well-documented <a href="http://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-rhel/">guide</a> on how to install VirtualBox on to a Fedora 22 or 23 system. For a more detailed explanation of how to do it, you can read the <a href="http://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-rhel/">instructions</a>, but for simplicity, I have the commands here to show how to add it to your Fedora system quickly.</p>
<pre tabindex="0"><code>$ sudo dnf upgrade
$ cd /etc/yum.repos.d/
$ sudo wget http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo
$ sudo dnf install VirtualBox-5.0 binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms
$ sudo /usr/lib/virtualbox/vboxdrv.sh setup
$ sudo usermod -a -G vboxusers your_username
</code></pre><p>From here,  VirtualBox  will be available as a provider within Vagrant.</p>

<h2 id="running-a-centos-7-image">Running a CentOS 7 image&nbsp;<a class="hanchor" href="#running-a-centos-7-image" aria-label="Anchor link for: Running a CentOS 7 image">🔗</a></h2>
<p>Now that you have both Vagrant and VirtualBox installed, you can create a Vagrant virtual machine with this image. Navigate to a new directory you want to use for managing your virtual machines. Once there, you can use the following commands to start your CentOS 7 virtual machine.</p>
<pre tabindex="0"><code>$ vagrant box add geerlingguy/centos7
$ vagrant init geerlingguy/centos7
$ vagrant up --provider virtualbox
</code></pre><p>After a lot of downloading and then waiting for the first setup to finish, you should receive a notification that your virtual machine started! Huzzah! You can log in directly to it by typing <code>vagrant ssh</code> in the same directory you ran the above commands.</p>
<p>There&rsquo;s a lot of cool things you can do to set up your virtual machines and configure how they start. For example, you can choose to use the VirtualBox GUI for running your virtual machines if you don&rsquo;t want to SSH into it. You can tweak several different flags to alter the environment for the virtual machine. However, that is out of the scope of this guide, and there is a fair amount of documentation already online.</p>

<h2 id="provisioning-with-ansible">Provisioning with Ansible&nbsp;<a class="hanchor" href="#provisioning-with-ansible" aria-label="Anchor link for: Provisioning with Ansible">🔗</a></h2>
<p>The fun part (and what was really cool for me) was provisioning new virtual machines with Ansible. You can instruct Vagrant to seek an Ansible playbook when creating a new virtual machine. It will use the instructions of the playbook to configure, install, or tweak whatever is in the playbook, as if it&rsquo;s being run for the first time. Or maybe it&rsquo;s the second, the third, the fourth time you&rsquo;ve run it. In either case, the idempotent nature of Ansible should help make sure you avoid repeating anything that doesn&rsquo;t need repeating.</p>
<p>In order to tell Vagrant to search for an Ansible playbook, you will need to edit the <code>Vagrantfile</code> for wherever you initialized Vagrant. Open it up in your favorite text editor and add the following bits at the bottom, but before the final <code>end</code> statement.</p>
<pre tabindex="0"><code>config.vm.provision &#34;ansible&#34; do |ansible|
  ansible.playbook = &#34;playbook.yml&#34;
end
</code></pre><p>These short but sweet instructions tell Vagrant to look for a <code>playbook.yml</code> file when starting this virtual machine. It will then be easy to provision (i.e. configure / modify / change) the machine with your playbook later on.</p>

<h3 id="writing-the-playbook">Writing the playbook&nbsp;<a class="hanchor" href="#writing-the-playbook" aria-label="Anchor link for: Writing the playbook">🔗</a></h3>
<p>For this blog post, I&rsquo;ll offer a simple but clear example of a playbook you can use to start a Vagrant machine. This snippet specifically comes from <em><a href="http://www.ansiblefordevops.com/">Ansible for DevOps</a></em> by <a href="http://jeffgeerling.com/">Jeff Geerling</a>, which I have (and am) using as a guide as I continue to learn more about Ansible (I highly recommend the book, consider getting a copy).</p>
<pre tabindex="0"><code>---
- hosts: all
  sudo: yes
  tasks:
  - name: Ensure NTP (for time synchronization) is installed.
    yum: name=ntp state=present
  - name: Ensure NTP is running.
    service: name=ntpd state=started enabled=yes
</code></pre><p>All this example playbook does installs NTP if it is not present on the system, and then start and enable it, if it is not already. This is a very simple example, but it&rsquo;s good for getting started quickly.</p>

<h3 id="running-the-playbook">Running the playbook&nbsp;<a class="hanchor" href="#running-the-playbook" aria-label="Anchor link for: Running the playbook">🔗</a></h3>
<p>In the same directory as your <code>Vagrantfile</code>, create a <code>playbook.yml</code> with the above content. Once you have the YAML file there, running the following command will run the Ansible playbook and allow you to see how it runs.</p>
<pre tabindex="0"><code>$ vagrant provision
</code></pre><p>Now, Vagrant will take your playbook and instantly run it in your machine. If all goes right, your virtual machine will now have NTP installed and be syncing your clock to the Internet! While a simple task, it was a satisfying feeling for me to see this run, but also to imagine the other possibilities that this could be used for. It would be easy to run a playbook on one, two, ten, a hundred, a thousand servers, and have it do the same thing on all of them.</p>
<p>The automation was fascinating to me and began giving me ideas of how I could automate my infrastructure, as well as to creating one for WordPress (for my GSoC project).</p>
<p>Congratulations! By the end of this short but (hopefully) useful guide, you will have Vagrant virtual machines that are controlled and orchestrated by Ansible.</p>]]></description></item></channel></rss>