Package Facts with multiple Packages

66 views
Skip to first unread message

Silvio Siefke

unread,
Oct 29, 2022, 9:36:19 AM10/29/22
to ansible...@googlegroups.com
Hello,

I try to install restic with Ansible and I use multiple Distros.


So I create a vars file with content:

_restic_packages:
  default:
    - restic
    - fuse
    - bzip2
    - pigz
  Alpine:
    - restic
    - fuse
    - bzip2
    - pigz
  Archlinux:
    - restic
    - fuse3
    - fuse2fs
    - bzip2
    - pigz
  Debian:
    - restic
    - fuse3
    - fuse2fs
    - bzip2
    - pigz
  FreeBSD:
    - restic
    - fusefs-sshfs
    - fusefs-libs
    - bzip2
    - pigz

restic_packages: "{{ _restic_packages[ansible_os_family ~ '-' ~
ansible_distribution_major_version] |
default(_restic_packages[ansible_os_family] |
default(_restic_packages['default'] )) }}"


The install file in role:

- name: Check installation
  ansible.builtin.package_facts:
    manager: auto

- name: Install restic
  ansible.builtin.package:
    name: "{{ restic_packages }}"
    state: present
  when:
    - "'{{ restic_packages }}' not in ansible_facts.packages"



This end with:

[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: {{ restic_packages }} not in
ansible_facts.packages
fatal: [storage_fr]: FAILED! =>
  msg: |-
    The conditional check '{{ restic_packages }} not in
ansible_facts.packages' failed. The error was: Unexpected templating
type error occurred on ({% if ['restic', 'fuse3', 'fuse2fs', 'bzip2',
'pigz'] not in ansible_facts.packages %} True {% else %} False {% endif
%}): unhashable type: 'list'

    The error appears to be in
'/home/siefke/Public/projects/coding/ansible/roles/core/restic/tasks/install.yml':
line 3, column 3, but may
    be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:

    ---
    - name: Check installation
      ^ here
    to retry, use: --limit
@/home/siefke/Public/projects/coding/ansible/data/retry/restic.retry



How can I make it correct? Thank you.

Silvio

Felix Fontein

unread,
Oct 29, 2022, 10:26:18 AM10/29/22
to ansible...@googlegroups.com
Hi,

> - name: Install restic
>   ansible.builtin.package:
>     name: "{{ restic_packages }}"
>     state: present
>   when:
>     - "'{{ restic_packages }}' not in ansible_facts.packages"

1. This condition does not do what you want. You are testing whether
the list of packages is a *member* of the set of installed packages.
(Which does not work, because lists cannot be part of hash sets and
lists are not hashable.) You want to test for subset, not for
membership.

2. Why not simply remove the `when:` and let the package action decide
whether the packages have already been installed or not? The package
action is supposed to be idempotent.

Cheers,
Felix


Reply all
Reply to author
Forward
0 new messages