Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
ansible_adanced_oci [2022/01/30 21:33] – andonovj | ansible_adanced_oci [2024/08/19 13:03] (current) – andonovj | ||
---|---|---|---|
Line 483: | Line 483: | ||
</ | </ | ||
- | That playbook, will | + | That playbook, will create a block device, attach it and also attach it on the host using the following playbook: |
+ | |||
+ | < | ||
+ | --- | ||
+ | |||
+ | - name: Attach the device | ||
+ | vars: | ||
+ | ansible_user: | ||
+ | ansible_become: | ||
+ | ansible_become_method: | ||
+ | |||
+ | hosts: '{{ instance_ip }}' | ||
+ | tasks: | ||
+ | - block: | ||
+ | - name: Execute commands | ||
+ | ansible.builtin.command: | ||
+ | loop: '{{ bash_commands }}' | ||
+ | </ | ||
+ | |||
+ | With all that, we can finally configure the refresh (resnap) and configure the replication: | ||
+ | |||
+ | |||
+ | ====Resnap the primary block device==== | ||
+ | < | ||
+ | --- | ||
+ | |||
+ | - hosts: localhost | ||
+ | collections: | ||
+ | - oracle.oci | ||
+ | vars_files: | ||
+ | - / | ||
+ | |||
+ | tasks: | ||
+ | - block: | ||
+ | - name: Get Replica instance details | ||
+ | oci_compute_instance_facts: | ||
+ | compartment_id: | ||
+ | lifecycle_state: | ||
+ | display_name: | ||
+ | register: replica_details | ||
+ | |||
+ | - name: Save Replica info | ||
+ | set_fact: | ||
+ | instance: '{{ replica_details[" | ||
+ | replica_ip: ' | ||
+ | |||
+ | - name: Add replica instance(s) to in-memory inventory | ||
+ | add_host: | ||
+ | name: " | ||
+ | ansible_host: | ||
+ | |||
+ | - name: Get attached devices on Replica | ||
+ | oci_compute_volume_attachment_facts: | ||
+ | compartment_id: | ||
+ | instance_id: | ||
+ | register: replica_volume_attachment_facts | ||
+ | |||
+ | - import_playbook: | ||
+ | vars: | ||
+ | instance_ip: | ||
+ | |||
+ | - name: Create iSCSI attachement | ||
+ | hosts: localhost | ||
+ | collections: | ||
+ | - oracle.oci | ||
+ | vars_files: | ||
+ | - / | ||
+ | tasks: | ||
+ | - block: | ||
+ | - name: Get Primary instance details | ||
+ | oci_compute_instance_facts: | ||
+ | compartment_id: | ||
+ | lifecycle_state: | ||
+ | display_name: | ||
+ | register: primary_details | ||
+ | |||
+ | - name: Save primary info | ||
+ | set_fact: | ||
+ | instance: '{{ primary_details[" | ||
+ | primary_ip: ' | ||
+ | primary_prv_ip: | ||
+ | |||
+ | - name: Add primary instance(s) to in-memory inventory | ||
+ | add_host: | ||
+ | name: '{{ item }}' | ||
+ | loop: '{{ primary_ip }}' | ||
+ | |||
+ | - name: Get attached devices on Primary | ||
+ | oci_compute_volume_attachment_facts: | ||
+ | compartment_id: | ||
+ | instance_id: | ||
+ | register: primary_volume_attachment_facts | ||
+ | |||
+ | - name: Get all devices on Replica | ||
+ | oci_compute_volume_attachment_facts: | ||
+ | compartment_id: | ||
+ | instance_id: | ||
+ | register: replica_volume_attachment_facts | ||
+ | |||
+ | - name: Set fact, attached device on the replica | ||
+ | set_fact: | ||
+ | replica_attached_blockdevice_id: | ||
+ | replica_attached_volume_id: | ||
+ | |||
+ | - name: Detach block level attachement device from Clone | ||
+ | oci_compute_volume_attachment: | ||
+ | volume_attachment_id: | ||
+ | state: absent | ||
+ | when: replica_attached_blockdevice_id|length != 0 | ||
+ | |||
+ | - name: Delete the block volume | ||
+ | oci_blockstorage_volume: | ||
+ | volume_id: '{{ replica_attached_volume_id[0] }}' | ||
+ | state: absent | ||
+ | when: replica_attached_volume_id|length != 0 | ||
+ | |||
+ | - name: Create a clone data from another block volume in the same availability domain | ||
+ | oci_blockstorage_volume: | ||
+ | name: '{{ REPLICA }}-bv-clone' | ||
+ | source_details: | ||
+ | id: '{{ primary_volume_attachment_facts.volume_attachments[0].volume_id }}' | ||
+ | type: ' | ||
+ | availability_domain: | ||
+ | compartment_id: | ||
+ | size_in_gbs: | ||
+ | register: created_clone_block_volume | ||
+ | |||
+ | - name: Attach the clone to the replica | ||
+ | oci_compute_volume_attachment: | ||
+ | compartment_id: | ||
+ | instance_id: | ||
+ | type: service_determined | ||
+ | volume_id: "{{ created_clone_block_volume.volume.id }}" | ||
+ | register: attached_block_device | ||
+ | |||
+ | - name: Save attachement commands | ||
+ | set_fact: | ||
+ | iscsi_attach_commands: | ||
+ | |||
+ | - import_playbook: | ||
+ | vars: | ||
+ | instance_ip: | ||
+ | bash_commands: | ||
+ | |||
+ | - import_playbook: | ||
+ | vars: | ||
+ | instance_ip: | ||
+ | primary_host: | ||
+ | primary_prv_ip: | ||
+ | primary_password: | ||
+ | </ | ||
+ | |||
+ | |||
+ | That playbook will use our final playbook: " | ||
+ | |||
+ | ====Setup replication==== | ||
+ | <Code: bash|Setup replication> | ||
+ | --- | ||
+ | |||
+ | - name: Detach the device | ||
+ | vars: | ||
+ | ansible_user: | ||
+ | ansible_become: | ||
+ | ansible_become_method: | ||
+ | vars_files: | ||
+ | - / | ||
+ | |||
+ | hosts: '{{ instance_ip }}' | ||
+ | tasks: | ||
+ | - name: Stop Firewall | ||
+ | service: name=firewalld state=stopped enabled=false | ||
+ | |||
+ | - name: Get file coordinate | ||
+ | shell: | | ||
+ | cat / | ||
+ | register: replication_file | ||
+ | |||
+ | - name: Get position coordinate | ||
+ | shell: | | ||
+ | wc {{ replication_file.stdout }} | awk -F " " ' | ||
+ | register: replication_position | ||
+ | |||
+ | - name: Change the UUID of MySQL | ||
+ | shell: | | ||
+ | newUUID=`uuidgen` | ||
+ | echo " | ||
+ | [auto] | ||
+ | server-uuid=${newUUID}" | ||
+ | |||
+ | - name: Start MySQL | ||
+ | service: name=mysqld state=started enabled=true | ||
+ | |||
+ | - name: Change primary on Replica | ||
+ | community.mysql.mysql_replication: | ||
+ | mode: changeprimary | ||
+ | primary_host: | ||
+ | primary_log_file: | ||
+ | primary_log_pos: | ||
+ | login_unix_socket: | ||
+ | login_user: root | ||
+ | login_password: | ||
+ | primary_user: | ||
+ | primary_password: | ||
+ | |||
+ | - name: Start replica | ||
+ | community.mysql.mysql_replication: | ||
+ | mode: startreplica | ||
+ | login_unix_socket: | ||
+ | login_user: root | ||
+ | login_password: | ||
+ | </ | ||
+ | |||
+ | With this, we were able to setup replication using OCI and ansible. The resnap can be done as many time as needed. It will always clone the primary' | ||
+ | |||
+ | There is one more playbook which we need to add. | ||
+ | |||
+ | < | ||
+ | --- | ||
+ | |||
+ | - name: Detach the device | ||
+ | vars: | ||
+ | ansible_user: | ||
+ | ansible_become: | ||
+ | ansible_become_method: | ||
+ | |||
+ | hosts: '{{ instance_ip }}' | ||
+ | tasks: | ||
+ | - name: Find the IQN | ||
+ | shell: | | ||
+ | iscsiadm --mode session | ||
+ | register: iqn | ||
+ | |||
+ | - name: Stop MySQL | ||
+ | service: name=mysqld state=stopped enabled=false | ||
+ | when: iqn.stdout != "" | ||
+ | |||
+ | - name: Find possible processes holding the file system | ||
+ | shell: | | ||
+ | lsof +D / | ||
+ | register: locking_processes | ||
+ | ignore_errors: | ||
+ | |||
+ | - name: Print locking process | ||
+ | debug: | ||
+ | msg: '{{ locking_processes }}' | ||
+ | when: locking_processes.stdout_lines|length != 0 | ||
+ | |||
+ | - name: Kill and unmount the file system | ||
+ | shell: | | ||
+ | kill -9 '{{ item }}' | ||
+ | with_items: '{{ locking_processes.stdout_lines }}' | ||
+ | when: locking_processes.stdout_lines|length != 0 | ||
+ | |||
+ | - name: Print IQN | ||
+ | debug: | ||
+ | msg: ' | ||
+ | |||
+ | - name: Unmount File system | ||
+ | mount: | ||
+ | path: / | ||
+ | state: unmounted | ||
+ | |||
+ | - name: Detach the iSCSI | ||
+ | community.general.open_iscsi: | ||
+ | login: no | ||
+ | target: ' | ||
+ | when: iqn.stdout != "" | ||
+ | </ | ||
+ | |||
+ | That playbook will stop the MySQL and detach the iSCSI from the compute node, so we can safely, remove it from the OCI as well. | ||
+ | |||
+ | =====MySQL Clusterset Automation===== | ||
+ | This ansible project consists of 3 roles: | ||
+ | - MySQL Instance | ||
+ | - MySQL InnoDB Cluster | ||
+ | - MySQL Router | ||
+ | |||
+ | That will create architecture with 1 InnoDB Clusterset and a replicaset attached to it. The full code, you can see here: | ||
+ | |||
+ | https:// | ||
+ |