-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'
     }
]
4

1 回答 1

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 }}"
于 2017-04-02T09:40:49.747 回答