在Ansible中以更大的输出搜索关键字

时间:2019-06-07 07:03:00

标签: ansible

我有这样的输出。

“ vlan_output ['stdout_lines'] [0]”:[

 "VLAN Name                             Status    Ports", 
    "---- -------------------------------- --------- -------------------------------", 
    "1    default                          active    Po10, Po11, Eth1/44, Eth1/45", 
    "                                                Eth1/46, Eth1/47, Eth1/48", 
    "                                                Eth2/1, Eth2/2, Eth2/3", 
    "2    VLAN0002                         active    Po11, Po12, Po121, Eth1/1", 
    "                                                Eth1/2, Eth1/3, Eth1/4, Eth1/47", 
    "                                                Eth1/48", 
    "3    VLAN0003                         active    Po11, Po12, Po121, Eth1/1", 
    "                                                Eth1/2, Eth1/3, Eth1/4, Eth1/47", 
    "                                                Eth1/48", 
    "4    VLAN0004                         active    Po11, Po12, Po121, Eth1/1", 

VLAN此处显示1,2,3,4 通常我们会删除一系列VLAN。删除后,我需要确保它们不在输出中。当然,实际输出中会有很多VLAN。 通常我们会得到这样的范围3000-3005,3200,3400-3402

删除后,如何在此输出中搜索?在上面的输出中,输出中不应包含3000,3001,3002,3003,3004,3005,3200,3400,3401,3402(3000-3005 = 3001,3002,3003,3004,3005)。

我是Ansible的新手。不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

以下不是完整的答案,而是您所发布问题的开始。

剧本->

---
- hosts: localhost
  gather_facts: true
  tasks:
    - name: play
      set_fact:
        file_lines: "{{ lookup('file', 'table').split('\n') }}"

    - debug:
        msg: "{{ item }}"
      when: item | search("VLAN")
      with_items: "{{ file_lines | list}}"

输出->

[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|search` use `result is search`. This feature will
be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [localhost] => (item="VLAN Name                             Status    Ports", ) => {
    "msg": "\"VLAN Name                             Status    Ports\", "
}
skipping: [localhost] => (item=    "---- -------------------------------- --------- -------------------------------", )
skipping: [localhost] => (item=    "1    default                          active    Po10, Po11, Eth1/44, Eth1/45", )
skipping: [localhost] => (item=    "                                                Eth1/46, Eth1/47, Eth1/48", )
skipping: [localhost] => (item=    "                                                Eth2/1, Eth2/2, Eth2/3", )
ok: [localhost] => (item=    "2    VLAN0002                         active    Po11, Po12, Po121, Eth1/1", ) => {
    "msg": "    \"2    VLAN0002                         active    Po11, Po12, Po121, Eth1/1\", "
}
skipping: [localhost] => (item=    "                                                Eth1/2, Eth1/3, Eth1/4, Eth1/47", )
skipping: [localhost] => (item=    "                                                Eth1/48", )
ok: [localhost] => (item=    "3    VLAN0003                         active    Po11, Po12, Po121, Eth1/1", ) => {
    "msg": "    \"3    VLAN0003                         active    Po11, Po12, Po121, Eth1/1\", "
}
skipping: [localhost] => (item=    "                                                Eth1/2, Eth1/3, Eth1/4, Eth1/47", )
skipping: [localhost] => (item=    "                                                Eth1/48", )
ok: [localhost] => (item=    "4    VLAN0004                         active    Po11, Po12, Po121, Eth1/1",) => {
    "msg": "    \"4    VLAN0004                         active    Po11, Po12, Po121, Eth1/1\","
}

PLAY RECAP *******************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

**我已经使用查找来获取所需的信息,但是您可以使用寄存器变量的输出 **

答案 1 :(得分:0)

我通过使用简单的正则表达式解决了这个问题

  • set_fact:vlan_regex =“ VLAN0003 \ s”

  • set_fact:run_vlan =“ {{vlan_output ['stdout_lines'] [0] | select('search',vlan_regex)| list}}”

  • 调试:var = run_vlan

带有关键字后跟空格的简单正则表达式