Ansible Debug消息重定向为文件

时间:2018-11-20 07:40:16

标签: ansible

我正在寻找一些指导来解决无法将Ansible调试输出保存或重定向到JSON或md文件的问题。

    - debug:
        msg: 
         - "{{ item.results['show ip route'].splitlines() }}"  
         - "{{ item.results['show ip route summary'].splitlines() }}"
         - "{{ item.results['show ip route 0.0.0.0'].splitlines() }}"

      loop:
        - "{{ out2 }}"

上面提到的调试模块在我的剧本的最后运行。该剧本主要使用“ napalm_cli”网络模块来收集设备的少量输出。 “ napalm_cli”模块的输出格式不正确,因此,我必须使用分割线。

现在我正尝试将以下输出保存为文件

ok: [lab1-r1] => (item={'failed': False, u'changed': False, u'results': {u'show ip route': u'Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP\n       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area \n       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2\n       E1 - OSPF external type 1, E2 - OSPF external type 2\n       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2\n       ia - IS-IS inter area, * - candidate default, U - per-user static route\n       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP\n       a - application route\n       + - replicated route, % - next hop override, p - overrides from PfR\n\nGateway of last resort is not set\n\n      172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks\nC        172.16.10.0/24 is directly connected, GigabitEthernet0/1\nL        172.16.10.1/32 is directly connected, GigabitEthernet0/1', u'show ip route summary': u'IP routing table name is default (0x0)\nIP routing table maximum-paths is 32\nRoute Source    Networks    Subnets     Replicates  Overhead    Memory (bytes)\nconnected       0           2           0           136         360\nstatic          0           0           0           0           0\napplication     0           0           0           0           0\ninternal        1                                               440\nTotal           1           2           0           136         800', u'show ip route 0.0.0.0': u'% Network not in table'}}) => {
"msg": [
    [
        "Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP", 
        "       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area ", 
        "       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2", 
        "       E1 - OSPF external type 1, E2 - OSPF external type 2", 
        "       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2", 
        "       ia - IS-IS inter area, * - candidate default, U - per-user static route", 
        "       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP", 
        "       a - application route", 
        "       + - replicated route, % - next hop override, p - overrides from PfR", 
        "", 
        "Gateway of last resort is not set", 
        "", 
        "      172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks", 
        "C        172.16.10.0/24 is directly connected, GigabitEthernet0/1", 
        "L        172.16.10.1/32 is directly connected, GigabitEthernet0/1"
    ], 
    [
        "IP routing table name is default (0x0)", 
        "IP routing table maximum-paths is 32", 
        "Route Source    Networks    Subnets     Replicates  Overhead    Memory (bytes)", 
        "connected       0           2           0           136         360", 
        "static          0           0           0           0           0", 
        "application     0           0           0           0           0", 
        "internal        1                                               440", 
        "Total           1           2           0           136         800"
    ], 
    [
        "% Network not in table"
    ]
]
}

此外,我想摆脱

之间的内容
ok: [lab1-r1] =>   napalm_cli non formatted output
"msg": [

任何想法或想法。

谢谢

NN

1 个答案:

答案 0 :(得分:0)

我相信您可能需要JSON callback(还有一个Yaml,XMPP和a whole list of them)。启用它们的说明在the fine manual中,但是非常简短的版本是在调用ansible-playbook时仅定义一个环境变量:

env ANSIBLE_STDOUT_CALLBACK=json ansible-playbook ...

(如果您只想运行单个任务,它也可以与ansible一起使用