Trying To Get Certain Package Information In A List Format For All Host

25 views
Skip to first unread message

Marcus Hayhurst

unread,
Jun 3, 2021, 10:17:06 PM6/3/21
to Ansible Project
Hello everyone,

I was trying to loop through all my hosts and if a certain package (xauth in this case) was present it would create a list and email that list with this information:

If package was found:
HOSTNAME - PACKAGE_NAME:VERSION

If package was not found:
PACKAGE_NAME Not Found on HOSTNAME

I thought this would work but it does not.

- name: GATHER INSTALLED PACKAGES INFORMATION
  package_facts:
    manager: auto
    strategy: all
  ignore_errors: True
#
#
- name: SET FACT FOR PACKAGE PRESENCE
  set_fact:
    package_presence: "{{ ansible_facts.packages['xauth'] }}"
  ignore_errors: True
#
#
- name: LIST HOSTNAME, PACKAGE NAME AND VERSION IF PRESENT
  set_fact:
    machine_info:
      "{% for host in ansible_play_batch %}{% if package_presence is defined %}{{ hostvars[host]['ansible_hostname'] }} - {{ hostvars[host]['ansible_facts']['packages']['xauth'][0]['name'] }}:{{ hostvars[host]['ansible_facts']['packages']['xauth'][0]['version'] }}<br>{% else %}XAUTH Not Found on {{ hostvars[host] }}<br>{% endif 
%}{% endfor %}"
  run_once: yes
  ignore_errors: True

This is the error I receive:

The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'xauth'\n\nThe error appears to be in '/var/lib/awx/projects/roles/Package_Information/tasks/main.yml': line 21, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n#\n- name: LIST HOSTNAME, PACKAGE NAME AND VERSION IF PRESENT\n  ^ here\n",
Reply all
Reply to author
Forward
0 new messages