2013-05-23 02:25:05 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#coding: utf-8 -*-
|
|
|
|
|
2014-01-28 16:20:36 +00:00
|
|
|
# (c) 2013, Benno Joy <benno@ansible.com>
|
2013-11-23 05:37:47 +00:00
|
|
|
# (c) 2013, John Dewey <john@dewey.ws>
|
2013-05-23 02:25:05 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
from novaclient.v1_1 import client
|
2013-11-23 05:37:47 +00:00
|
|
|
from novaclient import exceptions
|
2013-05-23 02:25:05 +00:00
|
|
|
import time
|
|
|
|
except ImportError:
|
|
|
|
print("failed=True msg='novaclient is required for this module to work'")
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: nova_keypair
|
2013-11-18 23:55:49 +00:00
|
|
|
version_added: "1.2"
|
2013-06-14 09:53:43 +00:00
|
|
|
short_description: Add/Delete key pair from nova
|
2013-05-23 02:25:05 +00:00
|
|
|
description:
|
2013-06-14 09:53:43 +00:00
|
|
|
- Add or Remove key pair from nova .
|
2013-05-23 02:25:05 +00:00
|
|
|
options:
|
|
|
|
login_username:
|
|
|
|
description:
|
|
|
|
- login username to authenticate to keystone
|
|
|
|
required: true
|
|
|
|
default: admin
|
|
|
|
login_password:
|
|
|
|
description:
|
|
|
|
- Password of login user
|
|
|
|
required: true
|
2013-06-01 15:52:28 +00:00
|
|
|
default: 'yes'
|
2013-05-23 02:25:05 +00:00
|
|
|
login_tenant_name:
|
|
|
|
description:
|
|
|
|
- The tenant name of the login user
|
|
|
|
required: true
|
2013-06-01 15:52:28 +00:00
|
|
|
default: 'yes'
|
2013-05-23 02:25:05 +00:00
|
|
|
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:
|
2013-06-14 09:53:43 +00:00
|
|
|
- Name that has to be given to the key pair
|
2013-05-23 02:25:05 +00:00
|
|
|
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: ["novaclient"]
|
|
|
|
'''
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Creates 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') }}
|
|
|
|
|
|
|
|
# Creates 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
|
|
|
|
'''
|
2013-05-23 02:25:05 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2013-05-25 03:34:48 +00:00
|
|
|
argument_spec = dict(
|
|
|
|
login_username = dict(default='admin'),
|
|
|
|
login_password = dict(required=True),
|
|
|
|
login_tenant_name = dict(required='True'),
|
|
|
|
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
|
|
|
|
region_name = dict(default=None),
|
|
|
|
name = dict(required=True),
|
|
|
|
public_key = dict(default=None),
|
|
|
|
state = dict(default='present', choices=['absent', 'present'])
|
2013-05-23 02:25:05 +00:00
|
|
|
),
|
|
|
|
)
|
2013-11-23 05:37:47 +00:00
|
|
|
|
|
|
|
nova = nova_client.Client(module.params['login_username'],
|
|
|
|
module.params['login_password'],
|
|
|
|
module.params['login_tenant_name'],
|
|
|
|
module.params['auth_url'],
|
|
|
|
service_type='compute')
|
2013-05-23 02:25:05 +00:00
|
|
|
try:
|
2013-11-23 05:37:47 +00:00
|
|
|
nova.authenticate()
|
2014-02-02 17:33:27 +00:00
|
|
|
except exc.Unauthorized, e:
|
2013-11-23 05:37:47 +00:00
|
|
|
module.fail_json(msg = "Invalid OpenStack Nova credentials.: %s" % e.message)
|
2014-02-02 17:33:27 +00:00
|
|
|
except exc.AuthorizationFailure, e:
|
2013-11-23 05:37:47 +00:00
|
|
|
module.fail_json(msg = "Unable to authorize user: %s" % e.message)
|
|
|
|
|
2013-05-23 02:25:05 +00:00
|
|
|
if module.params['state'] == 'present':
|
2013-05-25 03:34:48 +00:00
|
|
|
for key in nova.keypairs.list():
|
|
|
|
if key.name == module.params['name']:
|
|
|
|
module.exit_json(changed = False, result = "Key present")
|
|
|
|
try:
|
|
|
|
key = nova.keypairs.create(module.params['name'], module.params['public_key'])
|
2014-02-02 17:33:27 +00:00
|
|
|
except Exception, e:
|
2013-05-25 03:34:48 +00:00
|
|
|
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)
|
2013-05-23 02:25:05 +00:00
|
|
|
if module.params['state'] == 'absent':
|
2013-05-25 03:34:48 +00:00
|
|
|
for key in nova.keypairs.list():
|
|
|
|
if key.name == module.params['name']:
|
|
|
|
try:
|
|
|
|
nova.keypairs.delete(module.params['name'])
|
2014-02-02 17:33:27 +00:00
|
|
|
except Exception, e:
|
2013-05-25 03:34:48 +00:00
|
|
|
module.fail_json(msg = "The keypair deletion has failed: %s" % e.message)
|
|
|
|
module.exit_json( changed = True, result = "deleted")
|
2013-05-23 02:25:05 +00:00
|
|
|
module.exit_json(changed = False, result = "not present")
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module.params['common.py
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2013-05-23 02:25:05 +00:00
|
|
|
main()
|
|
|
|
|