Skip to content

Commit

Permalink
Update upon review
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Oct 5, 2023
1 parent a0f7594 commit 72ef7a0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions playbooks/move_vm_from_on_prem_to_aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
10 changes: 5 additions & 5 deletions roles/clone_on_prem_vm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions roles/clone_on_prem_vm/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions roles/import_image_and_run_aws_instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
33 changes: 19 additions & 14 deletions roles/import_image_and_run_aws_instance/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }}"
Expand Down

0 comments on commit 72ef7a0

Please sign in to comment.