From 72ef7a0c55210520fdecfcea8faebc1b7791e35e Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Thu, 5 Oct 2023 16:14:10 +0200 Subject: [PATCH] Update upon review Signed-off-by: Alina Buzachis --- .../move_vm_from_on_prem_to_aws/README.md | 4 +-- .../move_vm_from_on_prem_to_aws.yml | 2 +- roles/clone_on_prem_vm/README.md | 10 +++--- roles/clone_on_prem_vm/tasks/main.yml | 6 ++-- .../README.md | 4 +-- .../tasks/main.yml | 33 +++++++++++-------- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/playbooks/move_vm_from_on_prem_to_aws/README.md b/playbooks/move_vm_from_on_prem_to_aws/README.md index 350d506f..44f7105d 100644 --- a/playbooks/move_vm_from_on_prem_to_aws/README.md +++ b/playbooks/move_vm_from_on_prem_to_aws/README.md @@ -45,9 +45,9 @@ AWS User Account with the following permissions: ### Needed for the cloud.aws_ops.clone_on_prem_vm role * **clone_on_prem_vm_source_vm_name** (str): (Required) The name of the on-prem VM you want to clone. -* **clone_on_prem_vm_image_name** (str): (Optional) The name you want to call the cloned image. If not set, the **on_prem_vm_clone_name** will be used with a _-clone_ suffix. +* **clone_on_prem_vm_image_name** (str): (Optional) The name you want to call the cloned image. If not set, the **clone_on_prem_vm_source_vm_name** will be used with a _-clone_ suffix. * **clone_on_prem_vm_uri** (str): (Optional) Libvirt connection uri. Default: "qemu:///system". -* **clone_on_prem_vm_overwrite** (bool): (Optional) Weather to overwrite or not an already existing on prem VM clone. Default: true. +* **clone_on_prem_vm_overwrite** (bool): (Optional) Whether to overwrite or not an already existing on prem VM clone. Default: true. * **clone_on_prem_vm_local_image_path** (str): (Optional) The path where you would like to save the image. If the path does not exists on localhost, the role will create it. If this parameter is not set, the role will save the image in a _~/tmp_ folder. ### Needed for the cloud.aws_ops.import_image_and_run_aws_instance role diff --git a/playbooks/move_vm_from_on_prem_to_aws/move_vm_from_on_prem_to_aws.yml b/playbooks/move_vm_from_on_prem_to_aws/move_vm_from_on_prem_to_aws.yml index afff7f71..9768840c 100644 --- a/playbooks/move_vm_from_on_prem_to_aws/move_vm_from_on_prem_to_aws.yml +++ b/playbooks/move_vm_from_on_prem_to_aws/move_vm_from_on_prem_to_aws.yml @@ -26,7 +26,7 @@ name: cloud.aws_ops.clone_on_prem_vm vars: clone_on_prem_vm_source_vm_name: "{{ clone_on_prem_vm_source_vm_name }}" - clone_on_prem_vm_dest_vm_name: "{{ clone_on_prem_vm_dest_vm_name }}" + clone_on_prem_vm_image_name: "{{ clone_on_prem_vm_image_name }}" clone_on_prem_vm_uri: "{{ clone_on_prem_vm_uri }}" clone_on_prem_vm_local_image_path: "{{ clone_on_prem_vm_local_image_path }}" clone_on_prem_vm_overwrite: "{{ clone_on_prem_vm_overwrite }}" diff --git a/roles/clone_on_prem_vm/README.md b/roles/clone_on_prem_vm/README.md index ed99eedc..988a5d90 100644 --- a/roles/clone_on_prem_vm/README.md +++ b/roles/clone_on_prem_vm/README.md @@ -12,10 +12,10 @@ Role Variables -------------- * **clone_on_prem_vm_source_vm_name**: (Required) The name of the on-prem VM you want to clone. -* **clone_on_prem_vm_image_name**: The name you want to call the cloned image. If not set, the **clone_on_prem_vm_source_vm_name** will be used with a _-clone_ suffix. -* **clone_on_prem_vm_overwrite**: Weather to overwrite or not an already existing on prem VM clone. Default: true. -* **clone_on_prem_vm_local_image_path**: The path where you would like to save the image. If the path does not exists on localhost, the role will create it. If this parameter is not set, the role will save the image in a _~/tmp_ folder. -* **clone_on_prem_vm_uri**: Libvirt connection uri. Default: "qemu:///system". +* **clone_on_prem_vm_image_name**: (Optional) The name you want to call the cloned image. If not set, the **clone_on_prem_vm_source_vm_name** will be used with a _-clone_ suffix. +* **clone_on_prem_vm_overwrite**: (Optional) Whether to overwrite or not an already existing on prem VM clone. Default: true. +* **clone_on_prem_vm_local_image_path**: (Optional) The path where you would like to save the image. If the path does not exists on localhost, the role will create it. If this parameter is not set, the role will save the image in a _~/tmp_ folder. +* **clone_on_prem_vm_uri**: (Optional) Libvirt connection uri. Default: "qemu:///system". Dependencies ------------ @@ -52,7 +52,7 @@ Example Playbook name: cloud.aws_ops.clone_on_prem_vm vars: clone_on_prem_vm_source_vm_name: "{{ on_prem_source_vm_name }}" - clone_on_prem_vm_dest_image_name: "{{ on_prem_vm_image_name }}" + clone_on_prem_vm_image_name: "{{ on_prem_vm_image_name }}" clone_on_prem_vm_local_image_path: "{{ local_image_path }}" delegate_to: kvm diff --git a/roles/clone_on_prem_vm/tasks/main.yml b/roles/clone_on_prem_vm/tasks/main.yml index 6489982c..d5d7ac5e 100644 --- a/roles/clone_on_prem_vm/tasks/main.yml +++ b/roles/clone_on_prem_vm/tasks/main.yml @@ -71,8 +71,8 @@ - name: Set 'clone_on_prem_vm__clone_path' and 'clone_on_prem_vm__raw_image_path' ansible.builtin.set_fact: - clone_on_prem_vm__clone_path: "{{ clone_on_prem_vm__tmpdir.path }}/{{ clone_on_prem_vm_dest_vm_name }}.qcow2" - clone_on_prem_vm__raw_image_path: "{{ clone_on_prem_vm__tmpdir.path }}/{{ clone_on_prem_vm_dest_vm_name }}.raw" + clone_on_prem_vm__clone_path: "{{ clone_on_prem_vm__tmpdir.path }}/{{ clone_on_prem_vm_image_name }}.qcow2" + clone_on_prem_vm__raw_image_path: "{{ clone_on_prem_vm__tmpdir.path }}/{{ clone_on_prem_vm_image_name }}.raw" - name: Cloning {{ clone_on_prem_vm_source_vm_name }} on prem VM ansible.builtin.command: | @@ -87,7 +87,7 @@ path: "{{ clone_on_prem_vm__clone_path }}" register: clone_on_prem_vm__clone_info -# Priviledge escalation is needed because the .qcow2 file is owned by root +# Privilege escalation is needed because the .qcow2 file is owned by root # when default hypervisor is used - name: Convert qcow2 to raw using qemu-img with priviledge escalation ansible.builtin.command: | diff --git a/roles/import_image_and_run_aws_instance/README.md b/roles/import_image_and_run_aws_instance/README.md index 0dcf0735..638e4c83 100644 --- a/roles/import_image_and_run_aws_instance/README.md +++ b/roles/import_image_and_run_aws_instance/README.md @@ -58,7 +58,7 @@ Dependencies Example Playbook ---------------- -This role can be used together with the [cloud.aws_ops.clone_on_prem_vm](../clone_on_prem_vm/README.md) role as shown below. If you wish to use it separately, just ensure you set the **clone_on_prem_vm_local_image_path** parameter. +This role can be used together with the [cloud.aws_ops.clone_on_prem_vm](../clone_on_prem_vm/README.md) role as shown below. - hosts: localhost gather_facts: false @@ -91,7 +91,7 @@ This role can be used together with the [cloud.aws_ops.clone_on_prem_vm](../clon name: cloud.aws_ops.clone_on_prem_vm vars: clone_on_prem_vm_source_vm_name: "{{ on_prem_source_vm_name }}" - clone_on_prem_vm_dest_image_name: "{{ on_prem_vm_image_name }}" + clone_on_prem_vm_image_name: "{{ on_prem_vm_image_name }}" clone_on_prem_vm_local_image_path: "{{ local_image_path }}" delegate_to: kvm diff --git a/roles/import_image_and_run_aws_instance/tasks/main.yml b/roles/import_image_and_run_aws_instance/tasks/main.yml index 0557c9b6..8d1f9655 100644 --- a/roles/import_image_and_run_aws_instance/tasks/main.yml +++ b/roles/import_image_and_run_aws_instance/tasks/main.yml @@ -19,16 +19,33 @@ msg: The name to assign to the AWS EC2 instance must be defined as import_image_and_run_aws_instance_instance_name when: import_image_and_run_aws_instance_instance_name is undefined + - name: Fail when 'import_image_and_run_aws_instance_import_image_task_name' is undefined + ansible.builtin.fail: + msg: The name of the EC2 import image task must be defined as import_image_and_run_aws_instance_import_image_task_name + when: import_image_and_run_aws_instance_import_image_task_name is undefined + - name: Get information about the S3 bucket community.aws.s3_bucket_info: name: "{{ import_image_and_run_aws_instance_bucket_name }}" register: import_image_and_run_aws_instance__bucket_info - - name: Fail when S3 bucket name does not exist + - name: Fail when S3 bucket does not exist ansible.builtin.fail: - msg: "The on prem VM {{ import_image_and_run_aws_instance_bucket_name }} does not exist." + msg: "The S3 bucket {{ import_image_and_run_aws_instance_bucket_name }} does not exist." when: import_image_and_run_aws_instance__bucket_info.buckets | length == 0 + - name: Check if an instance with the specified name already exists + amazon.aws.ec2_instance_info: + filters: + "tag:Name": "{{ import_image_and_run_aws_instance_instance_name }}" + instance-state-name: [ "running", "pending", "shutting-down", "stopping", "stopped"] + register: import_image_and_run_aws_instance__ec2_instance_info + + - name: Fail when an instance with the specified name already exists + ansible.builtin.fail: + msg: "An EC2 instance with name {{ import_image_and_run_aws_instance_instance_name }} already exists" + when: import_image_and_run_aws_instance__ec2_instance_info.instances | length == 1 + - name: Set 'import_image_and_run_aws_instance__s3_object_key' variable ansible.builtin.set_fact: import_image_and_run_aws_instance__s3_object_key: "{{ import_image_and_run_aws_instance_bucket_name }}/{{ import_image_and_run_aws_instance_instance_name }}.raw" @@ -69,18 +86,6 @@ import_image_and_run_aws_instance__ami_id: "{{ import_image_and_run_aws_instance__import_image_info.import_image[0].image_id }}" import_image_and_run_aws_instance__snapshot_id: "{{ import_image_and_run_aws_instance__import_image_info.import_image[0].snapshot_details[0].snapshot_id }}" - - name: Check if an instance with the specified name alrerady exists - amazon.aws.ec2_instance_info: - filters: - "tag:Name": "{{ import_image_and_run_aws_instance_instance_name }}" - instance-state-name: [ "running", "pending", "shutting-down", "stopping", "stopped"] - register: import_image_and_run_aws_instance__ec2_instance_info - - - name: Fail when an instance with the specified name already exists - ansible.builtin.fail: - msg: "An EC2 instance with name {{ import_image_and_run_aws_instance_instance_name }} already exists" - when: import_image_and_run_aws_instance__ec2_instance_info.instances | length == 1 - - name: Start EC2 instance amazon.aws.ec2_instance: name: "{{ import_image_and_run_aws_instance_instance_name }}"