summaryrefslogtreecommitdiff
path: root/cloud/cloudstack/cs_instance_facts.py
blob: 2aee631395d0dd9d774f5984630524cbfa668e18 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (c) 2016, René Moser <mail@renemoser.net>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

ANSIBLE_METADATA = {'status': ['stableinterface'],
                    'supported_by': 'community',
                    'version': '1.0'}

DOCUMENTATION = '''
---
module: cs_instance_facts
short_description: Gathering facts from the API of instances from Apache CloudStack based clouds.
description:
    - Gathering facts from the API of an instance.
version_added: "2.1"
author: "René Moser (@resmo)"
options:
  name:
    description:
      - Name or display name of the instance.
    required: true
  domain:
    description:
      - Domain the instance is related to.
    required: false
    default: null
  account:
    description:
      - Account the instance is related to.
    required: false
    default: null
  project:
    description:
      - Project the instance is related to.
    required: false
    default: null
extends_documentation_fragment: cloudstack
'''

EXAMPLES = '''
- cs_instance_facts:
    name: web-vm-1
  delegate_to: localhost

- debug:
    var: cloudstack_instance
'''

RETURN = '''
---
cloudstack_instance.id:
  description: UUID of the instance.
  returned: success
  type: string
  sample: 04589590-ac63-4ffc-93f5-b698b8ac38b6
cloudstack_instance.name:
  description: Name of the instance.
  returned: success
  type: string
  sample: web-01
cloudstack_instance.display_name:
  description: Display name of the instance.
  returned: success
  type: string
  sample: web-01
cloudstack_instance.group:
  description: Group name of the instance is related.
  returned: success
  type: string
  sample: web
created:
  description: Date of the instance was created.
  returned: success
  type: string
  sample: 2014-12-01T14:57:57+0100
cloudstack_instance.password_enabled:
  description: True if password setting is enabled.
  returned: success
  type: boolean
  sample: true
cloudstack_instance.password:
  description: The password of the instance if exists.
  returned: success
  type: string
  sample: Ge2oe7Do
cloudstack_instance.ssh_key:
  description: Name of SSH key deployed to instance.
  returned: success
  type: string
  sample: key@work
cloudstack_instance.domain:
  description: Domain the instance is related to.
  returned: success
  type: string
  sample: example domain
cloudstack_instance.account:
  description: Account the instance is related to.
  returned: success
  type: string
  sample: example account
cloudstack_instance.project:
  description: Name of project the instance is related to.
  returned: success
  type: string
  sample: Production
cloudstack_instance.default_ip:
  description: Default IP address of the instance.
  returned: success
  type: string
  sample: 10.23.37.42
cloudstack_instance.public_ip:
  description: Public IP address with instance via static NAT rule.
  returned: success
  type: string
  sample: 1.2.3.4
cloudstack_instance.iso:
  description: Name of ISO the instance was deployed with.
  returned: success
  type: string
  sample: Debian-8-64bit
cloudstack_instance.template:
  description: Name of template the instance was deployed with.
  returned: success
  type: string
  sample: Debian-8-64bit
cloudstack_instance.service_offering:
  description: Name of the service offering the instance has.
  returned: success
  type: string
  sample: 2cpu_2gb
cloudstack_instance.zone:
  description: Name of zone the instance is in.
  returned: success
  type: string
  sample: ch-gva-2
cloudstack_instance.state:
  description: State of the instance.
  returned: success
  type: string
  sample: Running
cloudstack_instance.security_groups:
  description: Security groups the instance is in.
  returned: success
  type: list
  sample: '[ "default" ]'
cloudstack_instance.affinity_groups:
  description: Affinity groups the instance is in.
  returned: success
  type: list
  sample: '[ "webservers" ]'
cloudstack_instance.tags:
  description: List of resource tags associated with the instance.
  returned: success
  type: dict
  sample: '[ { "key": "foo", "value": "bar" } ]'
cloudstack_instance.hypervisor:
  description: Hypervisor related to this instance.
  returned: success
  type: string
  sample: KVM
cloudstack_instance.instance_name:
  description: Internal name of the instance (ROOT admin only).
  returned: success
  type: string
  sample: i-44-3992-VM
'''

import base64

# import cloudstack common
from ansible.module_utils.cloudstack import *

class AnsibleCloudStackInstanceFacts(AnsibleCloudStack):

    def __init__(self, module):
        super(AnsibleCloudStackInstanceFacts, self).__init__(module)
        self.instance = None
        self.returns = {
            'group':                'group',
            'hypervisor':           'hypervisor',
            'instancename':         'instance_name',
            'publicip':             'public_ip',
            'passwordenabled':      'password_enabled',
            'password':             'password',
            'serviceofferingname':  'service_offering',
            'isoname':              'iso',
            'templatename':         'template',
            'keypair':              'ssh_key',
        }
        self.facts = {
            'cloudstack_instance': None,
        }


    def get_instance(self):
        instance = self.instance
        if not instance:
            instance_name = self.module.params.get('name')

            args                = {}
            args['account']     = self.get_account(key='name')
            args['domainid']    = self.get_domain(key='id')
            args['projectid']   = self.get_project(key='id')
            # Do not pass zoneid, as the instance name must be unique across zones.
            instances = self.cs.listVirtualMachines(**args)
            if instances:
                for v in instances['virtualmachine']:
                    if instance_name.lower() in [ v['name'].lower(), v['displayname'].lower(), v['id'] ]:
                        self.instance = v
                        break
        return self.instance


    def run(self):
        instance = self.get_instance()
        if not instance:
            self.module.fail_json(msg="Instance not found: %s" % self.module.params.get('name'))
        self.facts['cloudstack_instance'] = self.get_result(instance)
        return  self.facts


    def get_result(self, instance):
        super(AnsibleCloudStackInstanceFacts, self).get_result(instance)
        if instance:
            if 'securitygroup' in instance:
                security_groups = []
                for securitygroup in instance['securitygroup']:
                    security_groups.append(securitygroup['name'])
                self.result['security_groups'] = security_groups
            if 'affinitygroup' in instance:
                affinity_groups = []
                for affinitygroup in instance['affinitygroup']:
                    affinity_groups.append(affinitygroup['name'])
                self.result['affinity_groups'] = affinity_groups
            if 'nic' in instance:
                for nic in instance['nic']:
                    if nic['isdefault'] and 'ipaddress' in nic:
                        self.result['default_ip'] = nic['ipaddress']
        return self.result


def main():
    argument_spec = cs_argument_spec()
    argument_spec.update(dict(
        name = dict(required=True),
        domain = dict(default=None),
        account = dict(default=None),
        project = dict(default=None),
    ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=False,
    )

    cs_instance_facts = AnsibleCloudStackInstanceFacts(module=module).run()
    cs_facts_result = dict(changed=False, ansible_facts=cs_instance_facts)
    module.exit_json(**cs_facts_result)

from ansible.module_utils.basic import *
if __name__ == '__main__':
    main()