Getting started with the Skytap dynamic inventory for Ansible

The Skytap dynamic inventory for Ansible allows you to manage and configure Skytap VMs using Ansible commands and playbooks. With simple commands and playbooks, you can perform bulk actions across VMs.

For example, you can combine Ansible and Skytap to:

  • Set up user accounts on VMs.
  • Install software and software updates on VMs.
  • Collect log files from VMs.

This tutorial walks you through the steps you’ll need to begin communicating with Skytap VMs using the dynamic inventory.

The Skytap dynamic inventory for Ansible is an open-source project built using the Skytap REST API.

Contents

Prerequisites

Before you begin
  • Prepare the machine that will run Ansible (either a local computer or Skytap VM).
    1. Install the following:
    2. Verify that you have a way to authenticate to VMs over SSH using a command line.

      To follow this tutorial on Linux, you can install SSHPass for non-interactive user/password authentication. On Windows, you can install PUTTY or Cygwin combined with SSHPass. This tutorial uses non-interactive user/password authentication; it’s simpler to demonstrate, but less secure than public key authentication.

  • Prepare SSH on the Skytap VMs you want to manage:
    1. Install and run SSH on the VM. Instructions vary, depending on the VM operating system.
    2. Create an administrative username and password on the VM that Ansible can use; create the same username and password on all of the VMs you want to manage in the environment.
      • For this tutorial, save these usernames and passwords on the VM Credentials page.

        If you want only to try out Ansible, you can create an environment using the Ubuntu Server 14.04 public template. The VM has SSH enabled and saved credentials.

  • Create a network connection between the Ansible machine and the environment you want to manage.

    If you installed Ansible on a VM in Skytap:

    • Place the Ansible VM in the same environment as the VMs you want to manage.

      or

    • Place the Ansible VM in a separate environment and connect the networks between those environments (see Networking Between Environments).

    If you installed Ansible on a local machine:

Installing and configuring the Skytap dynamic inventory for Ansible

From the machine where you installed Ansible:

  1. Download the Skytap dynamic inventory for Ansible from GitHub. You can either fork or clone the repository. For example, using the command line:
     git clone https://github.com/skytap/skytap-ansible-inventory.git
    
  2. Edit SSH settings for Ansible.
    • Create a new file called ansible.cfg. Add the following text to the file and save it to the root of Ansible user directory.
        [defaults]
        host_key_checking=False
        timeout = 10
        [ssh_connection]
        ssh_args = -o ControlMaster=auto -o ControlPersist=60s
        control_path = /tmp/ansible-%r@%h:%p
      

      or

    • Edit the ~/.ssh/config file to include the following text:

        Host *
        ControlMaster auto
        ControlPath /tmp/ansible-%r@%h:%p
        StrictHostKeyChecking no
      
  3. Copy EXAMPLE_skytap.ini (located with the Skytap dynamic inventory for Ansible project files) to create a new file called skytap.ini.
  4. Open skytap.ini and replace the placeholder information with your username, API token, and information about the Skytap environment you want to work with.

    If you’re using a GitHub fork and you plan to push changes to the public project, make sure you add skytap.ini to .gitignore. Otherwise, you may accidentally share your Skytap credentials.

    For example, the file may contain:

     [skytap_vars]
     base_url:https://cloud.skytap.com/v2
     username:jdoe
     api_token:123abcde456fghijklm123
    
     [skytap_env_vars]
     configuration_id:1234567
     network_type:nat_vpn
     use_api_credentials:true
     skytap_vm_username:skytap
     api_credential_delimiter:/
    
     [ansible_ssh_vars]
     port:22
    

Using the Skytap dynamic inventory for Ansible

Once the inventory is installed and configured, you can begin using it with Ansible commands to communicate with Skytap VMs. For example:

To check connectivity to all of the VMs in an environment
  1. Navigate to the directory where the inventory project is saved:

     cd ~/skytap-ansible-inventory
    
  2. Run the following command:

     ansible -i skytap_ansible.py all -m ping
    

You should receive a response similar to:

vmname | success >> {
    "changed": false,
    "ping": "pong"
}

You can also pair the inventory with Ansible playbooks. For example:

To create a simple playbook that checks connectivity to all of the hosts in the environment
  1. Create a file called ping.yaml, containing the following text:

     ---
     - hosts: all
       tasks:
       - name: "Ping all of the hosts in the inventory."
         ping:
    
  2. Then run:

     ansible-playbook -i skytap_inventory.py ping.yaml
    

For more information about creating Ansible playbooks, see the Ansible Intro to Playbooks.

Working with multiple Skytap environments

The Skytap dynamic inventory for Ansible works with the Skytap environment specified in the skytap.ini file. To work with multiple Skytap environments, create multiple .ini files (with different names), using the skytap.ini file as a template.

To switch between .ini files, create an environment variable called SKYTAP_INI and set it the name of the .ini file you want to work with.

For example:

export SKYTAP_INI=otherfilename.ini

To use skytap.ini again, edit the SKYTAP_INI variable:

export SKYTAP_INI=skytap.ini