Error while readin split part for updating Ubuntu packages - Should I include the path of splitpath,py in playbook ?

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

Hii

"splitpath.py" isn't a valid key for the config file. The correct one
is "filter_plugins", see:
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-filter-plugin-path

But I don't see why you would need such heavy weapons if all you want
is a list of package names that have pending updates.
This should roughly do the same, without the complexity of custom filters:

- name: Check for pending updates
  become: true
  hosts: all

  tasks:
    - name: Update cache
      apt:
        update-cache: yes
      changed_when: false

    - name: Fetch package list of updates
      command: apt list --upgradable
      register: aptlist

    - set_fact:
        updates: "{{ aptlist.stdout_lines | difference(['Listing...'])

map('regex_replace', '^(.*?)\/(.*)', '\\1') | list }}"

    - debug: var=updates

Great! Thanks that worked but I had to modify the syntax in the set fact to make it work.

  • name: Check for pending updates
    become: true
    hosts: all

tasks:

  • name: Update cache
    apt:
    update-cache: yes
    changed_when: false

  • name: Fetch package list of updates
    command: apt list --upgradable
    register: aptlist

  • set_fact:
    updates: "{{ aptlist.stdout_lines | difference([‘Listing…’])