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

43 views
Skip to first unread message

Work-Hard

unread,
Feb 3, 2020, 7:11:50 PM2/3/20
to Ansible Project
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

Dick Visser

unread,
Feb 4, 2020, 2:57:21 AM2/4/20
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9de09a5f-1173-43b7-a2d7-3332ba0e060d%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Work-Hard

unread,
Feb 4, 2020, 2:24:36 PM2/4/20
to Ansible Project
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...'])
| map('regex_replace', '^(.*?)/(.*)', '\\1') | list }}"
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages