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”
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.
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.
- 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
[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}