Update
Q: "Check the version, copy a new image, and upgrade if required."
A: You can test the version. See Comparing versions. For example, given the current image for testing
ansible_net_image: bootflash:///n5000-uk9-kickstart.5.1.3.N2.1b.bin
Get the version. Declare the variable
ani_version: "{{ ansible_net_image|
splitext|first|
split('.', 1)|last }}"
gives
ani_version: 5.1.3.N2.1b
Then test the version. In your question, you try to find out whether the versions are equal or not. This can be achieved by the comparison '=='
below
- assert:
that: ani_version is version(NX_OS_Upgrade_Version, '==')
success_msg: "Image is version {{ NX_OS_Upgrade_Version }}"
fail_msg: "Image is NOT version {{ NX_OS_Upgrade_Version }}"
But, to check whether the image is newer or not you might want to use the comparison '<'
- assert:
that: ani_version is version(NX_OS_Upgrade_Version, '<')
success_msg: "Image is newer version {{ NX_OS_Upgrade_Version }}"
fail_msg: "Image is not newer version {{ NX_OS_Upgrade_Version }}"
Example of a complete playbook for testing
shell> cat pb.yml
- hosts: localhost
vars:
ani_version: "{{ ansible_net_image|
splitext|first|
split('.', 1)|last }}"
vars_prompt:
- name: NX_OS_Upgrade_Version
prompt: Please enter NX-OS version to upgrade to
private: no
tasks:
- name: Gather IOS configuration and software facts from switches
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# For testing use *set_fact* instead of *nxos_facts*
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# nxos_facts:
# gather_subset: "!hardware"
set_fact:
ansible_net_image: bootflash:///n5000-uk9-kickstart.5.1.3.N2.1b.bin
- debug:
msg: |
ani_version: {{ ani_version }}
NX_OS_Upgrade_Version: {{ NX_OS_Upgrade_Version }}
- assert:
# that: ani_version is version(NX_OS_Upgrade_Version, '==')
# success_msg: "Image is version {{ NX_OS_Upgrade_Version }}"
# fail_msg: "Image is NOT version {{ NX_OS_Upgrade_Version }}"
that: ani_version is version(NX_OS_Upgrade_Version, '<')
success_msg: "Image is newer version {{ NX_OS_Upgrade_Version }}"
fail_msg: "Image is not newer version {{ NX_OS_Upgrade_Version }}"
- debug:
msg: Copy and upgrade new image.
Copy and upgrade the new image
shell> ansible-playbook pb.yml
Please enter NX-OS version to upgrade to: 5.1.3.N2.1c
PLAY [localhost] ******************************************************************************
TASK [Gather IOS configuration and software facts from switches] ******************************
ok: [localhost]
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: |-
ani_version: 5.1.3.N2.1b
NX_OS_Upgrade_Version: 5.1.3.N2.1c
TASK [assert] *********************************************************************************
ok: [localhost] => changed=false
msg: Image is newer version 5.1.3.N2.1c
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: Copy and upgrade new image.
PLAY RECAP ************************************************************************************
localhost: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Fails if the image is not newer
shell> ansible-playbook pb.yml
Please enter NX-OS version to upgrade to: 5.1.3.N2.1b
PLAY [localhost] ******************************************************************************
TASK [Gather IOS configuration and software facts from switches] ******************************
ok: [localhost]
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: |-
ani_version: 5.1.3.N2.1b
NX_OS_Upgrade_Version: 5.1.3.N2.1b
TASK [assert] *********************************************************************************
fatal: [localhost]: FAILED! => changed=false
assertion: ani_version is version(NX_OS_Upgrade_Version, '<')
evaluated_to: false
msg: Image is not newer version 5.1.3.N2.1b
PLAY RECAP ************************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Origin
For example,
- hosts: localhost
vars:
ansible_net_image: 'bootflash:///n5000-uk9-kickstart.5.1.3.N2.1b.bin'
NX_OS_Upgrade_Version1: 'n5000-uk9-kickstart.5.1.3.N2.1b'
my_regex1: '^bootflash:///{{ NX_OS_Upgrade_Version1 }}.bin'
NX_OS_Upgrade_Version2: 'n5000-uk9-kickstart.5.1.4.N2.1b'
my_regex2: '^bootflash:///{{ NX_OS_Upgrade_Version2 }}.bin'
tasks:
- assert:
that: ansible_net_image is match(my_regex1)
success_msg: "Image is version {{ NX_OS_Upgrade_Version1 }}"
fail_msg: "Image is NOT version {{ NX_OS_Upgrade_Version1 }}"
- assert:
that: ansible_net_image is match(my_regex2)
success_msg: "Image is version {{ NX_OS_Upgrade_Version2 }}"
fail_msg: "Image is NOT version {{ NX_OS_Upgrade_Version2 }}"
gives (abridged)
TASK [assert] *************************************************************
ok: [localhost] => changed=false
msg: Image is version n5000-uk9-kickstart.5.1.3.N2.1b
TASK [assert] *************************************************************
fatal: [localhost]: FAILED! => changed=false
assertion: ansible_net_image is match(my_regex2)
evaluated_to: false
msg: Image is NOT version n5000-uk9-kickstart.5.1.4.N2.1b