summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/web_infrastructure/ansible_tower/tower_inventory_source.py
blob: 9aec7a8b95feb62e85a6df408c5fbf22f39b8355 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/python
# coding: utf-8 -*-

# Copyright: (c) 2018, Adrien Fleury <fleu42@gmail.com>
# 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


ANSIBLE_METADATA = {'status': ['preview'],
                    'supported_by': 'community',
                    'metadata_version': '1.1'}


DOCUMENTATION = '''
---
module: tower_inventory_source
author: "Adrien Fleury (@fleu42)"
version_added: "2.7"
short_description: create, update, or destroy Ansible Tower inventory source.
description:
    - Create, update, or destroy Ansible Tower inventories source. See
      U(https://www.ansible.com/tower) for an overview.
options:
    name:
      description:
        - The name to use for the inventory source.
      required: True
    description:
      description:
        - The description to use for the inventory source.
    inventory:
      description:
        - The inventory the source is linked to.
      required: True
    source:
      description:
        - Types of inventory source.
      choices:
        - file
        - scm
        - ec2
        - gce
        - azure
        - azure_rm
        - vmware
        - satellite6
        - cloudforms
        - openstack
        - rhv
        - tower
        - custom
      required: True
    credential:
      description:
        - Credential to use to retrieve the inventory from.
    source_vars:
      description:
        - >-
          The source_vars allow to Override variables found in the source config
          file. For example with Openstack, specifying *private: false* would
          change the output of the openstack.py script. It has to be YAML or
          JSON.
    timeout:
      description:
        - Number in seconds after which the Tower API methods will time out.
    source_project:
      description:
        - Use a *project* as a source for the *inventory*.
    source_path:
      description:
        - Path to the file to use as a source in the selected *project*.
    update_on_project_update:
      description:
        - >-
          That parameter will sync the inventory when the project is synced. It
          can only be used with a SCM source.
      type: bool
    source_regions:
      description:
        - >-
          List of regions for your cloud provider. You can include multiple all
          regions. Only Hosts associated with the selected regions will be
          updated. Refer to Ansible Tower documentation for more detail.
    instance_filters:
      description:
        - >-
          Provide a comma-separated list of filter expressions. Hosts are
          imported when all of the filters match. Refer to Ansible Tower
          documentation for more detail.
    group_by:
      description:
        - >-
          Specify which groups to create automatically. Group names will be
          created similar to the options selected. If blank, all groups above
          are created. Refer to Ansible Tower documentation for more detail.
    source_script:
      description:
        - >-
          The source custom script to use to build the inventory. It needs to
          exist.
    overwrite:
      description:
        - >-
          If set, any hosts and groups that were previously present on the
          external source but are now removed will be removed from the Tower
          inventory. Hosts and groups that were not managed by the inventory
          source will be promoted to the next manually created group or if
          there is no manually created group to promote them into, they will be
          left in the "all" default group for the inventory. When not checked,
          local child hosts and groups not found on the external source will
          remain untouched by the inventory update process.
      type: bool
    overwrite_vars:
      description:
        - >-
          If set, all variables for child groups and hosts will be removed
          and replaced by those found on the external source. When not checked,
          a merge will be performed, combining local variables with those found
          on the external source.
      type: bool
    update_on_launch:
      description:
        - >-
          Each time a job runs using this inventory, refresh the inventory from
          the selected source before executing job tasks.
      type: bool
    update_cache_timeout:
      description:
        - >-
          Time in seconds to consider an inventory sync to be current. During
          job runs and callbacks the task system will evaluate the timestamp of
          the latest sync. If it is older than Cache Timeout, it is not
          considered current, and a new inventory sync will be performed.
    state:
      description:
        - Desired state of the resource.
      default: "present"
      choices: ["present", "absent"]
    validate_certs:
      description:
        - Tower option to avoid certificates check.
      type: bool
      aliases: [ tower_verify_ssl ]
extends_documentation_fragment: tower
'''


EXAMPLES = '''
- name: Add tower inventory source
  tower_inventory_source:
    name: Inventory source
    description: My Inventory source
    inventory: My inventory
    credential: Devstack_credential
    source: openstack
    update_on_launch: true
    overwrite: true
    source_vars: '{ private: false }'
    state: present
    validate_certs: false
'''


RETURN = ''' # '''


from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, tower_check_mode

try:
    import tower_cli
    import tower_cli.exceptions as exc
    from tower_cli.conf import settings
