简体   繁体   English

Ansible 将标准输出与变量进行比较

[英]Ansible compare stdout with a variable

I have a questions since I am giving crazy.我有一个问题,因为我正在发疯。 I want to compare stdout result to one variable storaged in my vars file.我想将标准输出结果与存储在我的 vars 文件中的一个变量进行比较。 For instance, instead of using the variable but the string ( The IP 22.22.2.2), it works:例如,不是使用变量而是使用字符串(IP 22.22.2.2),它可以工作:

Nets.yml网络.yml

- List_nets_find: 
  - host 22.22.2.2

Playbook:剧本:

---
- name: Black List
  hosts: routers
  gather_facts: false
  vars_files:
   - vars/Nets.yml

  tasks:    

   - name: Check the object-group
     ios_command:
      commands: 
       - command: "show object-group BLOCK-LIST" 
     register: output
     tags:
     - see
   - debug:
      msg: included
     when: output.stdout is search('22.22.2.2')
     tags:
     - see

Result:结果:

TASK [debug] **************************************************************************************************************************************************************************************
skipping: [Router1]
ok: [Router3] => {
    "msg": "included"
}
ok: [Router2] => {
    "msg": "included"
}

But instead of using the string, I use the Variable, it does not work.但是我没有使用字符串,而是使用了变量,它不起作用。

- name: Black List
  hosts: routers
  gather_facts: false
  vars_files:
   - vars/Nets.yml

  tasks:    

   - name: Check the object-group
     ios_command:
      commands: 
       - command: "show object-group BLOCK-LIST" 
     register: output
     tags:
     - see
   - debug:
      msg: included
     when: output.stdout is search('{{List_net_find}}')
     tags:
     - see

This is the error:这是错误:

fatal: [Router1]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n         - see\n       - debug:\n         ^ here\n"}
fatal: [Router3]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n         - see\n       - debug:\n         ^ here\n"}
fatal: [Router2]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n         - see\n       - debug:\n         ^ here\n"}

Any suggestion?有什么建议吗? or other way to do it?或其他方式来做到这一点?

Thanks so much!非常感谢!

Your vars/Nets.yml does not declare a variable named List_nets_find , it declares an anonymous list whose first element is a dict whose only key is named List_nets_find您的vars/Nets.yml没有声明名为List_nets_find变量,它声明了一个匿名列表,其第一个元素是一个dict ,其唯一键名为List_nets_find

If you want a variable named that, then change - List_nets_find: to be List_nets_find:如果您想要一个名为该变量的变量,则将- List_nets_find:更改为List_nets_find:


Separately, the next question you are going to ask is "why does my search not match anything" and that's because List_nets_find is a list of str, so you will want to change that test to be output.stdout is search(List_nets_find | map("regex_escape") | join("|")) , using the regex_escape filter to turn those strings into something that can be safely fed into a search alternation另外,您要问的下一个问题是“为什么我的搜索不匹配任何内容”,这是因为List_nets_find是 str 的列表,因此您需要将该测试更改为output.stdout is search(List_nets_find | map("regex_escape") | join("|")) ,使用regex_escape过滤器将这些字符串转换为可以安全地输入search替换的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM