Copy to report

36 views
Skip to first unread message

Dimitri Yioulos

unread,
Dec 8, 2023, 8:23:22 AM12/8/23
to Ansible Project
Good day!

I guess I'm fond of creating reports from debug output. Well, actually, I have a business case for doing so. I have a couple of playbooks that I've set up to create report:

playbook 1 -

---

- hosts: host1
  gather_facts: false
  become: yes

  tasks:

    - name: yum_command
      shell: rpm -qa|sort -n
      register: packages

    - debug:
        var: packages

    - local_action:
        module: copy
        content: |
          {% for host in ansible_play_hosts %}
          {{ host }}:

          The following packages are installed

          {{ hostvars[host]['packages']['stdout']|default("None", true) }}


          {% endfor -%}
        dest: "/tmp/installed.txt"
      run_once: yes
      tags:
        - report


playbook 2 -

---

- hosts: host1
  gather_facts: false
  become: yes

  tasks:

    - name: yum_command
      yum:
        list=updates
      register: packages

    - debug:
        var: packages

    - debug: var=item
      with_items: "{{packages|json_query(jsonquery)}}"
      vars:
        jsonquery: "results[?name!='null'].envra"

    - local_action:
        module: copy
        content: |
          {% for host in ansible_play_hosts %}
          {{ host }}:

          The following packages will be updated -

          {{ hostvars[host]['packages']['stdout']|default("None", true) }}


          {% endfor -%}
        dest: "/tmp/updates.txt"
      run_once: yes
      tags:
        - report


Playbook 1 creates the report, and populates it with debug output. Playbook 2 created the report, but doesn't add debug output. Why? How can I make playbook 2 include output

Todd Lewis

unread,
Dec 8, 2023, 4:20:04 PM12/8/23
to Ansible Project
The first one lists all the rpms installed on the system.
The second one list only the available updates. Perhaps there are none?

You'll have to convince me that debug output ends up in either /tmp/updates.txt or /tmp/installed.txt. I'm highly skeptical of that claim.
Reply all
Reply to author
Forward
0 new messages