- hosts: all
tasks:
- name: Check if package is installed on RedHat
block:
- name: Find installed package on RedHat
command: rpm -qa | grep httpd
register: package_check_redhat
ignore_errors: true
- name: Save result to text file on RedHat
copy:
content: "{{ package_check_redhat.stdout }}"
dest: /tmp/result_redhat.txt
when: ansible_os_family == "RedHat"
- name: Check if package is installed on Debian
block:
- name: Find installed package on Debian
command: dpkg -l | fgrep dpkg
register: package_check_debian
ignore_errors: true
- name: Save result to text file on Debian
copy:
content: "{{ package_check_debian.stdout }}"
dest: /tmp/result_debian.txt
when: ansible_os_family == "Debian"
- hosts: APP gather_facts: yes
tasks:
- name: Gather package facts
package_facts:
manager: auto
- name: Get installed package version (example package)
command: "dpkg -s nginx"
register: dpkg_data
changed_when: false
ignore_errors: true
- name: Set fact for nginx version
set_fact:
nginx_version: "{{ dpkg_data.stdout_lines | select('search', 'Version') | first | default('Version not found') | regex_replace('Version: ', '') }}"
- name: Collect information
set_fact:
host_info: "{{ ansible_hostname }},{{ ansible_default_ipv4.address }},{{ nginx_version | regex_replace('\n', '; ') }}"
- name: Collect information from all hosts
lineinfile:
path: "collected_info.csv"
line: "{{ hostvars[item].host_info }}"
create: yes
with_items: "{{ ansible_play_batch }}"
delegate_to: localhost
run_once: true