How to setup an Ansible control server using Vagrant and VMWare

2 minute read

This article is a step by step guide on how to install / setup an Ansible control server and at least one server that will be managed by the Ansible control server. This guide is based upon using Vagrant and VMWare on a Mac OS to create Virtual machines for the Ansible control server and the application server that will be managed.

Instructions

  1. Initialize a project working directory for Vagrant
    • vagrant init
  2. Update the VagrantFile to define an Ansible Control Server and one application server
    • # -*- mode: ruby -*-
      # vi: set ft=ruby :
              
      # All Vagrant configuration is done below. The "2" in Vagrant.configure
      # configures the configuration version (we support older styles for
      # backwards compatibility). Please don't change it unless you know what
      # you're doing.
      Vagrant.configure("2") do |config|
        # The most common configuration options are documented and commented below.
        # For a complete reference, please see the online documentation at
        # https://docs.vagrantup.com.
              
        #-----------------------------------------------------------------------------
        # Ansible Control Server
        #-----------------------------------------------------------------------------
        # Create a private network, which allows host-only access to the machine
        # using a specific IP.
              
        config.vm.define "acs" do |acs|
          acs.vm.box = "centos/7"
          acs.vm.hostname = "acs"
          acs.vm.network "private_network", ip: "192.168.33.50"
        end
              
              
        #-----------------------------------------------------------------------------
        # Application Server - TEST
        #-----------------------------------------------------------------------------
        # Create a forwarded port mapping which allows access to a specific port
        # within the machine from a port on the host machine. In the example below,
        # accessing "localhost:8080" will access port 80 on the guest machine.
        # NOTE: This will enable public access to the opened port
                
        # Create a private network, which allows host-only access to the machine
        # using a specific IP.
                
        config.vm.define "appsvrtest" do |appsvrtest|
          appsvrtest.vm.box = "centos/7"
          appsvrtest.vm.hostname = "appsvrtest"
          appsvrtest.vm.network "private_network", ip: "192.168.33.60"
          appsvrtest.vm.network "forwarded_port", guest: 80, host: 8080
        end 
              
      end 
             
      
  3. Start the VMs in VMWare using Vagrant and the VMWare provider for Vagrant
    • vagrant up --provider vmware_desktop
  4. Verify the VMs are running in VMWare
    • vmrun list
  5. SSH into the Ansible Control server with hostname “acs” using the userid vagrant
    • ....
  6. Install the Extra Packages for Enterprise Linux (EPEL) using yum
    • sudo yum install epel-release
  7. Install Ansible
    • sudo yum install ansible
  8. Verify Ansible has been installed. Run the ansible command which will list all command options. Run the ansible –version command which will list out ansible details
    • [vagrant@acs ~]$ ansible
      Usage: ansible <host-pattern> [options]
             
      Define and run a single task 'playbook' against a set of hosts
             
      Options:
        -a MODULE_ARGS, --args=MODULE_ARGS
                              module arguments
        --ask-vault-pass      ask for vault password
      .
      .
      .
      .
      
      
    •  [vagrant@acs ~]$ ansible --version
       ansible 2.7.7
         config file = /etc/ansible/ansible.cfg
         configured module search path = [u'/home/vagrant/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
         ansible python module location = /usr/lib/python2.7/site-packages/ansible
         executable location = /usr/bin/ansible
         python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
      
  9. Setup ssh keys on Ansible control server and the app server(s)
    • [vagrant@acs ~]$ ssh-keygen
  10. Copy your ssh public key to a server, allowing you to authenticate without a password. Using ssh-copy-id utility
    •   [vagrant@acs ~]$ ssh-copy-id vagrant@192.168.33.60
        /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
        /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
        /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
        vagrant@192.168.33.60's password:
               
        Number of key(s) added: 1
               
        Now try logging into the machine, with:   "ssh 'vagrant@192.168.33.60'"
        and check to make sure that only the key(s) you wanted were added.