Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hold multiple packages using Ansible

Tags:

linux

ansible

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

like image 665
Sara Avatar asked Oct 24 '25 03:10

Sara


2 Answers

What about:

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

In my case, the output from the server side after the execution was the full list of packages.

like image 99
gizmo9002 Avatar answered Oct 26 '25 19:10

gizmo9002


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

  • How to do multiline shell script in Ansible
like image 24
U880D Avatar answered Oct 26 '25 17:10

U880D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!