03-09-2023 07:09 AM - edited 03-10-2023 02:22 AM
Hi, new to Ansible
I have the following working config to get the show run and sh ip ospf nei and put it in a file.
However, some routers don't have ospf neighbors, in that case I would like nothing added in the file. Now we get an "undefined variable" error. Which is logical, as there is no output from sh ip ospf nei.
Is there a way to ignore the fact that there are no ospf neighbors and only add the show run to the file?
Another small question: Is it possible to send a few returns so the output of sh run and sh ip ospf nei has a few lines of white space?
---
- name: Show run and sh ip ospf nei
hosts: Test
gather_facts: false
tasks:
- name: Show Commands
ignore_errors: true
ios_command:
commands:
- sh run
- sh ip ospf nei
register: output
- name: Save output to file
copy:
content: "{{ output.stdout | replace('\\n', '\n') }}"
dest: "show-output/{{ inventory_hostname }}.ios"
03-10-2023 04:52 AM - edited 03-10-2023 05:08 AM
Hello @quadrabe
Try this:
---
- name: Show run and sh ip ospf nei
hosts: Test
gather_facts: false
tasks:
- name: Show Commands
ignore_errors: true
ios_command:
commands:
- sh run
- sh ip ospf nei
register: output
- name: Save output to file
copy:
content: |-
{{ output.stdout | replace('\\n', '\n') }}
{% if output.stdout_lines[1] is defined %}
{{ '\n\n' }}
{% endif %}
{{ output.stdout_lines[2:] | join('\n') | default('') | replace('\\n', '\n') }}
dest: "show-output/{{ inventory_hostname }}.ios"
In this script we check if the second line of the output is defined and add two newline characters to separate the show run output from the OSPF neighbor output. Then, we use the ""join" filter to join the OSPF neighbor output lines with newline characters, and we use the "default" filter to set the output to an empty string if it is undefined. Finally, we replace any escaped newline characters with actual newline characters.
Also, you can add whitespace to the output by adding newline characters as shown in the playbook above. The "{{ '\n\n' }}" line adds two newline characters to separate the show run output from the OSPF neighbor output.
03-10-2023 06:05 AM
Hi
This looks promising but I guess I'm missing something.
I get the following error
The offending line appears to be:
{{ output.stdout | replace('\\n', '\n') }}
{% if output.stdout_lines[1] is defined %}
^ here
---
- name: Show run and sh ip ospf nei
hosts: Test
gather_facts: false
tasks:
- name: Show Commands
#ignore_errors: true
ios_command:
commands:
- sh run
- sh ip ospf nei
register: output
- name: Save output to file
copy:
content: |-
{{ output.stdout | replace('\\n', '\n') }}
{% if output.stdout_lines[1] is defined %}
{{ '\n\n' }}
{% endif %}
{{ output.stdout_lines[2:] | join('\n') | default('') | replace('\\n', '\n') }}
dest: "show-output/{{ inventory_hostname }}.ios"
03-10-2023 06:46 AM
Ok @quadrabe
Try this:
---
- name: Show run and sh ip ospf nei
hosts: Test
gather_facts: false
tasks:
- name: Show Commands
ignore_errors: true
ios_command:
commands:
- sh run
- sh ip ospf nei
register: output
- name: Save output to file
copy:
content: |-
{{ output.stdout | replace('\n', '\n\n') }}
{% if output.stdout_lines[1] is defined and output.stdout_lines[1] != '' %}
{{ '\n\n' }}
{% endif %}
{{ output.stdout_lines[2:] | join('\n') | default('') | replace('\n', '\n') }}
dest: "show-output/{{ inventory_hostname }}.ios"
vars:
sh_ip_ospf_nei: "{{ output.stdout_lines[1] | default('') }}"
03-10-2023 09:06 AM
Hi
I get the same error. It's a syntax error, not a function error.
03-13-2023 01:42 AM
Hello @quadrabe
this one is ok for me, I hope for you too:
- name: Show run and sh ip ospf nei
hosts: Test
gather_facts: false
tasks:
- name: Show Commands
ios_command:
commands:
- sh run
- sh ip ospf nei
register: output
ignore_errors: true
- name: Save output to file
copy:
content: |-
{% if 'stdout_lines' in output and output.stdout_lines[1] is defined %}
{{ output.stdout_lines[0] }}
{{ output.stdout_lines[1] }}
{% else %}
{{ output.stdout_lines[0] }}
{% endif %}
dest: "show-output/{{ inventory_hostname }}.ios"
03-13-2023 04:04 AM - edited 03-13-2023 06:30 AM
Okay, apparently I had an indentation error.
That's fixed now and the playbook runs.
However, for the routers that don't understand the "sh ip ospf nei" line we get the following error.
fatal: [switch]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/etc/ansible/playbooks/playbook.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Save output to file\n ^ here\n"}
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide