#ansible
#ansible
Вопрос:
У меня есть Ansible playbook, который обращается к нескольким устройствам Cisco, собирает результаты команд «показать» от каждого из них и впоследствии создает отдельные файлы, содержащие этот вывод. Затем 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
Комментарии:
1. Конечно, перепроверьте содержимое каталогов
/etc/ansible/YAML_Files/Config-Change-Logs/
на удаленных хостах.
Ответ №1:
Проблема с исходным файлом YAML заключалась в том, что конечная задача (с именем «Объединить все файлы в один») создавалась повторно, поскольку она находилась в разделе «хосты: все».
Создавая еще один блок в сборнике воспроизведения, применяя этот блок только к «host: localhost» и перемещая задачу в этот блок, игра для создания MergedFile.txt был воспроизведен только один раз и создайте желаемый совокупный файл без всех дублированных записей.
Окончательный (рабочий) Сборник пьес:
---
-
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