Unable to run sh run playbook on cisco_csr

I can run other sh commands like sh version, sh ip int brief but the sh run doesn’t work on the cisco CSR. What I am missing here?

swapna@jumphost1:~/ansible$ ansible-playbook shrun.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [Backup show run (enable mode commands)] **********************************************************************************************************************************************************
[WARNING]: While constructing a mapping from /home/swapna/ansible/host_vars/cisco_csr.yml, line 1, column 1, found a duplicate dict key (ansible_host). Using last defined value only.
[WARNING]: ansible-pylibssh not installed, falling back to paramiko

TASK [run enable level commands] ***********************************************************************************************************************************************************************
fatal: [cisco_csr]: FAILED! => {“changed”: false, “msg”: “show running-config\r\nshow running-config\r\n ^\r\n% Invalid input detected at ‘^’ marker.\r\n\r\nCSR2>”}

PLAY RECAP *********************************************************************************************************************************************************************************************
cisco_csr : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

swapna@jumphost1:~/ansible$

swapna@jumphost1:~/ansible$ cat shrun.yml

  • name: Backup show run (enable mode commands)
    hosts: all
    gather_facts: false
    connection: local

    tasks:

    • name: run enable level commands
      ios_command:
      commands:
      - show running-config

      register: print_output

    • debug: var=print_output.stdout_lines
      swapna@jumphost1:~/ansible$

@chinmasw Hello.

The user who login to devices does not have privilege permission.

Maybe you can execute show version not show running-config.

You should use a privilege user or privilege escalation to execute show running-config.

See also: IOS Platform Options — Ansible Documentation

1 Like

Thank you that worked.

1 Like

I am trying to backup the config and save it in backups folder, it is falling here “msg”: “can not use content with a dir as dest”

swapna@jumphost1:~/ansible$ ansible-playbook backup.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [cisco_csr] *****************************************************************************************************************************************************************************************************************
[WARNING]: ansible-pylibssh not installed, falling back to paramiko

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************
ok: [cisco_csr]

TASK [Get ansible dat/time facts] ************************************************************************************************************************************************************************************************
ok: [cisco_csr]

TASK [Store DTG as fact] *********************************************************************************************************************************************************************************************************
ok: [cisco_csr]

TASK [Create Directory 2024-02-19] ***********************************************************************************************************************************************************************************************
changed: [cisco_csr]

PLAY [cisco_csr] *****************************************************************************************************************************************************************************************************************

TASK [show run on device] ********************************************************************************************************************************************************************************************************
ok: [cisco_csr]

PLAY [cisco_csr] *****************************************************************************************************************************************************************************************************************

TASK [Save output to ~/ansible/backups] ******************************************************************************************************************************************************************************************
fatal: [cisco_csr]: FAILED! => {“changed”: false, “msg”: “can not use content with a dir as dest”}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************
cisco_csr : ok=5 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

swapna@jumphost1:~/ansible$ cd backups
swapna@jumphost1:~/ansible/backups$ ls
2024-02-19
swapna@jumphost1:~/ansible/backups$ cat 2024-02-19/
cat: 2024-02-19/: Is a directory
swapna@jumphost1:~/ansible/backups$ cd 2024-02-19/
swapna@jumphost1:~/ansible/backups/2024-02-19$ ls
swapna@jumphost1:~/ansible/backups/2024-02-19$ ls
swapna@jumphost1:~/ansible/backups/2024-02-19$

swapna@jumphost1:~/ansible$ cat backup.yml

Playbook to get system time and append it to backup files


  • hosts: cisco_csr

    tasks:

    • name: Get ansible dat/time facts
      setup:
      filter: “ansible_date_time”
      gather_subset: “!all”

    • name: Store DTG as fact
      set_fact:
      DTG: “{{ ansible_date_time.date }}”

    • name: Create Directory {{hostvars.cisco_csr.DTG}}
      file:
      path: ~/ansible/backups/{{hostvars.cisco_csr.DTG}}
      state: directory
      run_once: true

  • hosts: cisco_csr
    gather_facts: false

    tasks:

    • name: show run on device
      ios_command:
      commands:
      - show running-config
      register: config
  • hosts: cisco_csr
    gather_facts: false

    tasks:

    • name: Save output to ~/ansible/backups
      copy:
      content: “{{config.stdout[0]}}”
      dest: “~/ansible/backups/{{hostvars.cisco_csr.DTG}}”
      swapna@jumphost1:~/ansible$

@chinmasw

As per the error message can not use content with a dir as dest, you should specify file path to dest option of copy module.

1 Like

@chinmasw

For example, how about the following.

- name: Save output to ~/ansible/backups
  copy:
    content: "{{ config.stdout[0] }}"
    dest: "~/ansible/backups/{{ hostvars.cisco_csr.DTG }}/show_running-config.txt"

In this case the dest option needs a file path.

1 Like

Your Awesome it works. As a extension to the playbook, I am adding some of the show commands output. l guess I need to use some template like j2 for storing multiple sh commands output as the .txt you can only save 1 show command output. Is there a way out of not using j2?

1 Like

@chinmasw

There are two ways to store the results of multiple show commands.

  1. Store one result to per file.
    like show_running-config.txt, show_version.txt and so on.

  2. Store multiple results to one file.

Which way do you like?

1 Like

Option 2 storing in 1 file with multiple results.

@chinmasw

There are several ways to do it… How about this one?

  tasks:
    - name: show run on device
      ios_command:
        commands:
          - show running-config
          - show version
      register: config

    - name: Save output to ~/ansible/backups
      blockinfile:
        path: "~/ansible/backups/{{ hostvars.cisco_csr.DTG }}/show_results.txt"
        block: "{{ item }}"
        marker: "======================================" # separator
        create: true
      loop: "{{ config.stdout }}"
      loop_control:
        label: " " # suppressing log output
1 Like

Works perfectly. Thank you for your help

@chinmasw My pleasure.
Could you mark my answer as solution to explicit closing this topic?

1 Like

Yes, how do I close this conversation ?

It was marked as solution this comment, so this topic will close a month after the last reply.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.