09-10-2023 09:00 PM
Hello,
To my understanding, the output will be printed on the screen.
I would like save it under a log file.
I tried to add it to ansible.cfg file, but still I am not able to see the log file:
[default]
#some basic default values...
nocows=true
#log_path=/etc/ansible/logs/ansible_log.txt
log_path=./ansible_log.txt
callbacks_enabled=ansible.posix.profile_tasks
stdout_callback=ansible.posix.json
inventory = $HOME/.ansible/hosts
remote_tmp = $HOME/.ansible/tmp
forks = 150
sudo_user = root
transport = smart
:
host_key_checking = False
#
Thanks,
Netmart
Solved! Go to Solution.
10-01-2023 10:09 PM
Thank you Marcel.
Eventually I used:
tasks:
- name: show license all
ios_command:
commands:
- terminal length 0
- terminal width 200
- show license status
become: true
register: output
- name: print result
debug:
var: output
And directed the output to a text file:
ansible-playbook -i hosts site-iosxe-lic-enable-vault.yml -u cisco-k > site-iosxe-lic.txt
ass
09-10-2023 11:02 PM - edited 09-10-2023 11:04 PM
Hi, the log_path directive is correct. See this basic example:
ansible.cfg
[defaults]
log_path=./ansible.log
playbook (test.yml)
- name: test logging
hosts: localhost
gather_facts: false
tasks:
- name: a test debug message
ansible.builtin.debug:
msg: "This is a debug ouput"
this will create a log file ansible.log with the following content:
maercu@CHLT303344:~/dev/tmp$ cat ansible.log
2023-09-11 07:58:11,656 p=699 u=maercu n=ansible | [WARNING]: No inventory was parsed, only implicit localhost is available
2023-09-11 07:58:11,660 p=699 u=maercu n=ansible | [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
2023-09-11 07:58:11,765 p=699 u=maercu n=ansible | PLAY [test logging] ***************************************************************************************************************************************
2023-09-11 07:58:11,776 p=699 u=maercu n=ansible | TASK [a test debug message] *******************************************************************************************************************************
2023-09-11 07:58:11,789 p=699 u=maercu n=ansible | ok: [localhost] => {
"msg": "This is a debug ouput"
}
2023-09-11 07:58:11,796 p=699 u=maercu n=ansible | PLAY RECAP ************************************************************************************************************************************************
2023-09-11 07:58:11,796 p=699 u=maercu n=ansible | localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
09-12-2023 10:25 AM - edited 09-12-2023 10:26 AM
Thank you Marcel.
I tried and got the following in the logs:
$ cat ansible.log
2023-09-11 07:58:11,656 p=699 u=cisco n=ansible | [WARNING]: No inventory was parsed, only implicit localhost is available
2023-09-11 07:58:11,660 p=699 u=cisco n=ansible | [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
2023-09-11 07:58:11,765 p=699 u=cisco n=ansible | PLAY [test logging] ***************************************************************************************************************************************
2023-09-11 07:58:11,776 p=699 u=cisco n=ansible | TASK [a test debug message] *******************************************************************************************************************************
2023-09-11 07:58:11,789 p=699 u=cisco n=ansible | ok: [localhost] => {
"msg": "This is a debug ouput"
}
2023-09-11 07:58:11,796 p=699 u=cisco n=ansible | PLAY RECAP ************************************************************************************************************************************************
2023-09-11 07:58:11,796 p=699 u=cisco n=ansible | localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
My goal was to capture the show output of the following runbook. So not sure, if this does require a different type of logging:
site.yml file
---
- name: Test Ansible ios_command on Cisco IOS XE
hosts: iosxe
tasks:
- name: show version and ip interface brief
ios_command:
commands:
- show version
- show ip interface brief
I would appreciate any advice.
Thanks,
Netmart
09-12-2023 10:38 AM
In that case you need to register and print (debug) the result of your task - something like that:
- name: Test Ansible ios_command on Cisco IOS XE
hosts: iosxe
tasks:
- name: show version and ip interface brief
ios_command:
commands:
- show version
- show ip interface brief
register: output
- name: print result
debug:
var: output.stdout
Doing so, you should see the output on screen as well as in your log file.
09-18-2023 06:18 PM
Thank you Marcel.
Yes, it does print output on screen plus append it to ansible.log file
Is it possible to save the output as separated file as txt, yaml, or even json.
Also, when I tried to run "show license feature", it seems that the loaded ansible collect is not support it.
*fatal: [10.254.199.199]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.ios_facts": {"failed": true, "invocation": {"module_args": {"available_network_resources": false, "gather_network_resources": null, "gather_subset": ["min"]}}, "msg": "ssh connection failed: Failed to authenticate public key: Socket error: disconnected"}}, "msg": "The following modules failed to execute: ansible.legacy.ios_facts\n"}
09-18-2023 11:49 PM - edited 09-18-2023 11:51 PM
The error you receive is because of the host key checking (https://docs.ansible.com/ansible/2.8/user_guide/intro_getting_started.html#host-key-checking), to disable it, disable host key checking in your ansible.cfg:
[defaults]
log_path=./ansible.log
host_key_checking = False
To collect json parsed output, I usually use the cli_parse module (https://docs.ansible.com/ansible/latest/collections/ansible/utils/cli_parse_module.html) with pyats/genie, to use it you may need to install the collection + libary:
ansible-galaxy collection install ansible.utils
pip install pyats
Once done you can use it, here is an example:
inentory
all:
hosts:
sandbox-iosxe-latest-1.cisco.com:
vars:
ansible_user: admin
ansible_password: C1sco12345
ansible_network_os: cisco.ios.ios
ansible_connection: ansible.netcommon.network_cli
playbook
- name: Log outputs to file
hosts: sandbox-iosxe-latest-1.cisco.com
gather_facts: false
tasks:
### GET AND SAVE TXT OUTPUT #########################
- name: Run command
cisco.ios.ios_command:
commands:
- show license usage
register: output
- name: Print result as txt
debug:
var: output.stdout[0]
- name: Save result as txt to file hostname_output.txt
ansible.builtin.copy:
content: "{{output.stdout[0]}}"
dest: "{{inventory_hostname}}_output.txt"
### GET AND SAVE JSON OUTPUT ########################
- name: Run command and parse with genie
ansible.utils.cli_parse:
command: show license usage
parser:
name: ansible.netcommon.pyats
register: output
- name: Print Result as json
ansible.builtin.debug:
var: output.parsed
- name: Save result as json to file hostname_output.json
ansible.builtin.copy:
content: "{{output.parsed}}"
dest: "{{inventory_hostname}}_output.json"
HTH
10-01-2023 10:09 PM
Thank you Marcel.
Eventually I used:
tasks:
- name: show license all
ios_command:
commands:
- terminal length 0
- terminal width 200
- show license status
become: true
register: output
- name: print result
debug:
var: output
And directed the output to a text file:
ansible-playbook -i hosts site-iosxe-lic-enable-vault.yml -u cisco-k > site-iosxe-lic.txt
ass
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide