# Installing packages from variable

**URL:** <https://forum.ansible.com/t/installing-packages-from-variable/30004>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 27, 2019, 10:52pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004 "2019-03-27T22:52:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [March 27, 2019, 10:52pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004/1 "2019-03-27T22:52:51Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 27, 2019, 11:04pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004/2 "2019-03-27T23:04:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 9, 2019, 2:39pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004/3 "2019-04-09T14:39:39Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 13, 2019, 1:47pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004/4 "2019-04-13T13:47:34Z")

</div>

> @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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 18, 2019, 4:11pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004/5 "2019-04-18T16:11:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [April 24, 2019, 10:14pm UTC](https://forum.ansible.com/t/installing-packages-from-variable/30004/6 "2019-04-24T22:14:39Z")

</div>

Done: [https://github.com/ansible/ansible/issues/55741](https://github.com/ansible/ansible/issues/55741)
