下面的剧本试图断言Nexus映像的用户提示变量等于从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"执行时会显示此错误。
致命:开关:失败!=> {"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获得正确的语法。
如果有人能告诉我我做错了什么,那就太好了。
发布于 2019-12-18 22:28:14
例如
- 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 }}"给出
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"
}https://stackoverflow.com/questions/59400511
复制相似问题