| 1 |
--- |
| 2 |
- name: Using IBM Storage Virtualize Ansible collection to automate firmware upgrade using applysoftware |
| 3 |
hosts: localhost |
| 4 |
gather_facts: false |
| 5 |
vars_files: |
| 6 |
- inventory.ini |
| 7 |
vars: |
| 8 |
delay_period: "{{ poll_period | default(300) }}" |
| 9 |
retry_attempts: "{{ poll_retries | default(90) }}" |
| 10 |
upgrade_already_initiated: false |
| 11 |
tasks: |
| 12 |
- name: Check if an upgrade is already in progress |
| 13 |
register: update_status |
| 14 |
ibm.storage_virtualize.ibm_svc_info: |
| 15 |
clustername: "{{ clustername }}" |
| 16 |
username: "{{ username }}" |
| 17 |
password: "{{ password }}" |
| 18 |
command_list: ["lsupdate"] |
| 19 |
|
| 20 |
- name: Set ongoing upgrade version |
| 21 |
when: update_status.Update is defined and update_status.Update.status != 'success' and update_status.Update.system_new_code_level != '' |
| 22 |
ansible.builtin.set_fact: |
| 23 |
current_upgrade_version: "{{ update_status.Update.system_new_code_level | regex_search('^(\\d+\\.\\d+\\.\\d+\\.\\d+)') }}" |
| 24 |
|
| 25 |
- name: Fail if currently ongoing upgrade is not the desired upgrade |
| 26 |
when: current_upgrade_version is defined and current_upgrade_version != firmware_version_number |
| 27 |
ansible.builtin.fail: |
| 28 |
msg: An upgrade is already in progress to version {{ current_upgrade_version }}. Please wait for the ongoing upgrade to complete. |
| 29 |
|
| 30 |
- name: Set upgrade_already_initiated to true |
| 31 |
when: current_upgrade_version is defined and current_upgrade_version == firmware_version_number |
| 32 |
ansible.builtin.set_fact: |
| 33 |
upgrade_already_initiated: true |
| 34 |
|
| 35 |
- name: Check current build on the system |
| 36 |
register: system_info |
| 37 |
ibm.storage_virtualize.ibm_svc_info: |
| 38 |
clustername: "{{ clustername }}" |
| 39 |
username: "{{ username }}" |
| 40 |
password: "{{ password }}" |
| 41 |
gather_subset: "system" |
| 42 |
|
| 43 |
- name: Set timestamp cache file path |
| 44 |
ansible.builtin.set_fact: |
| 45 |
timestamp_cache_file: "{{ system_info.System.name }}_{{ firmware_version_number | replace('.', '_') }}_timestamp.yml" |
| 46 |
|
| 47 |
- name: Check if upgrade path is valid and start upgrade process |
| 48 |
when: not upgrade_already_initiated |
| 49 |
block: |
| 50 |
- name: Extract code level from current build |
| 51 |
ansible.builtin.set_fact: |
| 52 |
current_code_level: "{{ system_info.System.code_level | regex_search('^(\\d+\\.\\d+\\.\\d+\\.\\d+)') }}" |
| 53 |
|
| 54 |
- name: Display message if versions match |
| 55 |
ansible.builtin.debug: |
| 56 |
msg: The current build version {{ current_code_level }} is the same as the new build version {{ firmware_version_number }}. No upgrade needed. |
| 57 |
when: current_code_level == firmware_version_number |
| 58 |
|
| 59 |
- name: Stop the play if versions match |
| 60 |
ansible.builtin.meta: end_play |
| 61 |
when: current_code_level == firmware_version_number |
| 62 |
|
| 63 |
- name: Compare version numbers, to disallow downgrades at the start (applysoftware does not support downgrades) |
| 64 |
ansible.builtin.set_fact: |
| 65 |
downgrade_flag: >- |
| 66 |
{{ (firmware_version_number.split('.') | map('int') | list) < |
| 67 |
(current_code_level.split('.') | map('int') | list) }} |
| 68 |
|
| 69 |
- name: Stop if the current build is the newer than as the desired build |
| 70 |
when: downgrade_flag |
| 71 |
ansible.builtin.fail: |
| 72 |
msg: > |
| 73 |
The current build version {{ current_code_level }} is newer than the build version desired {{ firmware_version_number }}. This is not supported. |
| 74 |
|
| 75 |
- name: Extract build and test file names from paths |
| 76 |
ansible.builtin.set_fact: |
| 77 |
build_name: "{{ firmware_package_src_path | basename }}" |
| 78 |
test_name: "{{ upgrade_test_util_src_path | basename }}" |
| 79 |
|
| 80 |
- name: Remove host entries from known_hosts to avoid SSH issues |
| 81 |
ansible.builtin.command: ssh-keygen -R "{{ clustername }}" |
| 82 |
ignore_errors: true |
| 83 |
register: remove_known_hosts_entries |
| 84 |
changed_when: remove_known_hosts_entries.rc != 0 |
| 85 |
|
| 86 |
- name: Copy svcupgradetest (CCU Checker) from Ansible controller to cluster |
| 87 |
ansible.builtin.expect: |
| 88 |
command: scp -O -o StrictHostKeyChecking=no -r {{ upgrade_test_util_src_path }} {{ username }}@{{ clustername }}:/upgrade |
| 89 |
responses: |
| 90 |
(?i)Are you sure you want to continue connecting (yes/no/[fingerprint])?: "yes\r\n" |
| 91 |
(?i)Password: "{{ password }}" |
| 92 |
timeout: 60 |
| 93 |
|
| 94 |
- name: Install svcupgradetest utility |
| 95 |
register: svcupgradetest_install |
| 96 |
ibm.storage_virtualize.ibm_svctask_command: |
| 97 |
command: "svctask applysoftware -file {{ test_name }}" |
| 98 |
clustername: "{{ clustername }}" |
| 99 |
username: "{{ username }}" |
| 100 |
password: "{{ password }}" |
| 101 |
failed_when: svcupgradetest_install.rc not in [0, 1] |
| 102 |
|
| 103 |
- name: Check if svcupgradetest utility was installed successfully |
| 104 |
block: |
| 105 |
- name: Run svcupgradetest utility |
| 106 |
ansible.builtin.expect: |
| 107 |
command: "ssh {{ username }}@{{ clustername }} svcupgradetest -v {{ firmware_version_number }}" |
| 108 |
responses: |
| 109 |
(?i)Are you sure you want to continue connecting (yes/no/[fingerprint])?: "yes\r\n" |
| 110 |
(?i)Password: "{{ password }}" |
| 111 |
timeout: null |
| 112 |
register: svcupgradetest_result |
| 113 |
rescue: |
| 114 |
- name: Disallow upgrade paths which are not supported |
| 115 |
when: > |
| 116 |
svcupgradetest_result.stdout_lines | |
| 117 |
select('match', '.*upgrade path from \\d+\\.\\d+\\.\\d+\\.\\d+ to \\d+\\.\\d+\\.\\d+\\.\\d+ is unsupported.*') | list | length > 0 |
| 118 |
ansible.builtin.fail: |
| 119 |
msg: The tool (svcupgradetest) has detected that the upgrade path specified is unsupported. |
| 120 |
|
| 121 |
- name: Parse svcupgradetest output for errors and warnings |
| 122 |
ansible.builtin.set_fact: |
| 123 |
error_line: > |
| 124 |
"{{ svcupgradetest_result.stdout_lines | select('match', '^The tool has found \\d+ errors and \\d+ warnings\\.$') | list | first }}" |
| 125 |
|
| 126 |
- name: Extract number of warnings and errors from the line |
| 127 |
ansible.builtin.set_fact: |
| 128 |
tool_stats: "{{ error_line | regex_findall('\\d+') }}" |
| 129 |
|
| 130 |
- name: Fail if any errors or warnings are found |
| 131 |
ansible.builtin.fail: |
| 132 |
msg: > |
| 133 |
WARNING: The tool (svcupgradetest) has found {{ tool_stats[0] }} errors and {{ tool_stats[1] }} warnings. |
| 134 |
Resolve them before proceeding with the upgrade. If you want to proceed despite the warnings, |
| 135 |
modify the task 'Run svcupgradetest utility' to set ignore_errors to true and re-run the playbook. |
| 136 |
when: (tool_stats[0] | int > 0) or (tool_stats[1] | int > 0) |
| 137 |
|
| 138 |
- name: Copy build from Ansible controller to cluster |
| 139 |
ansible.builtin.expect: |
| 140 |
command: scp -O -o StrictHostKeyChecking=no -r {{ firmware_package_src_path }} {{ username }}@{{ clustername }}:/upgrade |
| 141 |
responses: |
| 142 |
(?i)Are you sure you want to continue connecting (yes/no/[fingerprint])?: "yes\r\n" |
| 143 |
(?i)Password: "{{ password }}" |
| 144 |
timeout: null |
| 145 |
|
| 146 |
- name: Install build on cluster |
| 147 |
ibm.storage_virtualize.ibm_svctask_command: |
| 148 |
command: "svctask applysoftware -file {{ build_name }}" |
| 149 |
clustername: "{{ clustername }}" |
| 150 |
username: "{{ username }}" |
| 151 |
password: "{{ password }}" |
| 152 |
register: build_applysoftware |
| 153 |
failed_when: build_applysoftware.rc not in [0, 1] |
| 154 |
|
| 155 |
- name: Check if timestamp file exists |
| 156 |
ansible.builtin.stat: |
| 157 |
path: "{{ timestamp_cache_file }}" |
| 158 |
register: timestamp_file |
| 159 |
|
| 160 |
- name: Get cached timestamp |
| 161 |
when: timestamp_file.stat.exists |
| 162 |
ansible.builtin.include_vars: |
| 163 |
file: "{{ timestamp_cache_file }}" |
| 164 |
|
| 165 |
- name: If cache does not exist, get timestamp and cache |
| 166 |
when: not timestamp_file.stat.exists |
| 167 |
block: |
| 168 |
- name: Get controller start time |
| 169 |
ansible.builtin.command: "date +%s" |
| 170 |
changed_when: false |
| 171 |
register: controller_start_time_output |
| 172 |
|
| 173 |
- name: Set controller start time in epoch |
| 174 |
ansible.builtin.set_fact: |
| 175 |
controller_start_time: "{{ controller_start_time_output.stdout | int }}" |
| 176 |
|
| 177 |
- name: Write controller start time to cache |
| 178 |
ansible.builtin.copy: |
| 179 |
content: | |
| 180 |
controller_start_time: {{ controller_start_time }} |
| 181 |
dest: "{{ timestamp_cache_file }}" |
| 182 |
mode: '0644' |
| 183 |
|
| 184 |
- name: Get event log |
| 185 |
register: event_log |
| 186 |
ibm.storage_virtualize.ibm_svc_info: |
| 187 |
clustername: "{{ clustername }}" |
| 188 |
username: "{{ username }}" |
| 189 |
password: "{{ password }}" |
| 190 |
gather_subset: ["eventlog"] |
| 191 |
|
| 192 |
- name: Get start upgrade event |
| 193 |
ansible.builtin.set_fact: |
| 194 |
start_time_event: "{{ event_log.EventLog | selectattr('description', 'equalto', 'Update prepared') | list }}" |
| 195 |
|
| 196 |
- name: Capture upgrade start time in epoch |
| 197 |
when: start_time_event | length > 0 |
| 198 |
ansible.builtin.set_fact: |
| 199 |
system_start_time: "{{ (start_time_event[0].last_timestamp | to_datetime('%y%m%d%H%M%S')).strftime('%s') | int }}" |
| 200 |
|
| 201 |
- name: Set system_start_time to controller if system time is not found |
| 202 |
when: system_start_time is not defined |
| 203 |
ansible.builtin.set_fact: |
| 204 |
system_start_time: "{{ controller_start_time }}" |
| 205 |
|
| 206 |
- name: Set offset |
| 207 |
ansible.builtin.set_fact: |
| 208 |
offset: "{{ (system_start_time | int) - (controller_start_time | int) }}" |
| 209 |
|
| 210 |
- name: Check and display upgrade progress |
| 211 |
ansible.builtin.include_tasks: _upgrade_status.yaml |
| 212 |
loop: "{{ range(0, retry_attempts | int) | list }}" |
| 213 |
when: upgrade_progress.Update is not defined or upgrade_progress.Update.status != 'success' |
| 214 |
|
| 215 |
- name: Stop if operation has not succeeded |
| 216 |
ansible.builtin.fail: |
| 217 |
msg: "ERROR: Upgrade did not complete successfully" |
| 218 |
when: upgrade_progress.Update.status != 'success' |
| 219 |
|
| 220 |
- name: Remove timestamp file |
| 221 |
ansible.builtin.file: |
| 222 |
path: "{{ timestamp_cache_file }}" |
| 223 |
state: absent |