ansible 弃用警告与 apt 中的自定义对象

ansible deprecation warning with custom objects in apt

我在使用 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 }}"

这让我可以做到

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

它建议将名称替换为“{{ apt_dependencies }}”,但这不适用于自定义 name/state

我这样做是为了安装依赖项以及删除服务器上不需要的任何内容

关于如何更改它以在没有警告的情况下工作的任何想法,我知道我可以关闭它但我宁愿在它被删除之前修复它

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

如果你将它们分成两步,添加和删除,它会:

- 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