Logging to readable format

91 views
Skip to first unread message

RayO

unread,
Mar 12, 2020, 9:52:49 AM3/12/20
to Ansible Project
Hi. I have a very basic cisco IOS playbook i'm working on and when I log the output to a file I cannot read the format. I read a few threads that mention enabling a callback option in the ansible.cfg but that does not seem to work. Does anyone know how I can accomplish logging to a readable text?

---

- name: IOS General Checks
  hosts: switches
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Standard IOS Checks
      ios_command:
        commands:
          - show int status
          - show int description
      register: output1

    - name: print result
      debug:
        var: output1.stdout_lines

    - name: copy output to file
      copy: content="{{ output1.stdout_lines }}" dest=/home/sideswipe/ansible/cisco/logs/{{ inventory_hostname }}.txt

...

# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]
display_failed_stderr = no
display_ok_hosts = yes
display_skipped_hosts = yes
show_custom_stats = no
show_per_host_start = no

Stefan Hornburg (Racke)

unread,
Mar 12, 2020, 10:04:44 AM3/12/20
to ansible...@googlegroups.com
On 3/12/20 2:52 PM, RayO wrote:
> Hi. I have a very basic cisco IOS playbook i'm working on and when I log the output to a file I cannot read the format.
> I read a few threads that mention enabling a callback option in the ansible.cfg but that does not seem to work. Does
> anyone know how I can accomplish logging to a readable text?
>

Ansible stringifies the list object output1.stdout_lines, but you want to print each element of this list to line
in the file.

Using "{{ output1.stdout_lines | join('\n') }}" should do the trick.

Regards
Racke
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/098fbdd3-21e1-4a4c-afa2-16b1649ce4f7%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/098fbdd3-21e1-4a4c-afa2-16b1649ce4f7%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

RayO

unread,
Mar 12, 2020, 10:51:25 AM3/12/20
to Ansible Project
Hi Stefan,

Thanks for the reply. Unfortunately though it doesn't look like that worked. I replaced the below line with the entry you provided but it's still printing in a bad format

copy: content="{{ output1.stdout_lines | join('\n') }}"

Any other ideas?
>       copy: content="{{ output1.stdout_lines }}" dest=/home/user/ansible/cisco/logs/{{ inventory_hostname }}.txt
>
> ...
>
> # config file for ansible -- https://ansible.com/
> # ===============================================
>
> # nearly all parameters can be overridden in ansible-playbook
> # or with command line flags. ansible will read ANSIBLE_CONFIG,
> # ansible.cfg in the current working directory, .ansible.cfg in
> # the home directory or /etc/ansible/ansible.cfg, whichever it
> # finds first
>
> [defaults]
> display_failed_stderr = no
> display_ok_hosts = yes
> display_skipped_hosts = yes
> show_custom_stats = no
> show_per_host_start = no
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to

RayO

unread,
Mar 12, 2020, 4:27:58 PM3/12/20
to Ansible Project
Anyone have any thoughts on this?

RayO

unread,
Mar 12, 2020, 5:20:21 PM3/12/20
to Ansible Project
I was able to resolve this finding the below thread. In my case I had to alter the config slightly to the below. This was to merge the text together no space and eliminate "_lines"

    - name: print result
      debug:
        var: output1.stdout_lines

    - name: copy output to file
      copy: content="{{output1.stdout|join("\n")}}" dest=/home/userfolder/ansible/cisco/logs/{{ inventory_hostname }}.txt
Reply all
Reply to author
Forward
0 new messages