Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于条件失败\u的Ansible失败消息_Ansible - Fatal编程技术网

基于条件失败\u的Ansible失败消息

基于条件失败\u的Ansible失败消息,ansible,Ansible,我正在努力清理一些我们预期的错误,让运行剧本的操作员更容易理解这些错误。下面的代码工作得很好,但我想为每个故障条件分配一条自定义消息。例如,如果var1不等于4,那么我想显示一条消息,上面说“对于邻居x,lldp邻居中没有4个条目” 我知道我可以将它们分开,并遵循下面的逻辑,但我想知道我是否可以在一个游戏中完成这一点 - fail: msg: "There were not 4 entries in the lldp neighbor for neighbor x"

我正在努力清理一些我们预期的错误,让运行剧本的操作员更容易理解这些错误。下面的代码工作得很好,但我想为每个故障条件分配一条自定义消息。例如,如果var1不等于4,那么我想显示一条消息,上面说“对于邻居x,lldp邻居中没有4个条目”

我知道我可以将它们分开,并遵循下面的逻辑,但我想知道我是否可以在一个游戏中完成这一点

- fail:
    msg: "There were not 4 entries in the lldp neighbor for neighbor x"
  when: var1|length != 4
  
- fail:
    msg: "There were not 4 entries in the lldp neighbor for neighbor y"
  when: var2|length != 4

assert
模块用于:

- assert:
    that: var2|length == 4
    msg: 'lldp should have four neighbors'
还有几招:

  • 某些断言可以针对本地主机运行一次(f.e.ansible版本),并且只有
    run\u once:true

  • 添加
    标记:[始终]
    以确保执行断言任务


  • assert
    模块用于:

    - assert:
        that: var2|length == 4
        msg: 'lldp should have four neighbors'
    
    还有几招:

  • 某些断言可以针对本地主机运行一次(f.e.ansible版本),并且只有
    run\u once:true

  • 添加
    标记:[始终]
    以确保执行断言任务

  • 使用一个新的工具怎么样

    根据剧本:

    -主机:所有
    收集事实:不
    变量:
    var1:abcde
    var2:abcd
    任务:
    -失败:
    msg:{{item.msg}}
    时间:item.condition
    回路控制:
    标签:“{item.item}}如果item.condition else'valid'},则为{{'invalid'
    循环:
    -msg:邻居var1的lldp邻居中没有4个条目
    条件:{{var1 | length!=4}”
    项目:var1
    -msg:邻居var2的lldp邻居中没有4个条目
    条件:{{var2 | length!=4}”
    项目:var2
    
    这就产生了重述:

    PLAY [all] **********************************************************************************************************
    
    TASK [fail] *********************************************************************************************************
    failed: [localhost] (item=var1 is invalid) => {"ansible_loop_var": "item", "changed": false, "item": {"condition": true, "item": "var1", "msg": "There were not 4 entries in the lldp neighbor for neighbor var1"}, "msg": "There were not 4 entries in the lldp neighbor for neighbor var1"}
    skipping: [localhost] => (item=var2 is valid) 
    
    PLAY RECAP **********************************************************************************************************
    localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
    
    使用一个新的工具怎么样

    根据剧本:

    -主机:所有
    收集事实:不
    变量:
    var1:abcde
    var2:abcd
    任务:
    -失败:
    msg:{{item.msg}}
    时间:item.condition
    回路控制:
    标签:“{item.item}}如果item.condition else'valid'},则为{{'invalid'
    循环:
    -msg:邻居var1的lldp邻居中没有4个条目
    条件:{{var1 | length!=4}”
    项目:var1
    -msg:邻居var2的lldp邻居中没有4个条目
    条件:{{var2 | length!=4}”
    项目:var2
    
    这就产生了重述:

    PLAY [all] **********************************************************************************************************
    
    TASK [fail] *********************************************************************************************************
    failed: [localhost] (item=var1 is invalid) => {"ansible_loop_var": "item", "changed": false, "item": {"condition": true, "item": "var1", "msg": "There were not 4 entries in the lldp neighbor for neighbor var1"}, "msg": "There were not 4 entries in the lldp neighbor for neighbor var1"}
    skipping: [localhost] => (item=var2 is valid) 
    
    PLAY RECAP **********************************************************************************************************
    localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
    

    谢谢你,但是我怎么能把它建立在一堆条件失败的基础上呢。我正在运行一个命令,并希望在单个播放中验证该命令的输出。我宁愿不必执行多次失败或资产您可以编写一个模块。这比听起来要简单得多,它给了你所有python的能力来编写你想要的任何东西。谢谢你,但是我怎么能基于一堆条件失败呢。我正在运行一个命令,并希望在单个播放中验证该命令的输出。我宁愿不必执行多次失败或资产您可以编写一个模块。它比听起来容易得多,并且它给了您所有python的能力来编写您想要的任何东西。