Manage Scaleway Resources with Ansible Native Support

July 11, 2018 by Rémy Léone

Ansible and Scaleway

Hello! My name is Rémy Léone and I am a Cloud Developer Evangelist at Scaleway. I’ve written this post to let you know that support for Scaleway services are now available natively in the Ansible Project with the release of Ansible version 2.6.

Scaleway’s goal is to help developers get cloud resources as easily as possible. Developing support for our services using well-known tools such as Ansible is one of our top priorities for user engagement. In this article, I’ll discuss the different Scaleway modules and demonstrate how to natively manage your Scaleway resources in your Ansible Playbooks.

---

How to Configure an SSH Key on Scaleway

Connection to Scaleway Compute nodes use 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 compute resources is to have at least one SSH key configured.

scaleway_sshkeys 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 AAAAB3NzaC1yc2EAAAADAQABAAABAQDf29yyommeGyKSIgSmX0ISVXP+3x6RUY4JDGLoAMFh2efkfDaRVdsvkvnFuUywgP2RewrjTyLE8w0NpCBHVS5Fm1BAn3yvxOUtTMxTbsQcw6HQ8swJ02+1tewJYjHPwc4GrBqiDo3Nmlq354Us0zBOJg/bBzuEnVD5eJ3GO3gKaCSUYTVrYwO0U4eJE0D9OJeUP9J48kl4ULbCub976+mTHdBvlzRw0Tzfl2kxgdDwlks0l2NefY/uiTdz2oMt092bAY3wZHxjto/DXoChxvaf5s2k8Zb+J7CjimUYnzPlH+zA9F6ROjP5AUu6ZWPd0jOIBl1nDWWb2j/qfNLYM43l sieben@sieben-macbook.local"
      state: present
    register: result

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

How to Create a Server on Scaleway

Now that we have an SSH key configured, the next step is to spin up a server! scaleway_compute is an Ansible module that can create, update and delete Scaleway compute instances:

 - name: Create a server
   scaleway_compute:
     name: foobar
     state: present
     image: ec8b431e-ad39-4523-8b94-f3fa7f3cbd06
     organization: 951df375-e094-4d26-97c1-ba548eeb9c42
     region: ams1
     commercial_type: VC1S

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.

Take a look at this short playbook to see a working example using scaleway_compute:

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

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

  tasks:

  - name: Create a server
    scaleway_compute:
      name: foobar
      state: present
      image: ec8b431e-ad39-4523-8b94-f3fa7f3cbd06
      organization: 951df375-e094-4d26-97c1-ba548eeb9c42
      region: ams1
      commercial_type: START1-S
      wait: true

    register: server_creation_task

  - debug: var=server_creation_task

  - assert:
      that:
        - server_creation_task is success
        - server_creation_task is changed

  - name: Run it
    scaleway_compute:
      name: foobar
      state: running
      image: ec8b431e-ad39-4523-8b94-f3fa7f3cbd06
      organization: 951df375-e094-4d26-97c1-ba548eeb9c42
      region: ams1
      commercial_type: START1-S
      wait: true
      tags:
        - test
        - www
    register: server_run_task

  - debug: var=server_run_task

  - assert:
      that:
        - server_run_task is success
        - server_run_task is changed

 

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": "VC1S",
                    "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 compute nodes, a group based on each tag will be created.

Conclusion

For something to try after you’ve experimented with the modules discussed above, take a look at how to create a hot snapshot of your Scaleway instances. This will help ensure that you can spawn new instances without running your whole playbook from scratch.

All of us at Scaleway are very excited to be part of the Ansible 2.6 release. We hope that this article helps you combine Ansible and Scaleway services for your upcoming projects. Many thanks to the Red Hat staff for their helpful comments that helped to get those modules merged, and to the writers that helped make this article happen!

 

Share:

 

Rémy Léone

Rémy Léone is a Cloud Developer Evangelist at Scaleway


rss-icon  RSS Feed

Ansible Tower by Red Hat
Ansible Fest Austin 2018
Learn About Ansible Tower