Debian Codegraph @

ansible_collections/community/general/docs/docsite/rst/guide_packet.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_packet:
7
8 **********************************
9 Packet.net Guide
10 **********************************
11
12 Introduction
13 ============
14
15 `Packet.net <https://packet.net>`_ is a bare metal infrastructure host that is supported by the community.general collection through six cloud modules. The six modules are:
16
17 - :ansplugin:`community.general.packet_device#module`: manages servers on Packet. You can use this module to create, restart and delete devices.
18 - :ansplugin:`community.general.packet_ip_subnet#module`: assign IP subnet to a bare metal server
19 - :ansplugin:`community.general.packet_project#module`: create/delete a project in Packet host
20 - :ansplugin:`community.general.packet_sshkey#module`: adds a public SSH key from file or value to the Packet infrastructure. Every subsequently-created device will have this public key installed in .ssh/authorized_keys.
21 - :ansplugin:`community.general.packet_volume#module`: create/delete a volume in Packet host
22 - :ansplugin:`community.general.packet_volume_attachment#module`: attach/detach a volume to a device in the Packet host
23
24 Note, this guide assumes you are familiar with Ansible and how it works. If you are not, have a look at their :ref:`docs <ansible_documentation>` before getting started.
25
26 Requirements
27 ============
28
29 The Packet modules connect to the Packet API using the `packet-python package <https://pypi.org/project/packet-python/>`_. You can install it with pip:
30
31 .. code-block:: console
32
33 $ pip install packet-python
34
35 In order to check the state of devices created by Ansible on Packet, it is a good idea to install one of the `Packet CLI clients <https://www.packet.net/developers/integrations/>`_. Otherwise you can check them through the `Packet portal <https://app.packet.net/portal>`_.
36
37 To use the modules you will need a Packet API token. You can generate an API token through the Packet portal `here <https://app.packet.net/portal#/api-keys>`__. The simplest way to authenticate yourself is to set the Packet API token in an environment variable:
38
39 .. code-block:: console
40
41 $ export PACKET_API_TOKEN=Bfse9F24SFtfs423Gsd3ifGsd43sSdfs
42
43 If you are not comfortable exporting your API token, you can pass it as a parameter to the modules.
44
45 On Packet, devices and reserved IP addresses belong to `projects <https://www.packet.com/developers/api/#projects>`_. In order to use the packet_device module, you need to specify the UUID of the project in which you want to create or manage devices. You can find a project's UUID in the Packet portal `here <https://app.packet.net/portal#/projects/list/table/>`_ (it is just under the project table) or through one of the available `CLIs <https://www.packet.net/developers/integrations/>`_.
46
47
48 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:
49
50 .. code-block:: console
51
52 $ ssh-keygen -t rsa -f ./id_rsa
53
54 If you want to use an existing key pair, just copy the private and public key over to the playbook directory.
55
56
57 Device Creation
58 ===============
59
60 The following code block is a simple playbook that creates one `Type 0 <https://www.packet.com/cloud/servers/t1-small/>`_ server (the ``plan`` parameter). You have to supply ``plan`` and ``operating_system``. ``location`` defaults to ``ewr1`` (Parsippany, NJ). You can find all the possible values for the parameters through a `CLI client <https://www.packet.net/developers/integrations/>`_.
61
62 .. code-block:: yaml+jinja
63
64 # playbook_create.yml
65
66 - name: Create Ubuntu device
67 hosts: localhost
68 tasks:
69
70 - community.general.packet_sshkey:
71 key_file: ./id_rsa.pub
72 label: tutorial key
73
74 - community.general.packet_device:
75 project_id: <your_project_id>
76 hostnames: myserver
77 operating_system: ubuntu_16_04
78 plan: baremetal_0
79 facility: sjc1
80
81 After running ``ansible-playbook playbook_create.yml``, you should have a server provisioned on Packet. You can verify through a CLI or in the `Packet portal <https://app.packet.net/portal#/projects/list/table>`__.
82
83 If you get an error with the message "failed to set machine state present, error: Error 404: Not Found", please verify your project UUID.
84
85
86 Updating Devices
87 ================
88
89 The two parameters used to uniquely identify Packet devices are: "device_ids" and "hostnames". Both parameters accept either a single string (later converted to a one-element list), or a list of strings.
90
91 The ``device_ids`` and ``hostnames`` parameters are mutually exclusive. The following values are all acceptable:
92
93 - device_ids: ``a27b7a83-fc93-435b-a128-47a5b04f2dcf``
94
95 - hostnames: ``mydev1``
96
97 - device_ids: ``[a27b7a83-fc93-435b-a128-47a5b04f2dcf, 4887130f-0ccd-49a0-99b0-323c1ceb527b]``
98
99 - hostnames: ``[mydev1, mydev2]``
100
101 In addition, hostnames can contain a special ``%d`` formatter along with a ``count`` parameter that lets you easily expand hostnames that follow a simple name and number pattern; in other words, ``hostnames: "mydev%d", count: 2`` will expand to [mydev1, mydev2].
102
103 If your playbook acts on existing Packet devices, you can only pass the ``hostname`` and ``device_ids`` parameters. The following playbook shows how you can reboot a specific Packet device by setting the ``hostname`` parameter:
104
105 .. code-block:: yaml+jinja
106
107 # playbook_reboot.yml
108
109 - name: reboot myserver
110 hosts: localhost
111 tasks:
112
113 - community.general.packet_device:
114 project_id: <your_project_id>
115 hostnames: myserver
116 state: rebooted
117
118 You can also identify specific Packet devices with the ``device_ids`` parameter. The device's UUID can be found in the `Packet Portal <https://app.packet.net/portal>`_ or by using a `CLI <https://www.packet.net/developers/integrations/>`_. The following playbook removes a Packet device using the ``device_ids`` field:
119
120 .. code-block:: yaml+jinja
121
122 # playbook_remove.yml
123
124 - name: remove a device
125 hosts: localhost
126 tasks:
127
128 - community.general.packet_device:
129 project_id: <your_project_id>
130 device_ids: <myserver_device_id>
131 state: absent
132
133
134 More Complex Playbooks
135 ======================
136
137 In this example, we will create a CoreOS cluster with `user data <https://packet.com/developers/docs/servers/key-features/user-data/>`_.
138
139
140 The CoreOS cluster will use `etcd <https://etcd.io/>`_ for discovery of other servers in the cluster. Before provisioning your servers, you will need to generate a discovery token for your cluster:
141
142 .. code-block:: console
143
144 $ curl -w "\n" 'https://discovery.etcd.io/new?size=3'
145
146 The following playbook will create an SSH key, 3 Packet servers, and then wait until SSH is ready (or until 5 minutes passed). Make sure to substitute the discovery token URL in ``user_data``, and the ``project_id`` before running ``ansible-playbook``. Also, feel free to change ``plan`` and ``facility``.
147
148 .. code-block:: yaml+jinja
149
150 # playbook_coreos.yml
151
152 - name: Start 3 CoreOS nodes in Packet and wait until SSH is ready
153 hosts: localhost
154 tasks:
155
156 - community.general.packet_sshkey:
157 key_file: ./id_rsa.pub
158 label: new
159
160 - community.general.packet_device:
161 hostnames: [coreos-one, coreos-two, coreos-three]
162 operating_system: coreos_beta
163 plan: baremetal_0
164 facility: ewr1
165 project_id: <your_project_id>
166 wait_for_public_IPv: 4
167 user_data: |
168 # cloud-config
169 coreos:
170 etcd2:
171 discovery: https://discovery.etcd.io/<token>
172 advertise-client-urls: http://$private_ipv4:2379,http://$private_ipv4:4001
173 initial-advertise-peer-urls: http://$private_ipv4:2380
174 listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
175 listen-peer-urls: http://$private_ipv4:2380
176 fleet:
177 public-ip: $private_ipv4
178 units:
179 - name: etcd2.service
180 command: start
181 - name: fleet.service
182 command: start
183 register: newhosts
184
185 - name: wait for ssh
186 ansible.builtin.wait_for:
187 delay: 1
188 host: "{{ item.public_ipv4 }}"
189 port: 22
190 state: started
191 timeout: 500
192 loop: "{{ newhosts.results[0].devices }}"
193
194
195 As with most Ansible modules, the default states of the Packet modules are idempotent, meaning the resources in your project will remain the same after re-runs of a playbook. Thus, we can keep the ``packet_sshkey`` module call in our playbook. If the public key is already in your Packet account, the call will have no effect.
196
197 The second module call provisions 3 Packet Type 0 (specified using the ``plan`` parameter) servers in the project identified by the ``project_id`` parameter. The servers are all provisioned with CoreOS beta (the ``operating_system`` parameter) and are customized with cloud-config user data passed to the ``user_data`` parameter.
198
199 The ``packet_device`` module has a ``wait_for_public_IPv`` that is used to specify the version of the IP address to wait for (valid values are ``4`` or ``6`` for IPv4 or IPv6). If specified, Ansible will wait until the GET API call for a device contains an Internet-routeable IP address of the specified version. When referring to an IP address of a created device in subsequent module calls, it is wise to use the ``wait_for_public_IPv`` parameter, or ``state: active`` in the packet_device module call.
200
201 Run the playbook:
202
203 .. code-block:: console
204
205 $ ansible-playbook playbook_coreos.yml
206
207 Once the playbook quits, your new devices should be reachable through SSH. Try to connect to one and check if etcd has started properly:
208
209 .. code-block:: console
210
211 tomk@work $ ssh -i id_rsa core@$one_of_the_servers_ip
212 core@coreos-one ~ $ etcdctl cluster-health
213
214 If you have any questions or comments let us know! [email protected]