Ansible: "apt" module and loops

Posted by ads' corner on Monday, 2020-01-20
Posted in [Ansible][Linux]

After upgrading Ansible to version 2.8.3, the “apt” module started spitting out┬ádeprecation warnings:

TASK [common : basic packages] ****************************************************************
[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 }}"`, please use `name: ['vim', 'screen', 'rsync']` 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.
ok: [hostname] => (item=['vim', 'screen', 'rsync'])

The cause of the warning is the following task (or other similar apt tasks):

1
2
3
4
5
6
7
8
- name: basic packages
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - vim
    - screen
    - rsync

Apparently the apt module is invoked for every item in the loop, and that is slow, and expensive.

There is a better way to do this:

1
2
3
4
5
6
7
- name: basic packages
  apt:
    name:
      - vim
      - screen
      - rsync
    state: present

But I yet have to figure out how to do this if the package names are in a variable. That is for another day …


Categories: [Ansible] [Linux]