How to deploy Fuga Cloud infrastructure with Ansible

Introduction

The possibilities with Ansible are endless. In this tutorial, we will teach you how to deploy your Fuga Cloud infrastructure automatically with Ansible.

Note: This tutorial is tested with Ansible 2.7.9 and Ubuntu 18.04 LTS. If your running the latest ansible version and experience any problems with this tutorial, please contact us.

Prerequisites

Startup

  1. Install ansible and shade with pip3:

    sudo apt update
    sudo apt install python3-pip
    sudo pip3 install ansible
    sudo pip3 install shade
  2. Create a new folder called ansible somewhere on your system, this will contain all our files for this tutorial.

    mkdir ansible
  3. Copy or cut your OpenRC file into the new ansible folder.

  4. Open the ansible folder with the following command:

    cd ansible
  5. To give your Ansible playbook file access to your Fuga Account, enter the following command:

    source ./YOUR_OPENRC_FILE.SH
  6. When asked for your OpenStack password, enter your Fuga Cloud password

Deploying infrastructure in Fuga Cloud

  1. Start by creating a new Ansible playbook file in the ansible folder:

    touch provision_infra.yml
  2. Edit the provision_infra.yml (nano provision_infra.yml) and paste in the following code:

- name: Deploy OpenStack
hosts: localhost
gather_facts: false
tasks:
- name: Create network
os_network:
state: present
name: my_network
external: False
shared: False
register: my_network

- name: Create subnet
os_subnet:
state: present
network_name: "{{ my_network.id }}"
name: my_subnet
ip_version: 4
cidr: 192.168.0.0/24
enable_dhcp: yes
dns_nameservers:
- 8.8.8.8
register: my_subnet

- name: Create router
ignore_errors: yes
os_router:
state: present
name: my_router
network: public
interfaces:
- my_subnet

- name: Create new example Security group for HTTP
os_security_group:
state: present
name: example_group
description: security group for web servers

- name: Add example HTTP rule to security group
os_security_group_rule:
security_group: example_group
port_range_min: 80
port_range_max: 80
protocol: tcp
remote_ip_prefix: 0.0.0.0/0

- name: Create volume for instance
os_volume:
state: present
size: 100
display_name: my_volume

- name: Deploy an instance
os_server:
name: my_instance
state: present
image: Ubuntu 18.04 LTS
key_name: YOUR_SSH_KEY_NAME
wait: yes
flavor: c2.small
auto_ip: yes
network: my_network
security_groups:
- default
- example_group
register: my_instance

- name: Attach volume to instance
os_server_volume:
state: present
server: my_instance
volume: my_volume
device: /dev/vdb

- name: Add VM to inventory
add_host:
name: new_vm
groups: fuga_vms
ansible_host: "{{ my_instance.server.public_v4 }}"

- name: Adding host to fuga_hosts file
template:
src: hosts_template.j2
dest: "fuga_hosts" 
  1. By default a volume of 100 GB is created, you can change this to any size within your Fuga quota limit. Search for the task Create volume for instance and update the size field.

  2. In the task Deploy an instance we have to update our SSH YOUR_SSH_KEY_NAME. This should be the name of your SSH key on the Fuga dashboard.

  3. It’s also possible to change the flavor of your instance. The default is a c2.small but this can be changed to anything within your limits. Search for the task Deploy an instance and update the flavor field.

  4. All the naming begins with my_. You can change them to your own preferences.

  5. Save the provision_infra.yml file.

  6. Create a new file that will be the template for our Ansible inventory. Execute the following command inside the ansible directory:

    touch hosts_template.j2
  7. Copy paste the following code inside hosts_template.j2 (nano hosts_template.j2) using your favorite text editor:

    [fuga_vms:vars]
    ansible_python_interpreter=/usr/bin/env python3

    [fuga_vms]
    {{ my_instance.server.public_v4 }} 
  8. Save the file and exit your editor.

  9. You’re now ready to roll out your new infrastructure. To run this playbook file execute the following command:

    ansible-playbook provision_infra.yml
  10. When the Playbook is done deploying, a host has been added to your fuga_hosts file.

Conclusion

You now know how to deploy a basic infrastructure on Fuga Cloud using Ansible. The playbook used in this tutorial can be expanded to your liking.