Repository: ansible/ansible

Issue:
apt module installs python-apt with unnecessary dependencies
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
modules/packaging/os/apt.py

##### ANSIBLE VERSION
```
2.4 2.5 devel
```

##### CONFIGURATION

No special configuration

##### OS / ENVIRONMENT

apt-based distros. Tested on Debian 7,8,9, ubuntu 16.04, 18.04.

##### SUMMARY
apt module depends on python-apt package on remote machines and try to install that package. Command for installation is `apt-get install python-apt -q -y`. This command installs more packages than apt module needed. For example on debian 9 and ubuntu it will install two versions of python: 2.7 and 3. That is not acceptable in some situations.


##### STEPS TO REPRODUCE
```yaml
# install python to remote host
ansible all -i remote-host, -m raw -a 'apt-get update -q && apt-get install python2.7 -y'
# This command installs additional packages for python3 on remote host
ansible all -i remote-host, -m apt -a 'name=curl'
```

##### EXPECTED RESULTS

Remote host package list will be as minimal as it possible.

##### ACTUAL RESULTS

Some output for apt-get install python-apt:

```
debian:9

root@862737c34447:/# apt-get install python-apt
0 upgraded, 54 newly installed, 0 to remove and 3 not upgraded.
After this operation, 88.4 MB of additional disk space will be used.


root@862737c34447:/# apt-get install python-apt --no-install-recommends
0 upgraded, 35 newly installed, 0 to remove and 3 not upgraded.
After this operation, 35.0 MB of additional disk space will be used.
```


```
ubuntu:18.04

root@5b0fe1ade585:/# apt-get install python-apt
3 upgraded, 55 newly installed, 0 to remove and 63 not upgraded.
After this operation, 78.5 MB of additional disk space will be used.

root@5b0fe1ade585:/# apt-get install python-apt --no-install-recommends
3 upgraded, 35 newly installed, 0 to remove and 63 not upgraded.
After this operation, 28.7 MB of additional disk space will be used.

```

```
debian:8

root@6ff511b6406b:/# apt-get install python-apt
0 upgraded, 20 newly installed, 0 to remove and 3 not upgraded.
After this operation, 43.1 MB of additional disk space will be used.

root@6ff511b6406b:/# apt-get install python-apt --no-install-recommends
0 upgraded, 15 newly installed, 0 to remove and 3 not upgraded.
After this operation, 22.7 MB of additional disk space will be used.
```

```
debian:7

root@687c77c93eea:/# apt-get install python-apt
0 upgraded, 17 newly installed, 0 to remove and 1 not upgraded.
After this operation, 40.7 MB of additional disk space will be used.

root@687c77c93eea:/# apt-get install python-apt --no-install-recommends
0 upgraded, 12 newly installed, 0 to remove and 1 not upgraded.
After this operation, 22.5 MB of additional disk space will be used.
```


```
ubuntu:16.04

root@d15cc580e64a:/# apt-get install python-apt
0 upgraded, 30 newly installed, 0 to remove and 21 not upgraded.
After this operation, 68.1 MB of additional disk space will be used.

root@d15cc580e64a:/# apt-get install --no-install-recommends python-apt             
0 upgraded, 15 newly installed, 0 to remove and 21 not upgraded.
After this operation, 22.9 MB of additional disk space will be used.
```




Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/packaging/os/apt.py b/lib/ansible/modules/packaging/os/apt.py
--- a/lib/ansible/modules/packaging/os/apt.py
+++ b/lib/ansible/modules/packaging/os/apt.py
@@ -891,7 +891,7 @@ def main():
                                  "If run normally this module can auto-install it." % PYTHON_APT)
         try:
             module.run_command(['apt-get', 'update'], check_rc=True)
-            module.run_command(['apt-get', 'install', PYTHON_APT, '-y', '-q'], check_rc=True)
+            module.run_command(['apt-get', 'install', '--no-install-recommends', PYTHON_APT, '-y', '-q'], check_rc=True)
             global apt, apt_pkg
             import apt
             import apt.debfile


Test patch: