在这里编码菜鸟。 我编写了一个Ansible剧本,其中包含主机列表,检索ios_facts并将事实以JSON格式写入每个主机的1个文件中。
为了为网络中的每个设备创建一个文件报告,我希望能够过滤掉特定的事实并编写一个json文件。每条记录都需要一个“收据”,并且该数字对于json数组中的每条新记录都应递增。
我应该将其作为我的剧本的一部分来解决,还是编写另一个脚本来混搭ansible剧本的输出?
感谢您的协助。
谢谢 李
Ansible Playbook:
- hosts: ALL
connection: network_cli
become: yes
become_method: enable
vars:
tasks:
- name: ssh facts
register: iosfacts_out
ios_facts:
# provider: "{{ credentials }}"
# Collect all facts from the device
# gather_subset: all
- copy: content="{{ iosfacts_out | to_nice_json }}" dest="out/{{inventory_hostname}}_iosfacts.json"
所需的JSON结构在示例中列出了2个主机:
[
{
"STACKED_SERIAL": [
"SERIALNUMBER1"
],
"IP": [
"1.1.1.1"
],
"HOSTNAME": "hostname1",
"STACKED_MODELS": [ "stackedmodel1"
],
"VERSION": "ios_version1",
"recid": 1,
"MODEL": "model_number1",
"OS": "os_type",
"SERIAL": "serial_number1",
"LAST_FAILED": "no"
},
{
"STACKED_SERIAL": [ "SERIALNUMER"
],
"IP": [
"1.1.1.2",
"1.1.1.3"
],
"HOSTNAME": "hostname2",
"STACKED_MODELS": [ "stackedmodel2"
],
"VERSION": "ios_version2",
"recid": 2,
"MODEL": "ios_version2",
"OS": "os_type",
"SERIAL": "serial_number2",
"LAST_FAILED": "no"
}
]
我要使用的ansible剧本的输出json文件中的字段如下:
"ansible_net_hostname": "hostname",
"ansible_net_model": "model_number",
"ansible_net_serialnum": "serial_number",
"ansible_net_version": "ios_version"
"ansible_net_image": "image",
"ansible_net_all_ipv4_addresses": [
"10.1.2.3"
],
示例当前Ansible输出:
{
"ansible_facts": {
"ansible_net_all_ipv4_addresses": [
"10.10.20.48"
],
"ansible_net_all_ipv6_addresses": [
"3123:1:3:5::1",
"3123:1:3:5:250:56FF:FEBB:105E"
],
"ansible_net_filesystems": [
"bootflash:"
],
"ansible_net_gather_subset": [
"hardware",
"default",
"interfaces"
],
"ansible_net_hostname": "csr1000v",
"ansible_net_image": "bootflash:packages.conf",
"ansible_net_interfaces": {
"GigabitEthernet1": {
"bandwidth": 1000000,
"description": "MANAGEMENT INTERFACE - DON'T TOUCH ME",
"duplex": "Full",
"ipv4": [
{
"address": "10.10.20.48",
"subnet": "24"
}
],
"lineprotocol": "up ",
"macaddress": "0050.56bb.e14e",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
},
"GigabitEthernet2": {
"bandwidth": 1000000,
"description": "Network Interface",
"duplex": "Full",
"ipv4": [],
"ipv6": [
{
"address": "3123:1:3:5::1",
"subnet": "3123:1:3:5::/64"
},
{
"address": "3123:1:3:5:250:56FF:FEBB:105E",
"subnet": "3123:1:3:5::/64 [EUI]"
}
],
"lineprotocol": "up ",
"macaddress": "0050.56bb.105e",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
},
"GigabitEthernet3": {
"bandwidth": 1000000,
"description": "Network Interface",
"duplex": "Full",
"ipv4": [],
"lineprotocol": "down ",
"macaddress": "0050.56bb.2e4e",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "administratively down",
"type": "CSR vNIC"
}
},
"ansible_net_memfree_mb": 2070663,
"ansible_net_memtotal_mb": 2392443,
"ansible_net_model": "CSR1000V",
"ansible_net_neighbors": {
"null": [
{
"host": null,
"port": null
}
]
},
"ansible_net_serialnum": "9NR1X6ONBWR",
"ansible_net_version": "16.09.03"
},
"changed": false,
"failed": false
}
答案 0 :(得分:0)
为什么不编写Jinja2模板并将其模板化?