summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/digitalocean/plugins/inventory/digitalocean.py
blob: 6aafb5f45b4aa3cc9a44fade5035615c8f54c0ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-

# Copyright: (c), Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = r"""
name: digitalocean
author:
  - Janos Gerzson (@grzs)
  - Tadej Borovšak (@tadeboro)
  - Max Truxa (@maxtruxa)
short_description: DigitalOcean Inventory Plugin
version_added: "1.1.0"
description:
  - DigitalOcean (DO) inventory plugin.
  - Acquires droplet list from DO API.
  - Uses configuration file that ends with '(do_hosts|digitalocean|digital_ocean).(yaml|yml)'.
extends_documentation_fragment:
  - community.digitalocean.digital_ocean.documentation
  - constructed
  - inventory_cache
options:
  plugin:
    description:
      - The name of the DigitalOcean Inventory Plugin,
        this should always be C(community.digitalocean.digitalocean).
    required: true
    choices: ['community.digitalocean.digitalocean']
  api_token:
    description:
     - DigitalOcean OAuth token.
     - Template expressions can be used in this field.
    required: true
    type: str
    aliases: [ oauth_token ]
    env:
      - name: DO_API_TOKEN
  attributes:
    description: >-
      Droplet attributes to add as host vars to each inventory host.
      Check out the DO API docs for full list of attributes at
      U(https://docs.digitalocean.com/reference/api/api-reference/#operation/list_all_droplets).
    type: list
    elements: str
    default:
      - id
      - name
      - networks
      - region
      - size_slug
  var_prefix:
    description:
      - Prefix of generated varible names (e.g. C(tags) -> C(do_tags))
    type: str
    default: 'do_'
  pagination:
    description:
      - Maximum droplet objects per response page.
      - If the number of droplets related to the account exceeds this value,
        the query will be broken to multiple requests (pages).
      - DigitalOcean currently allows a maximum of 200.
    type: int
    default: 200
  filters:
    description:
      - Filter hosts with Jinja templates.
      - If no filters are specified, all hosts are added to the inventory.
    type: list
    elements: str
    default: []
    version_added: '1.5.0'
"""

EXAMPLES = r"""
# Using keyed groups and compose for hostvars
plugin: community.digitalocean.digitalocean
api_token: '{{ lookup("pipe", "./get-do-token.sh" }}'
attributes:
  - id
  - name
  - memory
  - vcpus
  - disk
  - size
  - image
  - networks
  - volume_ids
  - tags
  - region
keyed_groups:
  - key: do_region.slug
    prefix: 'region'
    separator: '_'
  - key: do_tags | lower
    prefix: ''
    separator: ''
compose:
  ansible_host: do_networks.v4 | selectattr('type','eq','public')
    | map(attribute='ip_address') | first
  class: do_size.description | lower
  distro: do_image.distribution | lower
filters:
  - '"kubernetes" in do_tags'
  - 'do_region.slug == "fra1"'
"""

import re
import json
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.inventory.group import to_safe_group_name
from ansible.module_utils._text import to_native
from ansible.module_utils.urls import Request
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable


class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):

    NAME = "community.digitalocean.digitalocean"

    # Constructable methods use the following function to construct group names. By
    # default, characters that are not valid in python variables, are always replaced by
    # underscores. We are overriding this with a function that respects the
    # TRANSFORM_INVALID_GROUP_CHARS configuration option and allows users to control the
    # behavior.
    _sanitize_group_name = staticmethod(to_safe_group_name)

    def verify_file(self, path):
        valid = False
        if super(InventoryModule, self).verify_file(path):
            if path.endswith(
                (
                    "do_hosts.yaml",
                    "do_hosts.yml",
                    "digitalocean.yaml",
                    "digitalocean.yml",
                    "digital_ocean.yaml",
                    "digital_ocean.yml",
                )
            ):
                valid = True
            else:
                self.display.vvv(
                    "Skipping due to inventory source file name mismatch. "
                    "The file name has to end with one of the following: "
                    "do_hosts.yaml, do_hosts.yml "
                    "digitalocean.yaml, digitalocean.yml, "
                    "digital_ocean.yaml, digital_ocean.yml."
                )
        return valid

    def _template_option(self, option):
        value = self.get_option(option)
        self.templar.available_variables = {}
        return self.templar.template(value)

    def _get_payload(self):
        # request parameters
        api_token = self._template_option("api_token")
        headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer {0}".format(api_token),
        }

        # build url
        pagination = self.get_option("pagination")
        url = "https://api.digitalocean.com/v2"
        if self.get_option("baseurl"):
            url = self.get_option("baseurl")
        url += "/droplets?per_page=" + str(pagination)

        # send request(s)
        self.req = Request(headers=headers, timeout=self.get_option("timeout"))
        payload = []
        try:
            while url:
                self.display.vvv("Sending request to {0}".format(url))
                response = json.load(self.req.get(url))
                payload.extend(response["droplets"])
                url = response.get("links", {}).get("pages", {}).get("next")
        except ValueError:
            raise AnsibleParserError("something went wrong with JSON loading")
        except (URLError, HTTPError) as error:
            raise AnsibleParserError(error)

        return payload

    def _populate(self, records):
        attributes = self.get_option("attributes")
        var_prefix = self.get_option("var_prefix")
        strict = self.get_option("strict")
        host_filters = self.get_option("filters")
        for record in records:

            host_name = record.get("name")
            if not host_name:
                continue

            host_vars = {}
            for k, v in record.items():
                if k in attributes:
                    host_vars[var_prefix + k] = v

            if not self._passes_filters(host_filters, host_vars, host_name, strict):
                self.display.vvv("Host {0} did not pass all filters".format(host_name))
                continue

            # add host to inventory
            self.inventory.add_host(host_name)

            # set variables for host
            for k, v in host_vars.items():
                self.inventory.set_variable(host_name, k, v)

            self._set_composite_vars(
                self.get_option("compose"),
                self.inventory.get_host(host_name).get_vars(),
                host_name,
                strict,
            )

            # set composed and keyed groups
            self._add_host_to_composed_groups(
                self.get_option("groups"), dict(), host_name, strict
            )
            self._add_host_to_keyed_groups(
                self.get_option("keyed_groups"), dict(), host_name, strict
            )

    def _passes_filters(self, filters, variables, host, strict=False):
        if filters and isinstance(filters, list):
            for template in filters:
                try:
                    if not self._compose(template, variables):
                        return False
                except Exception as e:
                    if strict:
                        raise AnsibleError(
                            "Could not evaluate host filter {0} for host {1}: {2}".format(
                                template, host, to_native(e)
                            )
                        )
                    # Better be safe and not include any hosts by accident.
                    return False
        return True

    def parse(self, inventory, loader, path, cache=True):
        super(InventoryModule, self).parse(inventory, loader, path)

        self._read_config_data(path)

        # cache settings
        cache_key = self.get_cache_key(path)
        use_cache = self.get_option("cache") and cache
        update_cache = self.get_option("cache") and not cache

        records = None
        if use_cache:
            try:
                records = self._cache[cache_key]
            except KeyError:
                update_cache = True

        if records is None:
            records = self._get_payload()

        if update_cache:
            self._cache[cache_key] = records

        self._populate(records)