From 0f933d768add6eb6008d12241ca9873efa1851d7 Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Tue, 19 Jul 2016 17:24:18 +0000 Subject: [PATCH] Fix creation of servers with on-the-fly keypairs On 301ae3faee3f68da36faaa628e7c2a45af33fee4 refactor, I overlooked the creation of servers with the generate_keypair flag enabled. This change adds it back. Change-Id: I891eba482db38e0b4f97222e37c9bd2e214571d1 --- tasks/create_server.yml | 5 +- tasks/create_servers.yml | 168 ------------------------------------- tasks/generate_keypair.yml | 18 ++++ 3 files changed, 22 insertions(+), 169 deletions(-) delete mode 100644 tasks/create_servers.yml create mode 100644 tasks/generate_keypair.yml diff --git a/tasks/create_server.yml b/tasks/create_server.yml index 5aee22b..8264b5e 100644 --- a/tasks/create_server.yml +++ b/tasks/create_server.yml @@ -1,4 +1,7 @@ --- +- include: generate_keypair.yml + when: generate_keypair|bool + - os_server: cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" state: "{{ item_server.state|default(omit) }}" @@ -15,7 +18,7 @@ floating_ips: "{{ item_server.floating_ips|default(omit) }}" image_exclude: "{{ item_server.image_exclude|default(omit) }}" key: "{{ item_server.key|default(omit) }}" - key_name: "{{ item_server.key_name|default(omit) }}" + key_name: "{{ cl_generated_keypair_name|default(item_server.key_name)|default(omit) }}" meta: "{{ item_server.meta|default(omit) }}" network: "{{ item_server.network|default(omit) }}" nics: "{{ item_server.nics|default(omit) }}" diff --git a/tasks/create_servers.yml b/tasks/create_servers.yml deleted file mode 100644 index d79b601..0000000 --- a/tasks/create_servers.yml +++ /dev/null @@ -1,168 +0,0 @@ ---- -- block: - - name: Generate keypair name - set_fact: cl_generated_keypair_name=launch-{{ lookup('pipe', "date +'%s'") }} - - name: Generate SSH keys - shell: ssh-keygen -t "{{ generate_keypair_key_type }}" -b "{{ generate_keypair_key_bits }}" -N '' -f "{{ generate_keypair_folder }}"/"{{ cl_generated_keypair_name }}" - - name: Store public key filename path - set_fact: cl_generated_keypair_public_key_filename={{ generate_keypair_folder}}/{{ cl_generated_keypair_name }}.pub - - name: Store private key filename path - set_fact: cl_generated_keypair_private_key_filename={{ generate_keypair_folder}}/{{ cl_generated_keypair_name }} - - - name: Create OpenStack keypair {{ cl_generated_keypair_name }} - os_keypair: - cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" - state: present - name: "{{ cl_generated_keypair_name }}" - public_key_file: "{{ cl_generated_keypair_public_key_filename }}" - - - name: Create per-cloud servers for {{ item_cloud.name }} - os_server: - cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" - state: "{{ item.state|default(omit) }}" - name: "{{ item.name }}" - image: "{{ item.image }}" - auto_ip: "{{ item.auto_ip|default(omit) }}" - boot_from_volume: "{{ item.boot_from_volume|default(omit) }}" - boot_volume: "{{ item.boot_volume|default(omit) }}" - config_drive: "{{ item.config_drive|default(omit) }}" - flavor: "{{ item.flavor|default(omit) }}" - flavor_include: "{{ item.flavor_include|default(omit) }}" - flavor_ram: "{{ item.flavor_ram|default(omit) }}" - floating_ip_pools: "{{ item.floating_ip_pools|default(omit) }}" - floating_ips: "{{ item.floating_ips|default(omit) }}" - image_exclude: "{{ item.image_exclude|default(omit) }}" - key: "{{ item.key|default(omit) }}" - key_name: "{{ cl_generated_keypair_name }}" - meta: "{{ item.meta|default(omit) }}" - network: "{{ item.network|default(omit) }}" - nics: "{{ item.nics|default(omit) }}" - scheduler_hints: "{{ item.scheduler_hints|default(omit) }}" - security_groups: "{{ item.security_groups|default(omit) }}" - terminate_volume: "{{ item.terminate_volume|default(omit) }}" - userdata: "{{ item.userdata|default(omit) }}" - volume_size: "{{ item.volume_size|default(omit) }}" - volumes: "{{ item.volumes|default(omit) }}" - with_items: "{{ item_cloud.servers|default([]) }}" - tags: - - servers - register: os_server_result - - - add_host: name={{ item.server.name }} groups=cl_servers_just_created ansible_ssh_host={{ item.server.public_v4 }} - with_items: "{{ os_server_result.results }}" - - - name: Create profiles servers for {{ item_cloud.name }} - os_server: - cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" - state: "{{ item.1.state|default(omit) }}" - name: "{{ item.1.name }}" - image: "{{ item.1.image }}" - auto_ip: "{{ item.1.auto_ip|default(omit) }}" - boot_from_volume: "{{ item.1.boot_from_volume|default(omit) }}" - boot_volume: "{{ item.1.boot_volume|default(omit) }}" - config_drive: "{{ item.1.config_drive|default(omit) }}" - flavor: "{{ item.1.flavor|default(omit) }}" - flavor_include: "{{ item.1.flavor_include|default(omit) }}" - flavor_ram: "{{ item.1.flavor_ram|default(omit) }}" - floating_ip_pools: "{{ item.1.floating_ip_pools|default(omit) }}" - floating_ips: "{{ item.1.floating_ips|default(omit) }}" - image_exclude: "{{ item.1.image_exclude|default(omit) }}" - key: "{{ item.1.key|default(omit) }}" - key_name: "{{ cl_generated_keypair_name }}" - meta: "{{ item.1.meta|default(omit) }}" - network: "{{ item.1.network|default(omit) }}" - nics: "{{ item.1.nics|default(omit) }}" - scheduler_hints: "{{ item.1.scheduler_hints|default(omit) }}" - security_groups: "{{ item.1.security_groups|default(omit) }}" - terminate_volume: "{{ item.1.terminate_volume|default(omit) }}" - userdata: "{{ item.1.userdata|default(omit) }}" - volume_size: "{{ item.1.volume_size|default(omit) }}" - volumes: "{{ item.1.volumes|default(omit) }}" - with_subelements: - - "{{ profiles|default([]) }}" - - servers - - { skip_missing: yes } - when: "{{ item_cloud.profiles is defined and item.0.name in item_cloud.profiles }}" - tags: - - servers - register: os_server_result - - - add_host: name={{ item.server.name }} groups=cl_servers_just_created ansible_ssh_host={{ item.server.public_v4 }} - with_items: "{{ os_server_result.results }}" - when: generate_keypair|bool - -- block: - - name: Create per-cloud servers for {{ item_cloud.name }} - os_server: - cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" - state: "{{ item.state|default(omit) }}" - name: "{{ item.name }}" - image: "{{ item.image }}" - auto_ip: "{{ item.auto_ip|default(omit) }}" - boot_from_volume: "{{ item.boot_from_volume|default(omit) }}" - boot_volume: "{{ item.boot_volume|default(omit) }}" - config_drive: "{{ item.config_drive|default(omit) }}" - flavor: "{{ item.flavor|default(omit) }}" - flavor_include: "{{ item.flavor_include|default(omit) }}" - flavor_ram: "{{ item.flavor_ram|default(omit) }}" - floating_ip_pools: "{{ item.floating_ip_pools|default(omit) }}" - floating_ips: "{{ item.floating_ips|default(omit) }}" - image_exclude: "{{ item.image_exclude|default(omit) }}" - key: "{{ item.key|default(omit) }}" - key_name: "{{ item.key_name|default(omit) }}" - meta: "{{ item.meta|default(omit) }}" - network: "{{ item.network|default(omit) }}" - nics: "{{ item.nics|default(omit) }}" - scheduler_hints: "{{ item.scheduler_hints|default(omit) }}" - security_groups: "{{ item.security_groups|default(omit) }}" - terminate_volume: "{{ item.terminate_volume|default(omit) }}" - userdata: "{{ item.userdata|default(omit) }}" - volume_size: "{{ item.volume_size|default(omit) }}" - volumes: "{{ item.volumes|default(omit) }}" - with_items: "{{ item_cloud.servers|default([]) }}" - tags: - - servers - register: os_server_result - - - add_host: name={{ item.server.name }} groups=cl_servers_just_created ansible_ssh_host={{ item.server.public_v4 }} - with_items: "{{ os_server_result.results }}" - - - name: Create profiles servers for {{ item_cloud.name }} - os_server: - cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" - state: "{{ item.1.state|default(omit) }}" - name: "{{ item.1.name }}" - image: "{{ item.1.image }}" - auto_ip: "{{ item.1.auto_ip|default(omit) }}" - boot_from_volume: "{{ item.1.boot_from_volume|default(omit) }}" - boot_volume: "{{ item.1.boot_volume|default(omit) }}" - config_drive: "{{ item.1.config_drive|default(omit) }}" - flavor: "{{ item.1.flavor|default(omit) }}" - flavor_include: "{{ item.1.flavor_include|default(omit) }}" - flavor_ram: "{{ item.1.flavor_ram|default(omit) }}" - floating_ip_pools: "{{ item.1.floating_ip_pools|default(omit) }}" - floating_ips: "{{ item.1.floating_ips|default(omit) }}" - image_exclude: "{{ item.1.image_exclude|default(omit) }}" - key: "{{ item.1.key|default(omit) }}" - key_name: "{{ item.1.key_name|default(omit) }}" - meta: "{{ item.1.meta|default(omit) }}" - network: "{{ item.1.network|default(omit) }}" - nics: "{{ item.1.nics|default(omit) }}" - scheduler_hints: "{{ item.1.scheduler_hints|default(omit) }}" - security_groups: "{{ item.1.security_groups|default(omit) }}" - terminate_volume: "{{ item.1.terminate_volume|default(omit) }}" - userdata: "{{ item.1.userdata|default(omit) }}" - volume_size: "{{ item.1.volume_size|default(omit) }}" - volumes: "{{ item.1.volumes|default(omit) }}" - with_subelements: - - "{{ profiles|default([]) }}" - - servers - - { skip_missing: yes } - when: "{{ item_cloud.profiles is defined and item.0.name in item_cloud.profiles }}" - tags: - - servers - register: os_server_result - - - add_host: name={{ item.server.name }} groups=cl_servers_just_created ansible_ssh_host={{ item.server.public_v4 }} - with_items: "{{ os_server_result.results }}" - when: not generate_keypair|bool diff --git a/tasks/generate_keypair.yml b/tasks/generate_keypair.yml new file mode 100644 index 0000000..7291916 --- /dev/null +++ b/tasks/generate_keypair.yml @@ -0,0 +1,18 @@ +--- +- block: + - name: Generate keypair name + set_fact: cl_generated_keypair_name=launch-{{ lookup('pipe', "date +'%s'") }} + - name: Generate SSH keys + shell: ssh-keygen -t "{{ generate_keypair_key_type }}" -b "{{ generate_keypair_key_bits }}" -N '' -f "{{ generate_keypair_folder }}"/"{{ cl_generated_keypair_name }}" + - name: Store public key filename path + set_fact: cl_generated_keypair_public_key_filename={{ generate_keypair_folder}}/{{ cl_generated_keypair_name }}.pub + - name: Store private key filename path + set_fact: cl_generated_keypair_private_key_filename={{ generate_keypair_folder}}/{{ cl_generated_keypair_name }} + + - name: Create OpenStack keypair {{ cl_generated_keypair_name }} + os_keypair: + cloud: "{{ item_cloud.oscc_cloud|default(item_cloud.name) }}" + state: present + name: "{{ cl_generated_keypair_name }}" + public_key_file: "{{ cl_generated_keypair_public_key_filename }}" + when: generate_keypair|bool