1

I have the following playbook that is trying to assert that a user prompt variable of NX-OS image equals the ansible_net_image captured from a Nexus switch. This is driving me crazy as to the correct format for the test condition.

---
- name: Upgrade NX-OS on switch with pre-checks to load image as required
  hosts:  nexus-7000
  gather_facts: no
  vars_prompt:
    - name: NX_OS_Upgrade_Version
      prompt: "Please enter NX-OS version to upgrade too - ensure that the filename is as used by boot variable"
      private: no
  ##############################################################################
  ## Start of task execution
  ##############################################################################
  tasks:
  - name: Gather IOS configuration and software facts from switches
    nxos_facts:
      gather_subset: "!hardware"
################################################################################
## Display running image version to terminal
################################################################################
  - debug:
      msg: "{{ NX_OS_Upgrade_Version }}"
  - assert:
      that:
        - "ansible_net_image | regex:/^bootflash:\\/\\/\\/(.*) ==  NX_OS_Upgrade_Version"

Upon execution this error is displayed.

fatal: [switcha]: FAILED! => {"msg": "The conditional check 'ansible_net_image | regex:/^bootflash:\/\/\/(.) == NX_OS_Upgrade_Version' failed. The error was: template error while templating string: expected token 'end of statement block', got '/'. String: {% if ansible_net_image | regex:/^bootflash:\/\/\/(.) == NX_OS_Upgrade_Version %} True {% else %} False {% endif %}"} fatal: [switchb]: FAILED! => {"msg": "The conditional check 'ansible_net_image | regex:/^bootflash:\/\/\/(.) == NX_OS_Upgrade_Version' failed. The error was: template error while templating string: expected token 'end of statement block', got '/'. String: {% if ansible_net_image | regex:/^bootflash:\/\/\/(.) == NX_OS_Upgrade_Version %} True {% else %} False {% endif %}"}

The regex has been tested, but I am struggling to get the correct syntax for the Ansible play.

It would be great if someone could show me what I have got wrong.

Adrian Mole
  • 43,040
  • 110
  • 45
  • 72
MikefNZ
  • 11
  • 2

1 Answers1

1

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

ok: [localhost] => {
    "changed": false, 
    "msg": "Image is version n5000-uk9-kickstart.5.1.3.N2.1b"
}

fatal: [localhost]: FAILED! => {
    "assertion": "ansible_net_image is match(my_regex2)", 
    "changed": false, 
    "evaluated_to": false, 
    "msg": "Image is NOT version n5000-uk9-kickstart.5.1.4.N2.1b"
}
Vladimir Botka
  • 39,879
  • 4
  • 23
  • 43
  • Hi Vladimir for your response. This works but is there a way to achieve the same using the vars_prompt for user input and the nxos_facts: ansible_net_image variable? I don't want to hard code the variables every time the play is run against switches with different versions. The intent is to check the version and copy a new image and upgrade if required. – MikefNZ Dec 19 '19 at 00:53