首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有assert运算符的不可测正则表达式

带有assert运算符的不可测正则表达式
EN

Stack Overflow用户
提问于 2019-12-18 21:57:45
回答 1查看 2.5K关注 0票数 1

下面的剧本试图断言Nexus映像的用户提示变量等于从Nexus交换机捕获的ansible_net_image。这使我对测试条件的正确格式感到疯狂。

代码语言:javascript
复制
---
- 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"

执行时会显示此错误。

致命:开关:失败!=> {"msg":“条件检查'ansible_net_image \regex:/^bootflash:\/\\/(.) == NX_OS_Upgrade_Version‘失败。错误是:模板错误,而模板字符串:预期的令牌‘结束语句块’,got '/‘。字符串:{%如果ansible_net_image \/(.) == NX_OS_Upgrade_Version %} True {% check %} False {% endif %}"}致命:开关b: failed!=> {"msg":“条件检查'ansible_net_image \\regex:/^bootflash:\/(.) == NX_OS_Upgrade_Version‘’失败。错误是:模板错误,而模板字符串:预期的令牌‘结束语句块’,got '/‘。字符串:{%,如果ansible_net_image =regex:/^bootflash:\/\/(.) == NX_OS_Upgrade_Version %} True {% regex %} False {% endif %}"}

regex已经过测试,但我很难为Ansible play获得正确的语法。

如果有人能告诉我我做错了什么,那就太好了。

EN

回答 1

Stack Overflow用户

发布于 2019-12-18 22:28:14

例如

代码语言:javascript
复制
- 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 }}"

给出

代码语言:javascript
复制
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"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59400511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档