cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2095
Views
3
Helpful
6
Replies

Ansible Logging Outout

Netmart
Level 1
Level 1

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

1 Accepted Solution

Accepted Solutions

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

       

View solution in original post

6 Replies 6

Marcel Zehnder
Spotlight
Spotlight

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   

 

 

 

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

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.

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.

  tasks:
     - name: show license feature
       ios_command:
          commands:
              - show license feature

*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"}

Marcel Zehnder
Spotlight
Spotlight

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

 

 

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