Files
nsg_ansible/roles/deploy-be-portal/tasks/setup.yml
T
thienqb123456 22d94c2530 fix3
2025-03-03 18:27:51 +07:00

105 lines
3.4 KiB
YAML

#Debug
- name: Debug iis_site_name
debug:
var: iis_site_name
- name: Debug aspnetcore_environtment
debug:
var: aspnetcore_environtment
- name: Debug blue_path
debug:
var: blue_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
- name: Validate required variables
assert:
that:
- iis_site_name is defined and iis_site_name | length > 0
- aspnetcore_environtment is defined and aspnetcore_environtment | length > 0
- blue_path is defined and blue_path | length > 0
- nexus_url is defined and nexus_url | length > 0
- artifact_name is defined and artifact_name | length > 0
fail_msg: "One or more required variables are missing or empty!"
- name: Check if IIS site exists
win_shell: |
Import-Module WebAdministration
if (Get-Website -Name "{{ iis_site_name }}") { "exists" } else { "not exists" }
register: site_check
failed_when: "'not exists' in site_check.stdout"
- name: Debug site existence
debug:
msg: "IIS site {{ iis_site_name }} exists!"
- name: Get current ASPNETCORE_ENVIRONMENT
ansible.windows.win_shell: |
Import-Module WebAdministration
$envVars = Get-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/{{ iis_site_name }}" `
-filter "system.webServer/aspNetCore/environmentVariables" `
-name "."
$envVar = $envVars | Where-Object { $_.name -eq "ASPNETCORE_ENVIRONMENT" }
if ($envVar) { $envVar.value } else { "NOT_SET" }
register: current_env
changed_when: false # Tránh bị đánh dấu là "changed" nếu chỉ đọc dữ liệu
- name: Debug current environment
debug:
var: current_env.stdout
- name: Set ASPNETCORE_ENVIRONMENT for IIS site if different
ansible.windows.win_shell: |
Import-Module WebAdministration
Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/{{ iis_site_name }}" `
-filter "system.webServer/aspNetCore/environmentVariables" `
-name "." `
-value @{name='ASPNETCORE_ENVIRONMENT'; value='{{ aspnetcore_environment }}'}
when: current_env.stdout != aspnetcore_environment
# - name: Set ASPNETCORE_ENVIRONMENT for site
# win_shell: |
# Import-Module WebAdministration
# Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/{{ iis_site_name }}" `
# -filter "system.webServer/aspNetCore/environmentVariables" `
# -name "." `
# -value @{name="ASPNETCORE_ENVIRONMENT"; value="{{ aspnetcore_environtment }}"}
#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
- debug:
var: active_path
- name: Handle error if active_path is null
fail:
msg: "The IIS site active path could not be determined."
when: active_path.stdout is not defined or active_path.stdout == ''
# Chuẩn hóa giá trị của active_path
- name: Normalize active_path
set_fact:
active_path: "{{ active_path.stdout | trim }}"
- debug:
var: active_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
- debug:
var: target_path