Wednesday, January 5, 2022

[SOLVED] ansible deprecation warning with custom objects in apt

Issue

I am getting a deprecated warning with Ansible

[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying name: "{{ item.name | default(item) }}", please use name: '{{ apt_dependencies }}' and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

- name: 'Install system dependencies'
  apt:
    name: "{{ item.name | default(item) }}"
    state: "{{ item.state | default('present') }}"
  with_items: "{{ apt_dependencies }}"

This allows me to do

apt_dependencies:
- name: curl
  state: absent
- name: ntp
  state: present
- docker

It suggests replacing name with "{{ apt_dependencies }}" but that wont work right with the custom name/state

I am doing this so I can install dependencies as well as remove any I don't want on the server

Any ideas how to change this to work without having a warning, which I get I can turn off but I'd rather fix it before its removed


Solution

It suggests replacing name with "{{ apt_dependencies }}" but that wont work right with the custom name/state

It will if you batch them into two steps, adds and removals:

- set_fact:
   remove_apt_packages: >-
     {{ apt_dependencies 
     | selectattr("state", "==", "absent")
     | map(attribute="name")
     | list }}

   # using "!= absent" allows for the case where the list item
   # doesn't say "present" such as your "docker" example
   add_apt_packages: >-
     {{ apt_dependencies 
     | selectattr("state", "!=", "absent")
     | map(attribute="name")
     | list }}
- name: 'Remove system dependencies'
  apt:
    name: "{{ remove_apt_packages }}"
    state: absent
- name: 'Install system dependencies'
  apt:
    name: "{{ add_apt_packages }}"
    state: present


Answered By - mdaniel