commit 1301d3f726a3e3facea7987617bef33832a1a2e9 Author: thienqb123456 Date: Thu Dec 12 11:53:34 2024 +0700 Thienvv - first commit - ansible diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..c83c2ba --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,3 @@ +[defaults] +inventory = ./inventory +roles_path = ./roles \ No newline at end of file diff --git a/inventory/host_vars/prod_be_acp.yml b/inventory/host_vars/prod_be_acp.yml new file mode 100644 index 0000000..73da33a --- /dev/null +++ b/inventory/host_vars/prod_be_acp.yml @@ -0,0 +1,4 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "site_test" +blue_path: "C:\\deploy\\vpress\\acp\\be\\blue" +green_path: "C:\\deploy\\vpress\\acp\\be\\green" \ No newline at end of file diff --git a/inventory/host_vars/prod_fe_acp.yml b/inventory/host_vars/prod_fe_acp.yml new file mode 100644 index 0000000..22ec3c7 --- /dev/null +++ b/inventory/host_vars/prod_fe_acp.yml @@ -0,0 +1,4 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "xx" +blue_path: "C:\\deploy\\vpress\\acp\\be\\blue" +green_path: "C:\\deploy\\vpress\\acp\\be\\green" \ No newline at end of file diff --git a/inventory/host_vars/uat_be_acp.yml b/inventory/host_vars/uat_be_acp.yml new file mode 100644 index 0000000..5b8e711 --- /dev/null +++ b/inventory/host_vars/uat_be_acp.yml @@ -0,0 +1,4 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "api-acp-2025" +blue_path: "C:\\deploy\\vpress\\acp\\be\\blue" +green_path: "C:\\deploy\\vpress\\acp\\be\\green" \ No newline at end of file diff --git a/inventory/host_vars/uat_be_portal.yml b/inventory/host_vars/uat_be_portal.yml new file mode 100644 index 0000000..1046b2e --- /dev/null +++ b/inventory/host_vars/uat_be_portal.yml @@ -0,0 +1,4 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "api-portal-2025" +blue_path: "C:\\deploy\\vpress\\portal\\be\\blue" +green_path: "C:\\deploy\\vpress\\portal\\be\\green" \ No newline at end of file diff --git a/inventory/host_vars/uat_fe_acp.yml b/inventory/host_vars/uat_fe_acp.yml new file mode 100644 index 0000000..293a6c6 --- /dev/null +++ b/inventory/host_vars/uat_fe_acp.yml @@ -0,0 +1,4 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "uat-acp.vpress.vn" +blue_path: "C:\\deploy\\vpress\\acp\\fe\\blue" +green_path: "C:\\deploy\\vpress\\acp\\fe\\green" \ No newline at end of file diff --git a/inventory/host_vars/uat_fe_portal.yml b/inventory/host_vars/uat_fe_portal.yml new file mode 100644 index 0000000..e1a0f01 --- /dev/null +++ b/inventory/host_vars/uat_fe_portal.yml @@ -0,0 +1,4 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "uat_be_acp_TEST" +blue_path: "C:\\deploy\\vpress\\portal\\fe\\blue" +green_path: "C:\\deploy\\vpress\\portal\\fe\\green" \ No newline at end of file diff --git a/inventory/prod.ini b/inventory/prod.ini new file mode 100644 index 0000000..84a7e11 --- /dev/null +++ b/inventory/prod.ini @@ -0,0 +1,11 @@ +[prod_be_acp_webservers] +prod_be_acp ansible_host=123.30.186.68 ansible_user=jenkins_cicd_ktdt ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore + +[prod_be_portal_webservers] +prod_be_portal ansible_host=123.30.186.68 ansible_user=jenkins_cicd_ktdt ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore + +[prod_fe_acp_webservers] +prod_fe_acp ansible_host=123.30.186.68 ansible_user=jenkins_cicd_ktdt ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore + +[prod_be_portal_webservers] +prod_fe_portal ansible_host=123.30.186.68 ansible_user=jenkins_cicd_ktdt ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore diff --git a/inventory/uat.ini b/inventory/uat.ini new file mode 100644 index 0000000..fdef4b1 --- /dev/null +++ b/inventory/uat.ini @@ -0,0 +1,11 @@ +[uat_be_acp_webservers] +uat_be_acp ansible_host=123.30.172.34 ansible_user=jenkins_cicd ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore + +[uat_be_portal_webservers] +uat_be_portal ansible_host=123.30.172.34 ansible_user=jenkins_cicd ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore + +[uat_fe_acp_webservers] +uat_fe_acp ansible_host=123.30.172.34 ansible_user=jenkins_cicd ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore + +[uat_be_portal_webservers] +uat_fe_portal ansible_host=123.30.172.34 ansible_user=jenkins_cicd ansible_password=Thienthien123@ ansible_connection=winrm ansible_winrm_server_cert_validation=ignore diff --git a/playbooks/deploy_be.yml b/playbooks/deploy_be.yml new file mode 100644 index 0000000..65aff32 --- /dev/null +++ b/playbooks/deploy_be.yml @@ -0,0 +1,5 @@ +- name: Deploy Backend for Project Acp, Portal + hosts: "{{ target_host }}" + roles: + - deploy + \ No newline at end of file diff --git a/playbooks/deploy_fe.yml b/playbooks/deploy_fe.yml new file mode 100644 index 0000000..aa608f4 --- /dev/null +++ b/playbooks/deploy_fe.yml @@ -0,0 +1,4 @@ +- name: Deploy Frontend for Project Acp, Portal + hosts: "{{ target_host }}" + roles: + - deploy diff --git a/roles/deploy/tasks/cleanup.yml b/roles/deploy/tasks/cleanup.yml new file mode 100644 index 0000000..50350b2 --- /dev/null +++ b/roles/deploy/tasks/cleanup.yml @@ -0,0 +1,5 @@ +# Xóa file tạm +- name: Clean up temporary files + win_file: + path: "{{ temp_dir }}\\{{ artifact_name }}" + state: absent \ No newline at end of file diff --git a/roles/deploy/tasks/common.yml b/roles/deploy/tasks/common.yml new file mode 100644 index 0000000..93564aa --- /dev/null +++ b/roles/deploy/tasks/common.yml @@ -0,0 +1,25 @@ +# Kiểm tra kết nối Nexus +- name: Check Nexus connectivity + uri: + url: "{{ nexus_url }}" + method: GET + return_content: yes + status_code: 200 + register: nexus_response + +- name: Verify Nexus response + fail: + msg: "Unable to connect to Nexus at {{ nexus_url }}" + when: nexus_response.status != 200 + +# Kiểm tra quyền ghi trên thư mục target +- name: Verify write access to target path + win_acl: + path: "{{ target_path }}" + state: query + register: acl_status + +- name: Ensure target path is writable + fail: + msg: "No write permissions for target path: {{ target_path }}" + when: acl_status.permissions | length == 0 diff --git a/roles/deploy/tasks/deploy.yml b/roles/deploy/tasks/deploy.yml new file mode 100644 index 0000000..caeef62 --- /dev/null +++ b/roles/deploy/tasks/deploy.yml @@ -0,0 +1,28 @@ +# Tải artifact từ Nexus +- name: Download artifact from Nexus + win_get_url: + url: "{{ nexus_url }}/{{ artifact_name }}" + dest: "{{ temp_dir }}\\{{ artifact_name }}" + validate_certs: no + url_username: "{{ nexus_username }}" + url_password: "{{ nexus_password }}" + register: download_status + vars: + nexus_url: "{{ nexus_url }}" + artifact_name: "{{ artifact_name }}" + nexus_username: "{{ nexus_username }}" + nexus_password: "{{ nexus_password }}" + +# Kiểm tra xem artifact đã được tải thành công chưa +- name: Verify artifact download + fail: + msg: "Failed to download artifact from Nexus." + when: download_status is failed + +# Giải nén artifact vào thư mục target +- name: Deploy Backend to target environment + win_unzip: + src: "{{ temp_dir }}\\{{ artifact_name }}" + dest: "{{ target_path }}" + overwrite: yes + diff --git a/roles/deploy/tasks/main.yml b/roles/deploy/tasks/main.yml new file mode 100644 index 0000000..50755d2 --- /dev/null +++ b/roles/deploy/tasks/main.yml @@ -0,0 +1,16 @@ +- name: Setup environment + import_tasks: setup.yml + +# - name: Run common checks +# import_tasks: common.yml + +- name: Deploy application + import_tasks: deploy.yml + +# - name: Clean up temporary files +# import_tasks: cleanup.yml + +- name: Switch + import_tasks: switch.yml + + diff --git a/roles/deploy/tasks/setup.yml b/roles/deploy/tasks/setup.yml new file mode 100644 index 0000000..25c7805 --- /dev/null +++ b/roles/deploy/tasks/setup.yml @@ -0,0 +1,43 @@ +- name: Debug iis_site_name + debug: + var: iis_site_name + +#Lấy đường dẫn vật lý của iis site +- name: Get the physical path of the current IIS site + win_shell: | + Import-Module WebAdministration + $site = Get-Website -Name "{{ iis_site_name }}" + $site.PhysicalPath + register: active_path + +# Chuẩn hóa giá trị của active_path +- name: Normalize active_path + set_fact: + active_path: "{{ active_path.stdout | trim }}" + +# Debug các biến quan trọng +- name: Debug active_path + debug: + var: active_path + +- name: Debug blue_path + debug: + var: blue_path + +# Chọn môi trường Blue hoặc Green để deploy +- name: Set target deployment environment + set_fact: + target_path: "{{ green_path if active_path == blue_path else blue_path }}" #lấy ra đường dẫn thư mục cần deploy + +- name: Debug target_path + debug: + var: target_path + +# Kiểm tra các thông số Nexus +- name: Debug Nexus URL + debug: + var: nexus_url + +- name: Debug artifact name + debug: + var: artifact_name \ No newline at end of file diff --git a/roles/deploy/tasks/switch.yml b/roles/deploy/tasks/switch.yml new file mode 100644 index 0000000..8ef77b9 --- /dev/null +++ b/roles/deploy/tasks/switch.yml @@ -0,0 +1,10 @@ +- name: Update IIS web application path + win_iis_website: + name: "{{ iis_site_name }}" + physical_path: "{{ target_path }}" + +# - name: Restart IIS site to apply changes +# win_iis_website: +# name: "{{ iis_site_name }}" +# state: restarted +