首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible:将多个文件合并为单个文件时文本的过度重复

Ansible:将多个文件合并为单个文件时文本的过度重复
EN

Stack Overflow用户
提问于 2020-12-02 19:40:14
回答 1查看 45关注 0票数 0

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

但是,合并后的单个文件中有很多重复,我不知道为什么。有人能帮我找出如何防止这种重复吗?谢谢!

Playbook

代码语言:javascript
复制
---
- 
  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

生成(合并)文件

代码语言:javascript
复制
# 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
EN

回答 1

Stack Overflow用户

发布于 2020-12-02 21:47:42

原始YAML文件的问题是,最后一个任务(命名为“合并所有文件为一个”)是重复创建的,因为它位于“host: all”部分下。

通过在剧本中创建另一个块,使该块只应用于"host: localhost"并将任务移动到该块中,用于创建MergedFile.txt的播放只播放一次,并创建所需的聚合文件,而不需要所有重复的条目。

Final (工作) Playbook

代码语言:javascript
复制
---
- 
  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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65115023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档