Network devices backup -cisco/dell

Hello Team,
below is my playbook ;

  • name: AUTOMATIC BACKUP OF RUNNING-CONFIG
    hosts: Core_Switch
    gather_facts: true
    connection: local

    tasks:

    • name: DISPLAYING THE RUNNING-CONFIG
      ios_command:
      commands:
      - show run
      timeout: 30
      host: “{{ ansible_host }}”
      username: xxxxx
      password: yyyyy
      register: config

    • name: Get Current Date
      local_action: command date +%Y-%b-%d
      register: date

    • name: Get Current Time
      local_action: command date +%H:%M
      register: time

    • name: save Output to /home/user1/Backups
      copy:
      content: “{{ config.stdout[0] }}”
      dest: “/home/user1/Backups/_{{ date.stdout }}at{{ time.stdout }}-{{ inventory_hostname }}.txt”

But when i run it i get this error;
FAILED! => {“changed”: false, “msg”: “Connection type local is not valid for this module”}

What could I be doing wrong?
Thanks in advance.

You cannot use both connection: local and local_action: at the same time. Not to mention that local_action: is deprecated in favor of delegate_to: localhost (which is more or less the same thing). Since all of these steps are running on the localhost, we can use connection: local as a default, and not redundantly delegate tasks to local when they already are.

For e.g:

- name: AUTOMATIC BACKUP OF RUNNING-CONFIG
  hosts: Core_Switch
  gather_facts: false
  connection: local

  tasks:
    - name: DISPLAYING THE RUNNING-CONFIG
      ios_command:
        commands:
          - show run
        timeout: 30
        host: “{{ ansible_host }}”
        username: xxxxx
        password: yyyyy
      register: config

    - name: Get Current Date
      command:
        cmd: date +%Y-%b-%d
      register: date

    - name: Get Current Time
      command:
        cmd: date +%H:%M
      register: time

    - name: Save Output to /home/user1/Backups
      copy:
        content: “{{ config.stdout[0] }}”
        dest: “/home/user1/Backups/_{{ date.stdout }}at{{ time.stdout }}-{{ inventory_hostname }}.txt”

FAILED! => {“changed”: false, “msg”: “Connection type ssh is not valid for this module”}

Getting above error.

Silly me, I left out connection: local at the top.

FAILED! => {“changed”: false, “msg”: “Connection type local is not valid for this module”}

Now getting this error:
am on ansible 2.14.9

Which task is giving you that error? I didn’t think local connections were unsupported anywhere.

Edit:

Regardless, you could try delegation instead of setting the connection type.

- name: AUTOMATIC BACKUP OF RUNNING-CONFIG
  hosts: Core_Switch
  gather_facts: false

  tasks:
    - name: DISPLAYING THE RUNNING-CONFIG
      ios_command:
        commands:
          - show run
        timeout: 30
        host: “{{ ansible_host }}”
        username: xxxxx
        password: yyyyy
      register: config
      delegate_to: localhost

    - name: Get Current Date
      command:
        cmd: date +%Y-%b-%d
      register: date
      delegate_to: localhost

    - name: Get Current Time
      command:
        cmd: date +%H:%M
      register: time
      delegate_to: localhost

    - name: Save Output to /home/user1/Backups
      copy:
        content: “{{ config.stdout[0] }}”
        dest: “/home/user1/Backups/_{{ date.stdout }}at{{ time.stdout }}-{{ inventory_hostname }}.txt”
      delegate_to: localhost

its now trying to ssh using root user of the linux.
How can i make it use local?

i mean credentials defined on the playbook?

What does your inventory look like? Delegating to localhost should definitely be using connection local, not your ssh connection or credentials…

Wait wait. I’m looking at the cisco.ios.ios_command module now, and there’s no host/username/password parameter for that. There are modules that do, and expect to be run as connection local while it establishes its own connection to the host separately, but that doesn’t appear to be the case here.

Unless you specified the username/password for your ios hosts in inventory, I would expect you to need to do something like this:
ansible-playbook ./this_play.yml -u <ios_username> -k # enter password for <ios_username>

