Complete debug output to file

574 views
Skip to first unread message

Dimitri Yioulos

unread,
Apr 26, 2021, 9:03:19 AM4/26/21
to Ansible Project
Hello, all.

It's been a long time since I posted here, so please forgive any posting faux pas.

I've created the following playboos:

---
- name: report aws migrator
  hosts: all
  gather_facts: false
  tasks:
    - name: collect facts
      shell:
        cmd: facter -p fqdn processorcount memorysize_mb blockdevice_sda_size blockdevice_vda_size operatingsystem operatingsystemrelease is_virtual processor0 --json
      register: facter_output

    - name: print output
      debug:
        msg: "{{ facter_output.stdout }}"
    - local_action: copy content="{{ facter_output.stdout }}". dest="/Users/dyioulos/Documents/prodrept.txt"

The problem is with the output to file.  Data are overwritten, and information for only the last host the playbook is run against is captured.  Additionally, i have to put any character here (denoted by ^): - local_action: copy content="{{ facter_output.stdout }}"^ dest="/Users/dyioulos/Documents/prodrept.txt" in order for the output to be in json format.  That extra character is also printed, including if I use newline) is also printed.

Your help in soving this would be greatly appreciated.

Stefan Hornburg (Racke)

unread,
Apr 26, 2021, 10:35:02 AM4/26/21
to ansible...@googlegroups.com
Hello Dimitri,

to collect the output from all hosts please try the following approach:

- local_action:
module: copy
content: |
{% for host in ansible_play_hosts %}
{{ host }}
{{ hostvars[host]['facter_output']['stdout'] }}

{% endfor %}
dest: "/Users/dyioulos/Documents/prodrept.txt"
run_once: yes

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/1fb0f55b-9eeb-461c-816b-ed292d068e5cn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1fb0f55b-9eeb-461c-816b-ed292d068e5cn%40googlegroups.com?utm_medium=email&utm_source=footer>.


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

OpenPGP_signature

Dimitri Yioulos

unread,
Apr 26, 2021, 11:08:26 AM4/26/21
to Ansible Project
That worked great; thank you so much!

Dimitri Yioulos

unread,
Apr 26, 2021, 11:28:49 AM4/26/21
to Ansible Project
Racke,

For my edification, might I ask exactly what your code does?

Thanks,

Dimitri

On Monday, April 26, 2021 at 9:03:19 AM UTC-4 Dimitri Yioulos wrote:
Message has been deleted

Dimitri Yioulos

unread,
Apr 30, 2021, 8:44:16 AM4/30/21
to Ansible Project
I'm not sure if I should create another post for this ask: forgive me if I should.  As per Racke, here's my playbook now, and it works great:

---

- hosts: all
  gather_facts: false
  become: yes
  become_user: root
  become_method: su
#  ignore_errors: yes

  tasks:
    - name: list java version
      command: java -version
      register: java_one

    - name: print output
      debug:
        msg: "{{ java_one.stderr_lines }}"
    - local_action:
        module: copy
        content: |
          {% for host in ansible_play_hosts %}
          {{ host }}
          {{ hostvars[host]['java_one']['stderr_lines'] }}

          {% endfor %}
        dest: "/Users/dyioulos/Documents/prodjava.txt"
      run_once: yes

But, now what I want to do is return data only when the debug output includes "Java(TM)".  I've searched, and tried several things, but, i'm not getting the desired result.  How do I do that?
Reply all
Reply to author
Forward
0 new messages