2017-03-31 20 views
-1

嘗試將路由信息解析爲類似下面的內容。 怎樣做解析表格中的數據

[ 
    { 
     "destination": '10.110.2.192', 
     "gateway": '10.110.0.129' 
     "interface": 'eth2' 
    }, 
    { 
     "destination": '10.110.2.64', 
     "gateway": '10.110.0.129' 
     "interface": 'eth2' 
    }, 
    { 
     "destination": '10.110.1.0', 
     "gateway": '0.0.0.0' 
     "interface": 'eth0' 
    }, 
    { 
     "destination": '10.110.1.128', 
     "gateway": '0.0.0.0' 
     "interface": 'eth2' 
    }, 
    { 
     "destination": '0.0.0.0', 
     "gateway": '10.110.1.1' 
     "interface": 'eth0' 
    } 
] 
+0

請提供原始數據。但最有可能的是直接解析你的數據是不合理的。 – iptizer

回答

0

正確的方式,你可以把這個作爲例子:

第一部分爲原生Ansible用正則表達式搜索和第二部分遠程主機作爲幫助上使用jq

--- 
- hosts: server 
    gather_facts: no 
    tasks: 
    # without jq on remote host 
    - command: netstat -nrw 
     register: routes 
    - debug: 
     msg: "{{ route }}" 
     with_items: "{{ routes.stdout_lines[2:] }}" 
     vars: 
     route: '{{ item | regex_search(regex,"\g<dest>","\g<gw>","\g<mask>","\g<flags>","\g<iface>") }}' 
     regex: '^(?P<dest>\S+)\s+(?P<gw>\S+)\s+(?P<mask>\S+)\s+(?P<flags>\S+).*\s(?P<iface>\S+)$' 
    # with jq on remote host 
    - shell: netstat -nrw | tail -n+3 | jq -R -c 'capture("^(?<dest>\\S+)\\s+(?<gw>\\S+)\\s+(?<mask>\\S+)\\s+(?<flags>\\S+).*\\s(?<iface>\\S+)$")' 
     register: routes 
    - debug: 
     msg: "{{ item }}" 
     with_items: "{{ routes.stdout_lines | map('from_json') | list }}"