summaryrefslogtreecommitdiff
path: root/cloud/openstack/_nova_keypair.py
blob: cabe35ea87da1327a4a59a4c74dd4e78329d7ec5 (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
#!/usr/bin/python
#coding: utf-8 -*-

# (c) 2013, Benno Joy <benno@ansible.com>
# (c) 2013, John Dewey <john@dewey.ws>
#
# This module 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.
#
# This software 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 this software.  If not, see <http://www.gnu.org/licenses/>.

import time
try:
    from novaclient.v1_1 import client as nova_client
    from novaclient import exceptions as exc
    HAS_NOVACLIENT = True
except ImportError:
    HAS_NOVACLIENT = False

DOCUMENTATION = '''
---
module: nova_keypair
version_added: "1.2"
author:
    - "Benno Joy (@bennojoy)"
    - "Michael DeHaan"
deprecated: Deprecated in 2.0. Use os_keypair instead
short_description: Add/Delete key pair from nova
description:
   - Add or Remove key pair from nova .
options:
   login_username:
     description:
        - login username to authenticate to keystone
     required: true
     default: admin
   login_password:
     description:
        - Password of login user
     required: true
     default: 'yes'
   login_tenant_name:
     description:
        - The tenant name of the login user
     required: true
     default: 'yes'
   auth_url:
     description:
        - The keystone url for authentication
     required: false
     default: http://127.0.0.1:35357/v2.0/
   region_name:
     description:
        - Name of the region
     required: false
     default: None
   state:
     description:
        - Indicate desired state of the resource
     choices: ['present', 'absent']
     default: present
   name:
     description:
        - Name that has to be given to the key pair
     required: true
     default: None
   public_key:
     description:
        - The public key that would be uploaded to nova and injected to vm's upon creation
     required: false
     default: None

requirements:
    - "python >= 2.6"
    - "python-novaclient"
'''
EXAMPLES = '''
- name: Create a key pair with the running users public key
  nova_keypair:
    state: present
    login_username: admin
    login_password: admin
    login_tenant_name: admin
    name: ansible_key
    public_key: "{{ lookup('file','~/.ssh/id_rsa.pub') }}"

- name: Create a new key pair and the private key returned after the run.
  nova_keypair:
    state: present
    login_username: admin
    login_password: admin
    login_tenant_name: admin
    name: ansible_key
'''

def main():
    argument_spec = openstack_argument_spec()
    argument_spec.update(dict(
        name                            = dict(required=True),
        public_key                      = dict(default=None),
        state                           = dict(default='present', choices=['absent', 'present'])
    ))
    module = AnsibleModule(argument_spec=argument_spec)
    if not HAS_NOVACLIENT:
        module.fail_json(msg='python-novaclient is required for this module to work')

    nova = nova_client.Client(module.params['login_username'],
                              module.params['login_password'],
                              module.params['login_tenant_name'],
                              module.params['auth_url'],
                              region_name=module.params['region_name'],
                              service_type='compute')
    try:
        nova.authenticate()
    except exc.Unauthorized as e:
        module.fail_json(msg = "Invalid OpenStack Nova credentials.: %s" % e.message)
    except exc.AuthorizationFailure as e:
        module.fail_json(msg = "Unable to authorize user: %s" % e.message)

    if module.params['state'] == 'present':
        for key in nova.keypairs.list():
            if key.name == module.params['name']:
                if module.params['public_key'] and (module.params['public_key'] != key.public_key ):
                    module.fail_json(msg = "name {} present but key hash not the same as offered.  Delete key first.".format(key['name']))
                else:
                    module.exit_json(changed = False, result = "Key present")            
        try:
            key = nova.keypairs.create(module.params['name'], module.params['public_key'])
        except Exception as e:
            module.exit_json(msg = "Error in creating the keypair: %s" % e.message)
        if not module.params['public_key']:
            module.exit_json(changed = True, key = key.private_key)
        module.exit_json(changed = True, key = None)
    if module.params['state'] == 'absent':
        for key in nova.keypairs.list():
            if key.name == module.params['name']:
                try:
                    nova.keypairs.delete(module.params['name'])
                except Exception as e:
                    module.fail_json(msg = "The keypair deletion has failed: %s" % e.message)
                module.exit_json( changed = True, result = "deleted")
        module.exit_json(changed = False, result = "not present")

# this is magic, see lib/ansible/module.params['common.py
from ansible.module_utils.basic import *
from ansible.module_utils.openstack import *
if __name__ == '__main__':
    main()