我有一个playbook输出show命令,我试图让它将输出发送到人类可读格式的文件。
这是我的代码:
- name: Run a set of commands
junos_command:
commands: [ 'show route summary', 'show bgp sum' ]
output: text
provider: "{{ netconf }}"
register: showcmds
- name: copy content
local_action: copy content={{ showcmds }} dest=backups/{{ show_file }}
- name: display show commands
debug: var=showcmds.stdout_lines
但文件看起来像这样:
[gary.rubel@sl7jump2 backups]$ more car1.txt
{"failed": false, "changed": false, "stdout": ["Autonomous system number: 3561\nRouter ID: 90.90.90.70\n\ninet.0: 1
1 destinations, 11 routes (11 active, 0 holddown, 0 hidden)\nLimit/Threshold: 16384/16384 destinations\n
Direct: 2 routes, 2 active\n Local: 5 routes, 5 active\n Static:
3 routes, 3 active\n IGMP: 1 routes, 1 active\n\ncoin-a.inet.0: 106 destinations
, 202 routes (106 active, 0 holddown, 0 hidden)\n Direct: 5 routes, 5 active\n
Local: 8 routes, 8 active\n BGP: 173 routes, 77 active\n IS-IS:
13 routes, 13 active\n IGMP: 1 routes, 1 active\n PIM: 2 routes,
2 active\n\ncoin-a.inet.1: 43 destinations, 43 routes (43 active, 0 holddown, 0 hidden)\n Multicast
: 43 routes, 43 active\n\niso.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)\n D
irect: 1 routes, 1 active\n\ncoin-a.iso.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)\n
Direct: 1 routes, 1 active\n\ncoin-a.inet6.0: 2 destinations, 2 routes (2 active, 0 holddown,
0 hidden)\n PIM: 2 routes, 2 active\n\ncoin-a.inet6.1: 1 destinations, 1 routes (1 active
, 0 holddown, 0 hidden)\n Multicast: 1 routes, 1 active", "Groups: 5 Peers: 6 Down peers: 4\nPe
er AS InPkt OutPkt OutQ Flaps Last Up/Dwn State|#Active/Received/Accepted/Damped.
..\n10.191.254.241 55061 0 0 0 0 3w5d6h Idle \n166.49.31.54 79
86 0 0 0 0 3w5d6h Idle \n172.16.30.30 3561 32013 31956
0 31 3d 8:14:02 Establ\n coin-a.inet.0: 66/82/82/0\n172.16.50.50 3561 240667 251117
0 0 3w5d6h Establ\n coin-a.inet.0: 11/91/91/0\n172.16.51.51 64800 0 0
0 0 3w5d6h Active\n198.168.10.2 7986 0 0 0 0 3w5d6h Idle"
], "stdout_lines": [["Autonomous system number: 3561", "Router ID: 90.90.90.70", "", "inet.0: 11 destinations, 11 r
outes (11 active, 0 holddown, 0 hidden)", "Limit/Threshold: 16384/16384 destinations", " Direct:
2 routes, 2 active", " Local: 5 routes, 5 active", " Static: 3 route
s, 3 active", " IGMP: 1 routes, 1 active", "", "coin-a.inet.0: 106 destinations, 202
routes (106 active, 0 holddown, 0 hidden)", " Direct: 5 routes, 5 active", " L
ocal: 8 routes, 8 active", " BGP: 173 routes, 77 active", " IS-IS:
13 routes, 13 active", " IGMP: 1 routes, 1 active", " PIM: 2 r
outes, 2 active", "", "coin-a.inet.1: 43 destinations, 43 routes (43 active, 0 holddown, 0 hidden)", "
Multicast: 43 routes, 43 active", "", "iso.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)"
, " Direct: 1 routes, 1 active", "", "coin-a.iso.0: 1 destinations, 1 routes (1 active, 0 ho
lddown, 0 hidden)", " Direct: 1 routes, 1 active", "", "coin-a.inet6.0: 2 destinations, 2 ro
utes (2 active, 0 holddown, 0 hidden)", " PIM: 2 routes, 2 active", "", "coin-a.inet6.1:
1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)", " Multicast: 1 routes, 1 active"],
["Groups: 5 Peers: 6 Down peers: 4", "Peer AS InPkt OutPkt OutQ Flaps Last Up/Dwn
State|#Active/Received/Accepted/Damped...", "10.191.254.241 55061 0 0 0 0
3w5d6h Idle ", "166.49.31.54 7986 0 0 0 0 3w5d6h Idle ", "172.16.3
0.30 3561 32013 31956 0 31 3d 8:14:02 Establ", " coin-a.inet.0: 66/82/82/0", "172.
16.50.50 3561 240667 251117 0 0 3w5d6h Establ", " coin-a.inet.0: 11/91/91/0", "
172.16.51.51 64800 0 0 0 0 3w5d6h Active", "198.168.10.2 7986
0 0 0 0 3w5d6h Idle"]]}
答案 0 :(得分:0)
您是否尝试过将“stdout_lines”直接推送到文件中?我看到你把那些作为调试而不是吐出到文件中。如果您想要解释断路器,通常就是这样。 (而stdout用于字符串输出)
- name: copy content
local_action: copy content={{ showcmds.stdout_lines }} dest=backups/{{ show_file }}
如果我误读了这个并且您实际上想要以“更漂亮”的方式发送整个输出,您可以将注册变量视为字典并逐行发送输出。
- name: copy content
local_action: lineinfile line={{ item }} path=backups/{{ show_file }}
with_dict: {{ showcmds }}
我希望这会有所帮助。 :)
答案 1 :(得分:0)
尝试将输出设置为" json"在您的第一个任务中,它看起来像这样:output: json
。
我在使用json时使用的一个有用的工具是" prettyjson"包。您将需要Node来安装它。
https://www.npmjs.com/package/prettyjson
https://nodejs.org/en/download/
然后您可以运行以下命令:
prettyjson car1.json
more car1.json | prettyjson
希望这有帮助!