Debian Codegraph @

ansible_collections/community/general/docs/docsite/rst/guide_scaleway.rst

raw
1 ..
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5
6 .. _ansible_collections.community.general.docsite.guide_scaleway:
7
8 **************
9 Scaleway Guide
10 **************
11
12 Introduction
13 ============
14
15 `Scaleway <https://scaleway.com>`_ is a cloud provider supported by the community.general collection through a set of plugins and modules.
16 Those modules are:
17
18 - :ansplugin:`community.general.scaleway_compute#module`: manages servers on Scaleway. You can use this module to create, restart and delete servers.
19 - :ansplugin:`community.general.scaleway_compute_private_network#module`
20 - :ansplugin:`community.general.scaleway_container#module`
21 - :ansplugin:`community.general.scaleway_container_info#module`
22 - :ansplugin:`community.general.scaleway_container_namespace_info#module`
23 - :ansplugin:`community.general.scaleway_container_namespace#module`
24 - :ansplugin:`community.general.scaleway_container_registry_info#module`
25 - :ansplugin:`community.general.scaleway_container_registry#module`
26 - :ansplugin:`community.general.scaleway_database_backup#module`
27 - :ansplugin:`community.general.scaleway_function#module`
28 - :ansplugin:`community.general.scaleway_function_info#module`
29 - :ansplugin:`community.general.scaleway_function_namespace_info#module`
30 - :ansplugin:`community.general.scaleway_function_namespace#module`
31 - :ansplugin:`community.general.scaleway_image_info#module`
32 - :ansplugin:`community.general.scaleway_ip#module`
33 - :ansplugin:`community.general.scaleway_ip_info#module`
34 - :ansplugin:`community.general.scaleway_lb#module`
35 - :ansplugin:`community.general.scaleway_organization_info#module`
36 - :ansplugin:`community.general.scaleway_private_network#module`
37 - :ansplugin:`community.general.scaleway_security_group#module`
38 - :ansplugin:`community.general.scaleway_security_group_info#module`
39 - :ansplugin:`community.general.scaleway_security_group_rule#module`
40 - :ansplugin:`community.general.scaleway_server_info#module`
41 - :ansplugin:`community.general.scaleway_snapshot_info#module`
42 - :ansplugin:`community.general.scaleway_sshkey#module`: adds a public SSH key from a file or value to the Packet infrastructure. Every subsequently-created device will have this public key installed in .ssh/authorized_keys.
43 - :ansplugin:`community.general.scaleway_user_data#module`
44 - :ansplugin:`community.general.scaleway_volume#module`: manages volumes on Scaleway.
45 - :ansplugin:`community.general.scaleway_volume_info#module`
46
47 The plugins are:
48
49 - :ansplugin:`community.general.scaleway#inventory`: inventory plugin
50
51
52 .. note::
53 This guide assumes you are familiar with Ansible and how it works.
54 If you are not, have a look at :ref:`ansible_documentation` before getting started.
55
56 Requirements
57 ============
58
59 The Scaleway modules and inventory script connect to the Scaleway API using `Scaleway REST API <https://developer.scaleway.com>`_.
60 To use the modules and inventory script you will need a Scaleway API token.
61 You can generate an API token through the `Scaleway console's credential page <https://cloud.scaleway.com/#/credentials>`__.
62 The simplest way to authenticate yourself is to set the Scaleway API token in an environment variable:
63
64 .. code-block:: console
65
66 $ export SCW_TOKEN=00000000-1111-2222-3333-444444444444
67
68 If you are not comfortable exporting your API token, you can pass it as a parameter to the modules using the ``api_token`` argument.
69
70 If you want to use a new SSH key pair in this tutorial, you can generate it to ``./id_rsa`` and ``./id_rsa.pub`` as:
71
72 .. code-block:: console
73
74 $ ssh-keygen -t rsa -f ./id_rsa
75
76 If you want to use an existing key pair, just copy the private and public key over to the playbook directory.
77
78 How to add an SSH key?
79 ======================
80
81 Connection to Scaleway Compute nodes use Secure Shell.
82 SSH keys are stored at the account level, which means that you can reuse the same SSH key in multiple nodes.
83 The first step to configure Scaleway compute resources is to have at least one SSH key configured.
84
85 :ansplugin:`community.general.scaleway_sshkey#module` is a module that manages SSH keys on your Scaleway account.
86 You can add an SSH key to your account by including the following task in a playbook:
87
88 .. code-block:: yaml+jinja
89
90 - name: "Add SSH key"
91 community.general.scaleway_sshkey:
92 ssh_pub_key: "ssh-rsa AAAA..."
93 state: "present"
94
95 The ``ssh_pub_key`` parameter contains your ssh public key as a string. Here is an example inside a playbook:
96
97
98 .. code-block:: yaml+jinja
99
100 - name: Test SSH key lifecycle on a Scaleway account
101 hosts: localhost
102 gather_facts: false
103 environment:
104 SCW_API_KEY: ""
105
106 tasks:
107
108 - community.general.scaleway_sshkey:
109 ssh_pub_key: "ssh-rsa AAAAB...424242 [email protected]"
110 state: present
111 register: result
112
113 - ansible.builtin.assert:
114 that:
115 - result is success and result is changed
116
117 How to create a compute instance?
118 =================================
119
120 Now that we have an SSH key configured, the next step is to spin up a server!
121 :ansplugin:`community.general.scaleway_compute#module` is a module that can create, update and delete Scaleway compute instances:
122
123 .. code-block:: yaml+jinja
124
125 - name: Create a server
126 community.general.scaleway_compute:
127 name: foobar
128 state: present
129 image: 00000000-1111-2222-3333-444444444444
130 organization: 00000000-1111-2222-3333-444444444444
131 region: ams1
132 commercial_type: START1-S
133
134 Here are the parameter details for the example shown above:
135
136 - ``name`` is the name of the instance (the one that will show up in your web console).
137 - ``image`` is the UUID of the system image you would like to use.
138 A list of all images is available for each availability zone.
139 - ``organization`` represents the organization that your account is attached to.
140 - ``region`` represents the Availability Zone which your instance is in (for this example, ``par1`` and ``ams1``).
141 - ``commercial_type`` represents the name of the commercial offers.
142 You can check out the Scaleway pricing page to find which instance is right for you.
143
144 Take a look at this short playbook to see a working example using ``scaleway_compute``:
145
146 .. code-block:: yaml+jinja
147
148 - name: Test compute instance lifecycle on a Scaleway account
149 hosts: localhost
150 gather_facts: false
151 environment:
152 SCW_API_KEY: ""
153
154 tasks:
155
156 - name: Create a server
157 register: server_creation_task
158 community.general.scaleway_compute:
159 name: foobar
160 state: present
161 image: 00000000-1111-2222-3333-444444444444
162 organization: 00000000-1111-2222-3333-444444444444
163 region: ams1
164 commercial_type: START1-S
165 wait: true
166
167 - ansible.builtin.debug:
168 var: server_creation_task
169
170 - ansible.builtin.assert:
171 that:
172 - server_creation_task is success
173 - server_creation_task is changed
174
175 - name: Run it
176 community.general.scaleway_compute:
177 name: foobar
178 state: running
179 image: 00000000-1111-2222-3333-444444444444
180 organization: 00000000-1111-2222-3333-444444444444
181 region: ams1
182 commercial_type: START1-S
183 wait: true
184 tags:
185 - web_server
186 register: server_run_task
187
188 - ansible.builtin.debug:
189 var: server_run_task
190
191 - ansible.builtin.assert:
192 that:
193 - server_run_task is success
194 - server_run_task is changed
195
196 Dynamic Inventory Plugin
197 ========================
198
199 Ansible ships with :ansplugin:`community.general.scaleway#inventory`.
200 You can now get a complete inventory of your Scaleway resources through this plugin and filter it on
201 different parameters (``regions`` and ``tags`` are currently supported).
202
203 Let us create an example!
204 Suppose that we want to get all hosts that got the tag web_server.
205 Create a file named ``scaleway_inventory.yml`` with the following content:
206
207 .. code-block:: yaml+jinja
208
209 plugin: community.general.scaleway
210 regions:
211 - ams1
212 - par1
213 tags:
214 - web_server
215
216 This inventory means that we want all hosts that got the tag ``web_server`` on the zones ``ams1`` and ``par1``.
217 Once you have configured this file, you can get the information using the following command:
218
219 .. code-block:: console
220
221 $ ansible-inventory --list -i scaleway_inventory.yml
222
223 The output will be:
224
225 .. code-block:: json
226
227 {
228 "_meta": {
229 "hostvars": {
230 "dd8e3ae9-0c7c-459e-bc7b-aba8bfa1bb8d": {
231 "ansible_verbosity": 6,
232 "arch": "x86_64",
233 "commercial_type": "START1-S",
234 "hostname": "foobar",
235 "ipv4": "192.0.2.1",
236 "organization": "00000000-1111-2222-3333-444444444444",
237 "state": "running",
238 "tags": [
239 "web_server"
240 ]
241 }
242 }
243 },
244 "all": {
245 "children": [
246 "ams1",
247 "par1",
248 "ungrouped",
249 "web_server"
250 ]
251 },
252 "ams1": {},
253 "par1": {
254 "hosts": [
255 "dd8e3ae9-0c7c-459e-bc7b-aba8bfa1bb8d"
256 ]
257 },
258 "ungrouped": {},
259 "web_server": {
260 "hosts": [
261 "dd8e3ae9-0c7c-459e-bc7b-aba8bfa1bb8d"
262 ]
263 }
264 }
265
266 As you can see, we get different groups of hosts.
267 ``par1`` and ``ams1`` are groups based on location.
268 ``web_server`` is a group based on a tag.
269
270 In case a filter parameter is not defined, the plugin supposes all values possible are wanted.
271 This means that for each tag that exists on your Scaleway compute nodes, a group based on each tag will be created.
272
273 Scaleway S3 object storage
274 ==========================
275
276 `Object Storage <https://www.scaleway.com/object-storage>`_ allows you to store any kind of objects (documents, images, videos, and so on).
277 As the Scaleway API is S3 compatible, Ansible supports it natively through the amazon.aws modules: :ansplugin:`amazon.aws.s3_bucket#module`, :ansplugin:`amazon.aws.s3_object#module`.
278
279 You can find many examples in the `scaleway_s3 integration tests <https://github.com/ansible/ansible-legacy-tests/tree/devel/test/legacy/roles/scaleway_s3>`_.
280
281 .. code-block:: yaml+jinja
282
283 - hosts: myserver
284 vars:
285 scaleway_region: nl-ams
286 s3_url: https://s3.nl-ams.scw.cloud
287 environment:
288 # AWS_ACCESS_KEY matches your scaleway organization id available at https://cloud.scaleway.com/#/account
289 AWS_ACCESS_KEY: 00000000-1111-2222-3333-444444444444
290 # AWS_SECRET_KEY matches a secret token that you can retrieve at https://cloud.scaleway.com/#/credentials
291 AWS_SECRET_KEY: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
292 module_defaults:
293 group/amazon.aws.aws:
294 s3_url: '{{ s3_url }}'
295 region: '{{ scaleway_region }}'
296 tasks:
297 # use a fact instead of a variable, otherwise template is evaluate each time variable is used
298 - ansible.builtin.set_fact:
299 bucket_name: "{{ 99999999 | random | to_uuid }}"
300
301 # "requester_pays:" is mandatory because Scaleway does not implement related API
302 # another way is to use amazon.aws.s3_object and "mode: create" !
303 - amazon.aws.s3_bucket:
304 name: '{{ bucket_name }}'
305 requester_pays:
306
307 - name: Another way to create the bucket
308 amazon.aws.s3_object:
309 bucket: '{{ bucket_name }}'
310 mode: create
311 encrypt: false
312 register: bucket_creation_check
313
314 - name: add something in the bucket
315 amazon.aws.s3_object:
316 mode: put
317 bucket: '{{ bucket_name }}'
318 src: /tmp/test.txt # needs to be created before
319 object: test.txt
320 encrypt: false # server side encryption must be disabled