Information Gathering: Continued
Learn to output ios_facts about the devices to a JSON file using Ansible playbooks.
We'll cover the following...
Explanation of the ios_facts.yml
playbook
---- hosts: CAMPUStasks:- name: IOS Facts on CAMPUSregister: ios_facts_outputios_facts:provider: "{{ ioscli }}"- copy:content="{{ ios_facts_output | to_nice_json }}"dest="./results/{{ inventory_hostname }}_ios_facts_output.json"
First, specify the scope of the play (hosts
) and specify the tasks.
The task name is IOS Facts on CAMPUS
, which will display during runtime. Register the output to the variable named ios_facts_output
.
The provider (credentials) can be found in the variable {{ ioscli }}
.
Copy (copy
) the content (content
) of the variable
ios_facts_output
and apply the filter (|
) to_nice_json
to the
output. Send this output (dest
) to a file called {{ inventory_hostname }}
, a variable that will be replaced at run time
with each device hostname. Finally, append and create the file with
_ios_facts_output
as a JavaScript Object Notation (JSON) file
in the JSON format.
Review the check mode output from the playbook that executes against all
hosts listed in the [CAMPUS]
group (referencing hosts
).
The task [IOS Facts on CAMPUS]
is executed, serially, against the
hosts. This task has run successfully (for example, ok: [Core]
)
on each host. Confirm that there are no errors present in the playbook and the
commands executed successfully.
Next, the task [copy]
executes on each host creating (for example, changed: [CORE]
) new output files for each host.
The play recap shows the status of each device (ok=2
), how many
changes will be made (changed=1
...