0

I'm trying to use Ansible to hit a bunch (100+) Cisco Catalyst switches and check if they have a certain line card installed. Via SSH, this can be done with the "sh mod" command. I want to parse the output of that command in a playbook and then show the output of the command if a certain string matches. Right now with the playbook below I get the following error:

fatal: [redacted-hostname]: FAILED! => {"failed": true, "msg": "The conditional check 'showmod | search(\"4548\")' failed. The error was: Unexpected templating type error occurred on ({% if showmod | search(\"4548\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/etc/ansible/playbooks/linecard-4548.yaml': line 22, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug: \"msg='4548 Card Found'\"\n ^ here\n"}

Current playbook code:

---
- hosts: redacted-hostname
  gather_facts: yes
  connection: local

  tasks:

  - name: SYS | Define provider
    set_fact:
      provider:
        host: "{{ inventory_hostname }}" 
        username: redacted-user
        password: redacted-password

  - name: IOS | Get Module List
    ios_command:
      provider: "{{ provider }}"
      commands:
          - sh mod | inc 4548
    register: showmod

  - debug: "msg='4548 Card Found'"
    when: showmod.stdout | search("/4548/")  

I've tried the when in the debug with and without the .stdout to no avail. I've done some research and the error I'm getting usually occurs when, in my case, showmod is undefined but it definitely is. If I replace the debug with the following snippet, the playbook runs fine but of course it'll print the output for every switch which isn't what I want.

  - name: IOS | Show Output
    debug:
      var: showmod

Any suggestions?

Ross
  • 173
  • 1
  • 2
  • 12

1 Answers1

3

ios_command returns stdout as list and stdout_lines as list of lists (whereas command module return stdout as string and stdout_lines as list).

So in your case, you may want to try:

- debug: "msg='4548 Card Found'"
  when: showmod.stdout | join(" ") | search("/4548/")  
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193