allow export to set up a different key to use for the server

If the export instructions list a separate key for the server, use
that key when creating the new instance.

Change-Id: I11d3f24857bbd1017ca6f888a0173d9bff8e2402
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-04-30 22:17:55 +00:00
parent 4e0032f8bb
commit ce5d35840c
4 changed files with 32 additions and 4 deletions

View File

@ -1,7 +1,10 @@
# Resource file for downpour using the instance created in tiny.yml.
servers:
- name: downpour-demo-tiny
# Create the server using a separate key than it was created with in tiny.yml.
key_name: downpour-demo2
keypairs:
- name: downpour-demo
- name: downpour-demo2
images:
- name: cirros-0.3.5-x86_64-disk
- name: cirros-0.3.5-x86_64-disk

View File

@ -3,7 +3,8 @@
connection: local
gather_facts: no
tasks:
# Set up the keypair, saving the values locally.
# Set up the keypair we will use with the server,
# saving the values locally.
- name: Creating key pair downpour-demo
os_keypair:
state: present
@ -23,6 +24,25 @@
dest: "{{ inventory_dir }}/keys/{{ keypair.key.name }}"
mode: 0600
when: "'private_key' in keypair.key" # only save key if we created a new key
- name: Creating key pair downpour-demo2
os_keypair:
state: present
name: downpour-demo2
register: keypair
- name: Create local public key
local_action:
module: copy
content: "{{ keypair.key.public_key }}"
dest: "{{ inventory_dir }}/keys/{{ keypair.key.name }}.pub"
mode: 0600
when: "'private_key' in keypair.key" # only save key if we created a new key
- name: Create local private key
local_action:
module: copy
content: "{{ keypair.key.private_key }}"
dest: "{{ inventory_dir }}/keys/{{ keypair.key.name }}"
mode: 0600
when: "'private_key' in keypair.key" # only save key if we created a new key
# Create a security group and the rules needed for the ports we'll
# be using.

View File

@ -74,7 +74,9 @@ def export_data(cloud, config, args):
for server_info in to_export.servers:
server = cloud.get_server(server_info.name)
tasks.extend(res.server(server, save_state=server_info.save_state))
tasks.extend(res.server(server,
save_state=server_info.save_state,
key_name=server_info.get('key_name')))
playbook = [
# The default playbook is configured to run instructions

View File

@ -182,7 +182,7 @@ class Resolver:
}
yield self._map_uuids('subnet', subnet.name, subnet.id, 'subnet.id')
def server(self, server, save_state):
def server(self, server, save_state, key_name=None):
# The ssh key needed to login to the server.
keypair = self.cloud.get_keypair(server.key_name)
yield from self.keypair(keypair)
@ -215,6 +215,9 @@ class Resolver:
'nics': list(server.networks.keys()),
# 'image': image.name if image else server.image.id,
}
key_name = key_name or server.key_name
if key_name:
server_data['key_name'] = key_name
if vol_names:
server_data['volumes'] = vol_names
yield {