How to use Scaleway Ansible modules

Ansible Overview

Ansible is an IT automation tool. It simplifies cloud computing, configuration management, program setup, intra-service orchestration, and several other IT needs.

Ansible uses a very simple language (YAML, in the form of Ansible Playbooks) that allows you to spell out your automation jobs in a simplified way.

While there are many popular configuration management systems available for Linux systems, such as Chef, Ansible is one of the simplest configuration management systems to get started with.

Ansible works by configuring client machines from a computer that has the Ansible components installed and configured. It communicates over normal SSH channels to retrieve information from remote machines. This means that any computer that you can administer through SSH, can also be administered through Ansible.

In this tutorial we will see how to create Scaleway resources using Ansible Scaleway modules.

Requirements:

Installing Ansible on Ubuntu Bionic Beaver

1 . Connect to your server using SSH:

ssh root@SERVER_IP

If you do not know your server IP, you can list your existing servers using scw instance server list (Scaleway CLI). For more information on the Scaleway CLI, refer to the tutorial on the Scaleway Command Line Interface.

The server IP can also be retrieved from the Scaleway Console. Once logged in, check the IP Adresses in the Servers tab of the left menu.

Note: If you use the root user, you can remove the sudo before each command.

2 . Update the Ubuntu packet manager apt:

apt update

3 . Upgrade the Ubuntu packages already installed:

apt upgrade

Installing Ansible from a PPA repository

1 . Update your package index and install the software-properties-common package. This software will make it easier to manage this and other independent software repositories. Add the Ansible PPA (Personal Package Archive) and refresh your system’s package index once again.

apt install software-properties-common
apt-add-repository ppa:ansible/ansible
apt update

2 . Install the Ansible software

apt install ansible

3 . Check that the installation is successful

ansible --version

which returns something similar to:

ansible 2.9.14
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Jul 20 2020, 15:37:01) [GCC 7.5.0]

Alternative Installation of Ansible

To learn more about different methods for installing Ansible, refer to the official Ansible Documentation.

For more information on Ansible commands or playbook, refer to the official Ansible documentation.

Configuring an SSH Key on Scaleway

The connection to Scaleway Elements Instances are made using Secure Shell. SSH keys are stored at the account level, which means that you can re-use the same SSH key in multiple nodes. The first step to configure Scaleway Elements Instance resources is to have at least one SSH key configured.

scaleway_sshkey is an Ansible module that manages SSH keys on your Scaleway account. You can add an SSH key to your account by including the following task in a playbook:

- name: "Add SSH key"
  scaleway_sshkey:
     ssh_pub_key: "ssh-rsa AAAA..."
     state: "present"

The ssh_pub_key parameter contains your ssh public key as a string. Here is an example inside a playbook:

# SCW_API_KEY='XXX' ansible-playbook ./test/legacy/scaleway_ssh_playbook.yml

- name: Test SSH key lifecycle on a Scaleway account
  hosts: localhost
  gather_facts: no
  environment:
    SCW_API_KEY: ""

  tasks:

  - scaleway_sshkey:
      ssh_pub_key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDKGCJCDlDVQx+CFFPKe0c/v4AAREiB2yldCsr5uH64Jnco4Z7OzJHXJPglnsXOSL1uMS3TRrHav12ljzIlN0iV5xdLP3S536imhOKJSnd32JAPtKyPulmENY5fbQocZ7oEAsvBmKnVlCnfoAm49gN4G6cnh0NzupEncUuEpzr40eHTLuj5OSjyOJU3jg/bZePvwz4wZhv1Wp3BN+8jZlHbe5Ew6QpplYCybCMFUAeWgiYav3MaPr55oLKmnZI+JU8mVpstSBnrxKO8mrHmiXEr3jg6dUkIrzzvR5au2HlBCCusDjCvx7KHKCzZKy6vaFicOO6C9ANOS920xSMAmfYkUoH/N41yyyZa8C9dc7GFxKgx9Dg44Of7u6ef3E8XHNY7hCu/MncROHtJsL1E+ZtQPsdU362VMyGSIo2drRa/U1vR3UEQdie9NchtYyBVEg50uAi+cof2e2PnT47+KCoH5ITF6bxPEGR7RcIpudmS3as1U5qGEhDshX7AXsMwaxLmezgFxFgGIkvKeLbtO/j2dgdOgERsg0Bl1yYMePrGM8sSFAxC2zGWz8fTAMUWjzkyuxEeNuajUo9PrSDG5L4MiBO45/qWVV/5iJZHhJcPhzXr9D7gstufr75XAteVLKmpPOL31BRu1zn+AOJio6Nkxtgdtd27E5L3zTzNj2yldQ== my_user@example.com"
      state: present
    register: result

  - assert:
      that:
        - result is success and result is changed

