By clicking or navigating this website site, you agree to allow our collection of information on Scaleway to offer you an optimal user experience and to keep track of statistics through cookies. Learn more about our Cookie Policy.

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.

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 ps (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.

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:

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 accounthosts:localhostgather_facts:noenvironment:SCW_API_KEY:""tasks:-scaleway_sshkey:ssh_pub_key:"ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAACAQDKGCJCDlDVQx+CFFPKe0c/v4AAREiB2yldCsr5uH64Jnco4Z7OzJHXJPglnsXOSL1uMS3TRrHav12ljzIlN0iV5xdLP3S536imhOKJSnd32JAPtKyPulmENY5fbQocZ7oEAsvBmKnVlCnfoAm49gN4G6cnh0NzupEncUuEpzr40eHTLuj5OSjyOJU3jg/bZePvwz4wZhv1Wp3BN+8jZlHbe5Ew6QpplYCybCMFUAeWgiYav3MaPr55oLKmnZI+JU8mVpstSBnrxKO8mrHmiXEr3jg6dUkIrzzvR5au2HlBCCusDjCvx7KHKCzZKy6vaFicOO6C9ANOS920xSMAmfYkUoH/N41yyyZa8C9dc7GFxKgx9Dg44Of7u6ef3E8XHNY7hCu/MncROHtJsL1E+ZtQPsdU362VMyGSIo2drRa/U1vR3UEQdie9NchtYyBVEg50uAi+cof2e2PnT47+KCoH5ITF6bxPEGR7RcIpudmS3as1U5qGEhDshX7AXsMwaxLmezgFxFgGIkvKeLbtO/j2dgdOgERsg0Bl1yYMePrGM8sSFAxC2zGWz8fTAMUWjzkyuxEeNuajUo9PrSDG5L4MiBO45/qWVV/5iJZHhJcPhzXr9D7gstufr75XAteVLKmpPOL31BRu1zn+AOJio6Nkxtgdtd27E5L3zTzNj2yldQ==my_user@example.com"state:presentregister: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 instancescaleway_compute:name:foobarstate:presentimage:ec8b431e-ad39-4523-8b94-f3fa7f3cbd06organization:951df375-e094-4d26-97c1-ba548eeb9c42region:ams1commercial_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 (for this example, par1 and 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:scalewayregions:-ams1-par1tags:-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):

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: