我有这样的字符串
"3000 Native active Po121, Po123"
我需要检查此字符串中是否存在3000和active。如果没有断言
我想使用when
命令和set_fact
。检查变量是否存在并断言。 (我还没有完成)。现在,我只是在打印一条消息。这不是这样做的好方法。如果我可以直接断言3000和活动状态不存在的时间,那就太好了。
还有另一个问题,如果何时匹配第一个条件,它将打印调试消息。它应该同时匹配正确的an和?
var:
vlan_output: "3000 Native active Po121, Po123"
item={vlan_id: 3000, state: present}
我尝试过这样
- name: Validate vlan for delete
debug: msg="VLAN FAILED "
when: item.state == "present" and "item.vlan_id not in vlan_output"
答案 0 :(得分:0)
这里有几个问题
首先,您不应该引用"item.vlan_id not in vlan_output"
-这是一个字符串,并且始终会得出True
。
第二,not in
测试要求操作数为字符串类型(当前vlan_id
是整数)。
通过这些更改,您应该看到想要的行为:
vars:
vlan_output: "3000 Native active Po121, Po123"
item:
vlan_id: "3000"
state: present
tasks:
- debug: msg="VLAN FAILED"
when: item.state == "present" and item.vlan_id not in vlan_output
答案 1 :(得分:0)
如果输出中存在“ 3000”和“活动”,我们可以直接使用when条件
我相信vlan_id将是一个注册变量,因此可以使用vlan_id.stdout访问该值。
以下内容适用于ansible的when和assert模块
断言+ ve:
命令->
ansible-playbook tmp.yml --extra-vars "vlan_output='3000 active'"
剧本->
---
- hosts: localhost
tasks:
- debug:
msg: "Strings Matched"
when: vlan_output | search("3000") and vlan_output | search("active")
- debug:
var: vlan_output
- assert:
that:
- "'3000' in vlan_output"
- "'active' in vlan_output"
输出->
ok: [localhost] => {
"msg": "Strings Matched"
}
TASK [debug] *****************************************************************************************************************************
ok: [localhost] => {
"vlan_output": "3000 active"
}
TASK [assert] ****************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
PLAY RECAP *******************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
断言-ve:
命令->
ansible-playbook tmp.yml --extra-vars "vlan_output='is'"
剧本->
---
- hosts: localhost
tasks:
- debug:
msg: "Strings Matched"
when: vlan_output is not search("3000") and vlan_output is not search("active")
- debug:
var: vlan_output
- assert:
that:
- "'3000' not in vlan_output"
- "'active' not in vlan_output"
输出->
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Strings Matched"
}
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"vlan_output": "is"
}
TASK [assert] **********************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0