Hello, Please see below
ansible.cfg
[defaults]
inventory = ./hosts
splitpart.py = /etc/ansible/splitpart.py/
splitpart.py
def splitpart (value, index, char = ‘,’):
if isinstance(value, (list, tuple)):
ret =
for v in value:
ret.append(v.split(char)[index])
return ret
else:
return value.split(char)[index]
class FilterModule(object):
def filters(self):
return {‘splitpart’: splitpart
playbook
tasks:
do an “apt-get update”, to ensure latest package lists
- name: apt-get update
apt:
update-cache: yes
changed_when: 0
get a list of packages that have updates
- name: get list of pending upgrades
command: apt-get --simulate dist-upgrade
args:
warn: false # don’t warn us about apt having its own plugin
register: apt_simulate
changed_when: 0
pick out list of pending updates from command output. This essentially
takes the above output from “apt-get --simulate dist-upgrade”, and
pipes it through “cut -f2 -d’ ’ | sort”
- name: parse apt-get output to get list of changed packages
set_fact:
updates: ‘{{ apt_simulate.stdout_lines | select(“match”, "^Inst ") | list | splitpart(1, " ") | list | sort }}’
changed_when: 0
tell user about packages being updated
- name: show pending updates
debug:
var: updates
when: updates.0 is defined
playbook output
PLAY [myotherserver] *****************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [10.0.2.236]
TASK [apt-get update] ****************************************************************************************************************************
ok: [10.0.2.236]
TASK [get list of pending upgrades] **************************************************************************************************************
ok: [10.0.2.236]
TASK [parse apt-get output to get list of changed packages] **************************************************************************************
fatal: [10.0.2.236]: FAILED! => {“msg”: “template error while templating string: no filter named ‘splitpart’. String: {{ apt_simulate.stdout_lines | select("match", "^Inst ") | list | splitpart(1, " ") | list | sort }}”}
PLAY RECAP ***************************************************************************************************************************************
10.0.2.236 : ok=3 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0