Ansible:将多个文件合并到单个文件时,文本重复过多

Ansible:将多个文件合并到单个文件时,文本重复过多,ansible,Ansible,我有一个Ansible playbook,它可以接触到几个Cisco设备,从每个设备收集“show”命令的结果,然后创建包含该输出的单个文件。playbook然后将所有这些文件合并到一个文件中 然而,单个合并文件中有大量重复,我不知道为什么。有人能帮我找出如何防止这种重复吗?谢谢 剧本: --- - name: Lets Learn MORE YAML hosts: all gather_facts: no tasks: - name: Sending a simp

我有一个Ansible playbook,它可以接触到几个Cisco设备,从每个设备收集“show”命令的结果,然后创建包含该输出的单个文件。playbook然后将所有这些文件合并到一个文件中

然而,单个合并文件中有大量重复,我不知道为什么。有人能帮我找出如何防止这种重复吗?谢谢

剧本

---
- 
  name: Lets Learn MORE YAML
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show ip interface brief
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
---
- 
  name: Aggregating Output From Network Devices
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show configuration history
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

- 

  name: Merging All Output Into Single File
  hosts: localhost
  gather_facts: no

  tasks:

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt
结果(合并)文件

---
- 
  name: Lets Learn MORE YAML
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show ip interface brief
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
---
- 
  name: Aggregating Output From Network Devices
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show configuration history
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

- 

  name: Merging All Output Into Single File
  hosts: localhost
  gather_facts: no

  tasks:

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt

原始YAML文件的问题在于,最终任务(名为“将所有文件合并为一个”)是重复创建的,因为它位于“hosts:All”部分下

通过在playbook中创建另一个块,使该块仅适用于“host:localhost”并将任务移动到该块中,创建MergedFile.txt的播放仅播放一次,并创建所需的聚合文件,而不包含所有重复条目

最终(工作)剧本

---
- 
  name: Lets Learn MORE YAML
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show ip interface brief
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
---
- 
  name: Aggregating Output From Network Devices
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show configuration history
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

- 

  name: Merging All Output Into Single File
  hosts: localhost
  gather_facts: no

  tasks:

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt

原始YAML文件的问题在于,最终任务(名为“将所有文件合并为一个”)是重复创建的,因为它位于“hosts:All”部分下

通过在playbook中创建另一个块,使该块仅适用于“host:localhost”并将任务移动到该块中,创建MergedFile.txt的播放仅播放一次,并创建所需的聚合文件,而不包含所有重复条目

最终(工作)剧本

---
- 
  name: Lets Learn MORE YAML
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show ip interface brief
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"CSR-1"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       10.21.21.11     YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       211.11.11.11    YES manual up                    up      
GigabitEthernet6       unassigned      YES unset  administratively down down    
GigabitEthernet7       unassigned      YES unset  administratively down down    
GigabitEthernet8       unassigned      YES unset  administratively down down    
GigabitEthernet9       unassigned      YES unset  administratively down down    
GigabitEthernet10      unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
# BEGIN ANSIBLE MANAGED BLOCK
""
"SW-2"
# END ANSIBLE MANAGED BLOCK
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0     unassigned      YES unset  administratively down down    
GigabitEthernet0/1     unassigned      YES unset  administratively down down    
GigabitEthernet0/2     unassigned      YES unset  administratively down down    
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet1/0     unassigned      YES unset  administratively down down    
GigabitEthernet1/1     unassigned      YES unset  administratively down down    
GigabitEthernet1/2     unassigned      YES unset  administratively down down    
GigabitEthernet1/3     unassigned      YES unset  administratively down down    
GigabitEthernet2/0     unassigned      YES unset  administratively down down    
GigabitEthernet2/1     unassigned      YES unset  up                    up      
Vlan123                123.123.123.2   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r1"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         unassigned      YES unset  administratively down down    
GigabitEthernet0/1         123.123.123.1   YES manual up                    up      
GigabitEthernet0/2         10.12.12.1      YES manual up                    up      
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES manual administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         unassigned      YES unset  administratively down down    
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down    
Loopback0                  111.111.111.1   YES manual up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r2"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         10.22.22.2      YES manual up                    up      
GigabitEthernet0/1         10.21.21.2      YES manual up                    up      
GigabitEthernet0/2         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/3         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/4         10.12.12.2      YES manual up                    up      
GigabitEthernet0/5         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/6         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/7         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/8         unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/9         unassigned      YES NVRAM  administratively down down    
Loopback0                  222.222.222.222 YES TFTP   up                    up
# BEGIN ANSIBLE MANAGED BLOCK
""
"r3"
# END ANSIBLE MANAGED BLOCK
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         123.123.123.3   YES manual up                    up      
GigabitEthernet0/1         unassigned      YES unset  administratively down down    
GigabitEthernet0/2         unassigned      YES unset  administratively down down    
GigabitEthernet0/3         unassigned      YES unset  administratively down down    
GigabitEthernet0/4         unassigned      YES unset  administratively down down    
GigabitEthernet0/5         unassigned      YES unset  administratively down down    
GigabitEthernet0/6         32.32.32.3      YES manual up                    up      
GigabitEthernet0/7         unassigned      YES unset  administratively down down    
GigabitEthernet0/8         unassigned      YES unset  administratively down down    
GigabitEthernet0/9         unassigned      YES unset  administratively down down
---
- 
  name: Aggregating Output From Network Devices
  hosts: all
  gather_facts: no

  tasks:

    - name: Sending a simple Cisco IOS command
      cisco.ios.ios_command:
        commands: 
          - show configuration history
      register: Bob


    - debug: var=Bob.stdout_lines


    - name: Send output to file
      copy: content="{{Bob.stdout[0]}}" dest=/etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt

    - name: Prepend line to file
      blockinfile:
          path: /etc/ansible/YAML_Files/Config-Change-Logs/{{inventory_hostname}}.txt
          block: |
            ""
            "{{inventory_hostname}}"
          insertbefore: BOF

- 

  name: Merging All Output Into Single File
  hosts: localhost
  gather_facts: no

  tasks:

    - name: Merge All Files Into One
      assemble:
        src: /etc/ansible/YAML_Files/Config-Change-Logs/
        dest: /etc/ansible/YAML_Files/Config-Change-Logs/MergedFile.txt

当然,在远程主机上仔细检查目录
/etc/ansible/YAML_Files/Config Change Logs/
的内容。当然,在远程主机上仔细检查目录
/etc/ansible/YAML_Files/Config Change Logs/
的内容。