Hold multiple packages using Ansible

0

I am trying to hold multiple packages using ansible-playbook but it doesn't work with me.

Using the below code it holds the first package then un-hold it then hold the second package

Here is my code

- name: Prevent packages from being upgraded
  dpkg_selections:
    name: "{{ item }}"
    selection: hold
  with_items:
    - postgresql
    - docker

Here is the output from the server side while the code executing enter image description here the first line before executing the second line is the output when the first package was hold the third line when the second package was held and it is stoped

I don't understand why the behavior is like that? and how can I hold multiple packages at a time using ansible?

NOTE: I already followed the instruction from Anible doc https://docs.ansible.com/ansible/latest/collections/ansible/builtin/dpkg_selections_module.html Thanks in advance

ansible linux
2021-11-23 19:10:07
1

1

Already for performance and resource reasons, providing the packages as list might be better.

- name: Prevent packages from being upgraded
  dpkg_selections:
    name: ['postgresql', 'docker']
    selection: hold

However, your test reported

dpkg: error: unexpected data after package and selection

Therefore it might be that the module can't handle lists, so I had a look into the source dpkg_selections.py. It seems to be a somehow simple wrapper

module.run_command([dpkg, '--set-selections'], data="%s %s" % (name, selection), check_rc=True)

which just provide information for one module. I also assume the module should work with_items, but it seems to be not the case because of your question.

According man pages, the command dpkg itself seems to be able to handle a package list, but provided as character separated value file

dpkg --set-selections < /tmp/pkg_list

with delimiter in the format

postgresql hold
docker hold

A simple workaround could help in your case.

- name: Prevent packages from being upgraded
  shell:
    cmd: |
      dpkg --set-selections << EOF
      postgresql hold
      docker hold
      EOF
    warn: false
    register: result

You may need to implement some error and status handling by yourself, i.e.

changed_when: result.rc ...
failed_when: result.rc ...

Thanks to

2021-11-24 09:45:32

no syntax error ` “msg”: “dpkg: error: unexpected data after package and selection at line 1", “rc”: 2, “stderr”: “dpkg: error: unexpected data after package and selection at line 1\n”, “stderr_lines”: [ “dpkg: error: unexpected data after package and selection at line 1” ], “stdout”: “”, “stdout_lines”: [] `
Sara

@Sara, thanks for providing test results, I've updated the answer accordingly.
U880D

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................