Thursday 1 October 2015

Getting Started With Ansible and Rackspace P2

Introduction
In this post a simple playbook is demonstrated to provision a pseudo DC in Rackspace.

Following on from the previous post, a parametrised playbook is created to:
  •  Install a keypair from a file
  •  create a management network
  •  create a domain name
This playbook will likely only be run once

Create Datacenter Playbook (rs_create_dc.yml)
---
# rs_create_dc.yml

- name: Create Rackspace DC
  hosts: localhost
  connection: local
  gather_facts: false

  vars:
    - region: LON
    - management_net_cidr: 10.45.45.0/24
    - domain_contact: dns@{{ domain_name }}

  tasks:
    - name: Create KeyPair
      rax_keypair:
        name: rs_kp
        region: "{{ region }}"
        public_key: "{{ ssh_key }}"

    - name: Create Management Network
      rax_network:
        label: management
        cidr: "{{ management_net_cidr }}"
        region: "{{ region }}"

    - name: Add Domain Name
      rax_dns:
        name: "{{ domain_name }}"
        email: "{{ domain_contact }}"
        region: "{{ region }}"


The playbook might then be run like this:
ansible-playbook rs_create_dc.yml -e "ssh_key=/home/user/.ssh/publickey.pub domain_name=example.com"

Getting Started With Ansible and Rackspace P1

Introduction
The easiest way to play with an ansible module is from the command line.  This post demonstrates some of the Rackspace modules.

Setup
I started by creating my credentials file and setting the environmental RAX_CREDS_FILE to point to it.  As I was running pyrax from a Python virtual environment I also setup up the rax.py inventory and inventory file to point to a virtual environment.

Add a host key
ansible localhost -m rax_keypair -a "name=rs_kp public_key=/path/to/public_key region=LON" -c local -i inventory/ 
Worth noting that the path to the key will not expand ~ :(

Create a network
ansible localhost -m rax_network -a "region=LON label=alabel cidr=10.1.2.0/24" -c local -i inventory/

Create a VM
ansible localhost -m rax -a "flavor=general1-1 image="3cdcd2cc-238c-4f42-a9f4-0a80de217f7a" group=management name=vpn key_name=rs_kp networks=management,public wait=yes region=LON" 

In this example 2 vm's are created with incrementing numbers i.e vpn01, vpn02

ansible localhost -m rax -a "flavor=general1-1 image="3cdcd2cc-238c-4f42-a9f4-0a80de217f7a" group=management name=vpn%02d.example.com key_name=rs_kp networks=management,public wait=yes region=LON count=2"

Add DNS Zone
ansible localhost -m rax_dns -a "name=example.com email=hostmaster@example.com region=LON" (email address is required)

Add DNS Record to Zone
ansible localhost -m rax_dns_record -a "domain=example.com name=test1.example.com data=1.1.1.1 type=A region=LON" 

Get facts about a host
ansible localhost -m rax_facts -a name="server01" region=LON"