forked from nickrusso42518/racc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
racc_playbook.yml
89 lines (80 loc) · 3.92 KB
/
racc_playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
# This play performs the work to collect data from network devices in the
# network and store it in individual files.
- name: "Get information from network devices"
hosts: "all"
tasks:
# Perform data validation and timestamp/directory setup
- name: "INCLUDE >> Error checking and log setup"
ansible.builtin.include_tasks: "tasks/pre_check.yml"
# Include the proper task list based on the device type.
# All tasks will register outputs to 'CLI_OUTPUT' which
# must have a 'stdout_lines' 2D array of strings.
- name: "SYS >> Include tasks for various network device types"
ansible.builtin.include_tasks: "tasks/{{ ansible_network_os }}.yml"
when: "not ci_test"
# Include the CI testing task which builds mock data in the
# correct format that the jinja2 template expects. This ensures
# that the CLI_OUTPUT.results and command_list sequences
# are always the same length and remain in sync.
- name: "SYS >> Include tasks to generate mock output for CI"
ansible.builtin.include_tasks: "tasks/ci_test.yml"
when: "ci_test"
# All task files should have defined the CLI_OUTPUT variable which
# contains the CLI output lines from each command. The play cannot
# continue unless this variable has been validated, which includes
# ensuring it exists, is a list, and has the proper length for
# future iteration.
- name: "SYS >> Sanity check; ensure CLI_OUTPUT is valid"
ansible.builtin.assert:
that:
- "CLI_OUTPUT is defined and CLI_OUTPUT"
- "CLI_OUTPUT.stdout_lines | length == command_list | length"
msg: "CLI_OUTPUT or its sub data structures are invalid, see debugs"
# Iteratively include file to process CLI output and optional hashes
- name: "SYS >> Include tasks to perform output file functions"
ansible.builtin.include_tasks: "tasks/outputs.yml"
vars:
FILENAME: "{{ item.0 | replace(' ', '_') | replace('/', '') }}.txt"
loop: "{{ command_list | zip(CLI_OUTPUT.stdout_lines) | list }}"
loop_control:
label: "{{ item.0 }}"
# The hashes are already in hash,file CSV format, so join them with a
# newline and store them on disk.
- name: "SYS >> Write {{ hash_type }} hashes to disk"
ansible.builtin.copy:
content: "{{ HASHLIST | join(newline_sequence) }}{{ newline_sequence }}"
dest: "{{ OUTPUT_DIR }}/{{ hash_type }}_hashes.txt"
mode: "0444"
when: "hash_type"
- name: "BLOCK >> Post-processing on control machine"
block:
# Store the string file path of the future archive to simplify
# variable references later in the process.
- name: "SYS >> Store archive path/filename"
ansible.builtin.set_fact:
ARCH_FQDN: "archives/commands_{{ DTG }}.{{ archive_format }}"
# Zip up all of the individual text files into an archive and
# optionally remove the source files if 'remove_files' is true.
- name: "SYS >> Archive files into a {{ archive_format }}"
community.general.archive:
dest: "{{ ARCH_FQDN }}"
path: "files/*{{ DTG }}"
format: "{{ archive_format }}"
remove: "{{ remove_files }}"
mode: "0444"
# Users can optionally copy the ZIP bundle to an SCP server by copying
# the SCP command written to stdout for simplicity.
- name: "SYS >> Copy the command below to SCP the archive off-box"
ansible.builtin.debug:
msg: "scp {{ ARCH_FQDN }} {{ scp.user }}@{{ scp.host }}:/racc/"
run_once: true
delegate_to: "localhost"
# Final cleanup; delete empty directories from files/ when Boolean
# remove_files is true. The archive module does not do this automatically.
- name: "SYS >> Remove network device directory? {{ remove_files }}"
ansible.builtin.file:
path: "{{ OUTPUT_DIR }}"
state: "absent"
when: "remove_files"
...