And the ios_command module would pick up the credentials you’re using for the ios hosts, while the delegate_to tasks will just use your local “user1” permissions.

- name: AUTOMATIC BACKUP OF RUNNING-CONFIG
  hosts: Core_Switch
  gather_facts: false

  tasks:
    - name: DISPLAYING THE RUNNING-CONFIG
      ios_command:
        commands:
          - show run
      register: config

    - name: Get Current Date
      command:
        cmd: date +%Y-%b-%d
      register: date
      delegate_to: localhost

    - name: Get Current Time
      command:
        cmd: date +%H:%M
      register: time
      delegate_to: localhost

    - name: Save Output to /home/user1/Backups
      copy:
        content: “{{ config.stdout[0] }}”
        dest: “/home/user1/Backups/_{{ date.stdout }}at{{ time.stdout }}-{{ inventory_hostname }}.txt”
      delegate_to: localhost

Have configured creds on hosts config file.
Am i doing it wrong?
but still complain about module local not supported.
funny enough the same was running well on cent os, till i moved to rocky linux.

Did you remove connection: local this time? It would also be helpful if you show which task is failing.

As far as your inventory goes, I would expect something like this:

Core_Switch:
  ansible_connection: network_cli
  ansible_network_os: ios
  ansible_user: ansible
  ansible_password: cisco123

where your password at least is vault encrypted, if not the whole file, unless you just want to enter it on the command line everytime you run the playbook.

These connection credentials for your host can be saved as host_vars or group_vars.

I don’t think you do, but you shouldn’t have localhost defined anywhere in inventory (that can break the implicit localhost). I just wanted to mention it because I was getting suspicious about it before.

1 Like

@FELIXmu Hello,

The cisco.ios.ios_command module supports network_cli connection plugin not local.

So you need to specify ansible_connection variable to ansible.netcommon.network_cli (or connection directive).

The following is an example:

inventory.ini:

[Core_Switch]
ios01 ansible_host=192.168.x.x

group_vars/Core_Switch.yml:

---
ansible_network_os: cisco.ios.ios
ansible_connection: ansible.netcommon.network_cli
ansible_user: xxxxx
ansible_password: yyyyy

Playbook:

- name: AUTOMATIC BACKUP OF RUNNING-CONFIG
  hosts: Core_Switch
  gather_facts: false

  tasks:
    - name: DISPLAYING THE RUNNING-CONFIG
      ios_command:
        commands:
          - show run
      register: config

    - name: Get Current Date
      command:
        cmd: date +%Y-%b-%d
      register: date
      delegate_to: localhost

    - name: Get Current Time
      command:
        cmd: date +%H:%M
      register: time
      delegate_to: localhost

    - name: Save Output to /home/user1/Backups
      copy:
        content: “{{ config.stdout[0] }}”
        dest: "/home/user1/Backups{{ date.stdout }}at{{ time.stdout }}-{{ inventory_hostname }}.txt"
      delegate_to: localhost
1 Like

Sorry for the duplication.
Most of what I am saying is the same as @Denney-tech .

1 Like

Thank you for consolidating it into one post.

1 Like

[WARNING]: * Failed to parse /etc/ansible/hosts with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON:
Expecting value: line 1 column 1 (char 0) Syntax Error while loading YAML. did not find expected The error appears to be in
‘/etc/ansible/hosts’: line 47, column 1, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be:

[WARNING]: * Failed to parse /etc/ansible/hosts with ini plugin: /etc/ansible/hosts:64: Expected key=value, got: ansible_network_os: cisco.ios.ios
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

: UNREACHABLE! => {“changed”: false, “msg”: “Failed to connect to the host via ssh: root@core_switch: Permission denied (publickey,keyboard-interactive,password).”, “unreachable”: true}

Am getting errors above.

localhost]: FAILED! => {“changed”: false, “msg”: “Connection type local is not valid for this module”}

when i change to use connection : local

@FELIXmu

Could you show us your inventory file to understand?
If there is a password, mask it.