首页 > 解决方案 > Ansible 输出仅将一个设备写入文件

问题描述

我是 Ansible 的新手,并试图从 2 个测试设备中获取 ios_facts。正如您在输出中看到的那样,Ansible 会遍历两个设备,但只将第一个设备写入目标文件。我很确定需要得到我正在寻找的输出。有人可以告诉我还需要什么或将我推向正确的方向吗?非常感谢您提前。

主机文件

[LocationA]
1.1.1.1
2.2.2.2

神社模板:

{% for host in groups['LocationA'] %}
hostname: {{ ansible_net_hostname }}
version: {{ ansible_net_version }}
File_system: {{ ansible_net_filesystems_info }}
Neighbors: {{ansible_net_neighbors }} {{ansible_net_model}}
model: {{ansible_net_model}}
{% endfor %}

剧本-

 - name: Gathering IOS facts
  hosts: LocationA
  connection: network_cli
  gather_facts: no

  tasks:
  - name: Gather IOS device facts
    ios_facts:
      gather_subset: all

#  - name: View ansible fact keys
#    debug:
#      var: ansible_facts.keys()

  - name: output to a file
    ansible.builtin.template:
      src: ./templates/md5-result.j2
      dest: ./devices/switch-facts.txt
      newline_sequence: '\r\n'

输出-

hostname: 1.1.1.1
version: 16.12.04
File_system: {'flash:': {'spacetotal_kb': 1583952.0, 'spacefree_kb': 557316.0}}
Neighbors: {} WS-C3850-48T
model: WS-C3850-48T
hostname: 1.1.1.1
version: 16.12.04
File_system: {'flash:': {'spacetotal_kb': 1583952.0, 'spacefree_kb': 557316.0}}
Neighbors: {} WS-C3850-48T
model: WS-C3850-48T

标签: ansibleansible-template

解决方案


推荐阅读