Creating a Virtual Instance

Now that we have an SSH key configured, the next step is to spin up an Instance! scaleway_compute is an Ansible module that can create, update and delete Scaleway Elements Instances:

 - name: Create an instance
   scaleway_compute:
     name: foobar
     state: present
     image: ec8b431e-ad39-4523-8b94-f3fa7f3cbd06
     organization: 951df375-e094-4d26-97c1-ba548eeb9c42
     region: ams1
     commercial_type: gp1-xs

Here are the parameter details for the example shown above:

  • foobar is the name of the instance (the one that will show up in your web console).
  • image is the UUID of the system image you would like to use. A list of all images is available for each availability zone.
  • organization represents the organization that your account is attached to.
  • region represents the Availability Zone which your instance is in. It can be either: par1, par2, waw1 or ams1).
  • commercial_type represents the name of the commercial offers. You can check out the Scaleway pricing page to find which instance is right for you.

Ansible Dynamic Inventory Plugin

Ansible 2.6 ships with Scaleway Dynamic Inventory Plugin. You can now get a complete inventory of your Scaleway resources through this plugin and filter it on different parameters (regions and tags are currently supported).

Let’s create an example! Suppose that we want to get all hosts that got the tag web_server. First, you need to activate the plugin through an inventory file:

plugin: scaleway
  regions:
    - ams1
    - par1
  tags:
    - web_server

This inventory means that we want all hosts that got the tag web_server on the zone ams1 and par1.

Once you have configured this file, you can get the information using the following command (with scaleway_inventory.yml as the inventory file name):

ansible-inventory --list -i scaleway_inventory.yml
{
    "_meta": {
        "hostvars": {
            "dd8e3ae9-0c7c-459e-bc7b-aba8bfa1bb8d": {
                "ansible_verbosity": 6,
                "arch": "x86_64",
                "commercial_type": "DEV1-M",
                "hostname": "foobar",
                "ipv4": "51.15.200.26",
                "organization": "951df375-e094-4d26-97c1-ba548eeb9c42",
                "state": "running",
                "tags": [
                    "web_server"
                ]
            }
        }
    },
    "all": {
        "children": [
            "ams1",
            "par1",
            "ungrouped",
            "web_server"
        ]
    },
    "ams1": {},
    "par1": {
        "hosts": [
            "dd8e3ae9-0c7c-459e-bc7b-aba8bfa1bb8d"
        ]
    },
    "ungrouped": {},
    "web_server": {
        "hosts": [
            "dd8e3ae9-0c7c-459e-bc7b-aba8bfa1bb8d"
        ]
    }
}

As you can see, we get different groups of hosts. par1 and ams1 are groups based on location. web_server is a group based on a tag.

In case a filter parameter is not defined, the plugin supposes all values possible are wanted. This means that for each tag that exists on your Scaleway Elements Instances, a group based on each tag will be created.

Going Further

This is just a quick example but you can do many more tasks on Scaleway using ansible. Checkout the following tutorials to learn more:

Discover the Cloud That Makes Sense