regex 带有assert运算符的Ansible正则表达式

bfrts1fy  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(110)

我有以下剧本,试图AssertNX-OS映像的用户提示变量等于从Nexus交换机捕获的ansible_net_image。这让我对测试条件的正确格式感到疯狂。

---
- 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"

执行时显示此错误。
fatal:[switcha]:失败!=〉{“msg”:“条件检查'ansible_net_image|regex:/^bootflash:///(.)== NX_OS_Upgrade_Version'失败。错误为:模板化字符串时模板错误:应为标记“语句块结束”,得到“/”。字符串:{% if ansible_net_image|regex:/^bootflash:///(.)== NX_OS_Upgrade_Version %} True {% else %} False {% endif %}"} fatal:[switchb]:失败!=〉{“msg”:“条件检查'ansible_net_image|regex:/^bootflash:///(.)== NX_OS_Upgrade_Version'失败。错误为:模板化字符串时模板错误:应为标记“语句块结束”,得到“/”。字符串:{% if ansible_net_image|regex:/^bootflash:///(.)== NX_OS_Upgrade_Version %} True {% else %} False {% endif %}"}
正则表达式已经测试过了,但是我很难为Ansible play找到正确的语法。
如果有人能告诉我我做错了什么,那就太好了。

svujldwt

svujldwt1#

更新

问:***“检查版本,复制新映像,并根据需要进行升级。”***
答:您可以测试 * 版本 *。请参阅 * 比较版本 *。例如,给定当前映像进行测试

ansible_net_image: bootflash:///n5000-uk9-kickstart.5.1.3.N2.1b.bin

获取版本。声明变量

ani_version: "{{ ansible_net_image|
                   splitext|first|
                   split('.', 1)|last }}"

给予

ani_version: 5.1.3.N2.1b

然后测试版本。在您的问题中,您试图找出版本是否相等。这可以通过下面的比较'=='来实现

- 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 }}"

但是,要检查图像是否更新,您可能需要使用比较'<'

- 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 }}"

完整的测试剧本示例

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.

复制并升级新映像

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

如果映像不是较新的,则失败

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

来源

  • 使用7.3.2.单引号样式创建正则表达式。
  • 请参见测试字符串。

比如说

- 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

相关问题