Hi Everyone,
Im trying to fetch output for yum check-update and export the output to a csv file for multiple hosts. but im unable to replace empty line with IP address of each server from inventory_hostname (which is ip address) help would be greatly appreciated.
here is my playbook:
-
name: Playbook to get patch details from Linux Servers
hosts: all
tasks: -
name: remove the file
shell: ‘> /tmp/patch-updates.csv’ -
name: Get the Package list
shell: ‘yum check-update || test $? -eq 100’
register: result -
debug:
msg: “{{ ansible_distribution }}-{{ ansible_distribution_version }}”
- set_fact:
myvalue: “{{ result.stdout_lines[0] }}”
myvalue: “{{ result.stdout_lines[0] | default([{{ ansible_all_ipv4_addresses }}]) }}”
myvalue: “{{ result.stdout_lines[0] |regex_replace(‘(0)’, ‘test’)}}”
myvalue: “{{ result.stdout_lines[0] | regex_replace(‘\n*$’, ‘ansible_hostname’) }}”
vars:
regexp: ‘^\s*$’
replace: ‘{{ inventory_hostname }}’
-
debug:
var: myvalue -
debug: var=result.stdout_lines
-
name: If no patches or already upto date
fail: msg=“{{ ansible_host }} already upto date or no patches available”
when: result.stdout == “”
-
name: Saving output into csv file
local_action:
lineinfile
line={{ result.stdout }} path=/tmp/patch-updates.csv
when: result.stdout != “” -
name: Replace the space with comma
shell: sed -i -e ‘s/ {1,}/,/g’ /tmp/patch-updates.csv
when: result.stdout != “”
notify: send mail -
name: Replace Empty line with IP
replace:
path: /tmp/patch-updates.csv
regexp: ‘^\s*$’
replace: ‘{{ inventory_hostname }}’
handlers: -
name: send mail
mail:
port: 25
body: “Hello, \n\n This is auto generated report for available patch updates for Distribution Version. \n\n\n\n Please find the attached report”
attach: /tmp/patch-updates.csv
charset: utf8
delegate_to: localhost
run_once: True