Follow

Use Ansible to Manage Servers on CloudShare

 

Ansible Integration

One of many uses for Ansible is server management, and one of the great things we've done at CloudShare is to integrate with Ansible so that you can use Ansible to manage your CloudShare VMs in your CloudShare environments. 

The CloudShare Ansible dynamic inventory script uses CloudShare’s REST API and Python SDK to dynamically fetch a list of all your CloudShare environments and VM login credentials.

Tip

If you’d like us to integrate more deeply with Ansible, feel free to open a feature request issue and let us know.

Setup

  1. Download cloudshare-inv.py. This is the CloudShare dynamic inventory script.

  2. Install the CloudShare Python SDK: pip install cloudshare.

  3. Define your CloudShare API keys as environment variables: CLOUDSHARE_API_ID, CLOUDSHARE_API_KEY. You can grab them here.

Usage

To retrieve all VMs in all CloudShare environments and their login credentials:

ansible -i cloudshare-inv.py all -m ping

To limit the output to a specific environment:

ansible -i cloudshare-inv.py environment name -m ping

Example - Running the Inventory Script

Run the inventory script on its own to generate a list of environments, VMs and VM login credentials:

ansible -i cloudshare-inv.py all -m ping

The output is a JSON document that looks like this:

{ 
 "docker-templates": {
 "hosts": [ 
 "ubuntu-14-04-server-64-bit-small", 
 "ubuntu-14-04-server-64-bit-medium", 
 "ubuntu-14-04-server-64-bit-large", 
 "ubuntu-16-04-lts-server-small", 
 "ubuntu-16-04-lts-server-medium", 
 "ubuntu-16-04-lts-server-large" 
 ] 
}, 
"_meta": { 
 "hostvars": { 
 "login credentials": "will be populated here..." 
 } 
 } 
} 

Here we have one environment called docker-templates with six VMs.

Example - Running a Playbook

Let’s say you have a playbook called web-app.yml that you wish to run on one your VMs. Using the dynamic inventory, you can do this:

ansible-playbook -i cloudshare-inventory.py all web-app.yml

This will run the playbook on all your VMs. You’d probably want to limit it to a subset of your VMs, and you can use --limit for that and pass it a specific environment name, a pattern of VM names, etc.

Example - Running Ad-Hoc Commands

Something you probably find yourself doing from time to time is to apply patches to your servers.

In this example we have an environment called chat-demo containing a few Ubuntu Linux VMs, and we want to run apt-get upgrade on all of them. This is a one-liner in Ansible:

ansible -i cloudshare-inv.py chat-demo -m apt -a "upgrade=dist"
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk