cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
942
Views
0
Helpful
6
Replies

Ansible ignore command if not supported

quadrabe
Level 1
Level 1

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"

 

6 Replies 6

M02@rt37
VIP
VIP

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.

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

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"

 

 

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('') }}"

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

Hi

I get the same error. It's a syntax error, not a function error.

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"

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

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"}

 

 

 

Review Cisco Networking for a $25 gift card