我的剧本
- name: create HTML report
template:
src: report.j2
dest: /cktreport.html
delegate_to: localhost
run_once: true
报告.j2
<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<tr>
<th>HOSTNAME</th>
<th>PORT</th>
<th>CKTID</th>
</tr>
</thead>
<tbody>
{% for item in output.parsed %}
<tr>
{% if 'CID' in item.DESCRIP %}
<td>{{inventory_hostname}}</td>
<td>{{item.PORT}}</td>
<td>{{item.DESCRIP}}</td>
{%elif 'ckid' in item.DESCRIP %}
<td>{{inventory_hostname}}</td>
<td>{{item.PORT}}</td>
<td>{{item.DESCRIP}}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
'output.parsed' 有以下信息
TASK [debug] *******************************************************************
ok: [host-1] => {
"msg": [
{
"DESCRIP": "CID: xxxx",
"PORT": "Gi0/0/0",
"PROTOCOL": "up",
"STATUS": "up"
},
{
"DESCRIP": "",
"PORT": "Gi0/0/1",
"PROTOCOL": "up",
"STATUS": "up"
},
{
"DESCRIP": "",
"PORT": "Gi0/0/2",
"PROTOCOL": "down",
"STATUS": "down"
},
{
"DESCRIP": "ckid: XXXX",
"PORT": "Gi0/0/3",
"PROTOCOL": "up",
"STATUS": "up"
}
]
}
ok: [host-2] => {
"msg": [
{
"DESCRIP": "CID: xxxx",
"PORT": "Gi0/0/1",
"PROTOCOL": "up",
"STATUS": "up"
},
{
"DESCRIP": "",
"PORT": "Gi0/0/6",
"PROTOCOL": "up",
"STATUS": "up"
},
{
"DESCRIP": "",
"PORT": "Gi0/0/7",
"PROTOCOL": "down",
"STATUS": "down"
},
{
"DESCRIP": "ckid: XXXX",
"PORT": "Gi0/0/8",
"PROTOCOL": "up",
"STATUS": "up"
}
]
}
我遇到的问题是 - 当我运行 playbook 时,仅为 host-1 生成 cktreport.html,我有多个主机,如何生成单个/一个cktreport.html 使其包含所有主机信息。
我知道我需要修改 jinja2 模板,但不确定要使用什么逻辑。