Installing packages from variable

I am installing packages from a variable, which works fine with yum; however, with apt I am having issues unless I change the format of the variable. Perhaps apt still allows you to use “with_items” unlike yum (which complains that the loop is deprecated in favor of a variable)? I haven’t tested that last speculation yet.

With yum I can install the following:

`
packages: “dbus, krb5-workstation, sssd, sssd-client, oddjob, samba-common, samba-common-tools, chrony”

  • name: ldap_users - Install required packages (RedHat-based)
    yum:
    name: “{{ packages }}”
    state: present
    skip_broken: yes
    changed_when: false
    ignore_errors: yes
    when: ansible_os_family == “RedHat”

`

If I try something similar with apt, it always fails to install the packages (usually says it can’t find krb5-user).

Failing:

`
packages: “dbus, krb5-user, sssd, oddjob, samba-common, samba, chrony, libpam-sss, libnss-sss, libnss-ldap”

  • name: ldap_users - Install required packages (Debian-based)
    apt:
    name: “{{ packages }}”
    update_cache: yes
    state: present
    changed_when: false
    ignore_errors: yes
    when: ansible_os_family == “Debian”

`

However, if I change my variable to the following for Ubuntu, it works fine:

`
packages:

  • dbus
  • krb5-user
  • sssd
  • oddjob
  • samba-common
  • samba
  • chrony
  • libpam-sss
  • libnss-sss
  • libnss-ldap

`

Just wondering if someone can explain that to me?

For thouroughness, here is the error I get:

`

The full traceback is:
WARNING: The below traceback may not be related to the actual failure.
File “/tmp/ansible_apt_payload_wK2PGK/main.py”, line 345, in package_status
pkg = cache[pkgname]
File “/usr/lib/python2.7/dist-packages/apt/cache.py”, line 262, in getitem
raise KeyError(‘The cache has no package named %r’ % key)

fatal: [ansible-ub18]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“allow_unauthenticated”: false,
“autoclean”: false,
“autoremove”: false,
“cache_valid_time”: 1,
“deb”: null,
“default_release”: null,
“dpkg_options”: “force-confdef,force-confold”,
“force”: false,
“force_apt_get”: false,
“install_recommends”: null,
“name”: “dbus, krb5-user, sssd, oddjob, samba-common, samba, chrony, libpam-sss, libnss-sss, libnss-ldap”,
“only_upgrade”: false,
“package”: [
“dbus”,
" krb5-user",
" sssd",
" oddjob",
" samba-common",
" samba",
" chrony",
" libpam-sss",
" libnss-sss",
" libnss-ldap"
],
“purge”: false,
“state”: “present”,
“update_cache”: null,
“upgrade”: null
}
},
“msg”: “No package matching ’ krb5-user’ is available”
}
…ignoring

`

Yum module support comma separated list apt module don't, reason is that no one has implemented it.

Both of them support list so just use that.

@Kai that is incorrect, apt uses 'list' type for name/package/pkg
which accepts both yaml lists AND comma separated strings

The problem is probably what the literal error message returns 'no
package named krb5-user' be aware that 'apt command line' normally
does a 'similar package name search' when the literal package name
passed is not found and might install a different package than
intended. The apt module specifically avoids this behaviour.

@Kai that is incorrect, apt uses 'list' type for name/package/pkg
which accepts both yaml lists AND comma separated strings

OK, I did check the documentation before answering and it did not say anything about a comma separated strings on the apt module only on the yum module.

The problem is probably what the literal error message returns 'no
package named krb5-user' be aware that 'apt command line' normally
does a 'similar package name search' when the literal package name
passed is not found and might install a different package than
intended. The apt module specifically avoids this behaviour.

There is a package called krb5-user in Ubuntu and with a list it worked only failed when using comma separated string.

The error message actually says
No package matching ' krb5-user' is available

It has a space in the front, so I guess that when using comma separated string on the apt module you need to write the string without spaces.

There is no specific docs about what is a list in each module as in
general we accept [x, y, z] AND "x, y, z" everywhere we say 'list'.

But it does seem to be a bug in not 'trimming' the list elements,
which might not be an issue when using underlying CLI, but is probably
hitting us when using the API (we do both in most package modules).

Please open a bug issue with the reproducer.

Done: https://github.com/ansible/ansible/issues/55741