except ImportError:
    pass


SOURCE_CHOICES = {
    'file': 'Directory or Script',
    'scm': 'Sourced from a Project',
    'ec2': 'Amazon EC2',
    'gce': 'Google Compute Engine',
    'azure': 'Microsoft Azure',
    'azure_rm': 'Microsoft Azure Resource Manager',
    'vmware': 'VMware vCenter',
    'satellite6': 'Red Hat Satellite 6',
    'cloudforms': 'Red Hat CloudForms',
    'openstack': 'OpenStack',
    'rhv': 'Red Hat Virtualization',
    'tower': 'Ansible Tower',
    'custom': 'Custom Script',
}


def main():
    argument_spec = dict(
        name=dict(required=True),
        description=dict(required=False),
        inventory=dict(required=True),
        source=dict(required=True,
                    choices=SOURCE_CHOICES.keys()),
        credential=dict(required=False),
        source_vars=dict(required=False),
        timeout=dict(type='int', required=False),
        source_project=dict(required=False),
        source_path=dict(required=False),
        update_on_project_update=dict(type='bool', required=False),
        source_regions=dict(required=False),
        instance_filters=dict(required=False),
        group_by=dict(required=False),
        source_script=dict(required=False),
        overwrite=dict(type='bool', required=False),
        overwrite_vars=dict(type='bool', required=False),
        update_on_launch=dict(type='bool', required=False),
        update_cache_timeout=dict(type='int', required=False),
        state=dict(choices=['present', 'absent'], default='present'),
    )

    module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)

    name = module.params.get('name')
    inventory = module.params.get('inventory')
    source = module.params.get('source')
    state = module.params.get('state')

    json_output = {'inventory_source': name, 'state': state}

    tower_auth = tower_auth_config(module)
    with settings.runtime_values(**tower_auth):
        tower_check_mode(module)
        inventory_source = tower_cli.get_resource('inventory_source')
        try:
            params = {}
            params['name'] = name
            params['source'] = source

            if module.params.get('description'):
                params['description'] = module.params.get('description')

            if module.params.get('credential'):
                credential_res = tower_cli.get_resource('credential')
                try:
                    credential = credential_res.get(
                        name=module.params.get('credential'))
                    params['credential'] = credential['id']
                except (exc.NotFound) as excinfo:
                    module.fail_json(
                        msg='Failed to update credential source,'
                        'credential not found: {0}'.format(excinfo),
                        changed=False
                    )

            if module.params.get('source_project'):
                source_project_res = tower_cli.get_resource('project')
                try:
                    source_project = source_project_res.get(
                        name=module.params.get('source_project'))
                    params['source_project'] = source_project['id']
                except (exc.NotFound) as excinfo:
                    module.fail_json(
                        msg='Failed to update source project,'
                        'project not found: {0}'.format(excinfo),
                        changed=False
                    )

            if module.params.get('source_script'):
                source_script_res = tower_cli.get_resource('inventory_script')
                try:
                    script = source_script_res.get(
                        name=module.params.get('source_script'))
                    params['source_script'] = script['id']
                except (exc.NotFound) as excinfo:
                    module.fail_json(
                        msg='Failed to update source script,'
                        'script not found: {0}'.format(excinfo),
                        changed=False
                    )

            try:
                inventory_res = tower_cli.get_resource('inventory')
                params['inventory'] = inventory_res.get(name=inventory)['id']
            except (exc.NotFound) as excinfo:
                module.fail_json(
                    msg='Failed to update inventory source, '
                    'inventory not found: {0}'.format(excinfo),
                    changed=False
                )

            for key in ('source_vars', 'timeout', 'source_path',
                        'update_on_project_update', 'source_regions',
                        'instance_filters', 'group_by', 'overwrite',
                        'overwrite_vars', 'update_on_launch',
                        'update_cache_timeout'):
                if module.params.get(key) is not None:
                    params[key] = module.params.get(key)

            if state == 'present':
                params['create_on_missing'] = True
                result = inventory_source.modify(**params)
                json_output['id'] = result['id']
            elif state == 'absent':
                params['fail_on_missing'] = False
                result = inventory_source.delete(**params)

        except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo:
            module.fail_json(msg='Failed to update inventory source: \
                    {0}'.format(excinfo), changed=False)

    json_output['changed'] = result['changed']
    module.exit_json(**json_output)


if __name__ == '__main__':
    main()