Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

397
Views
Hold multiple packages using 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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!