Repository: ansible/ansible

Issue:
Specifying "latest" for pkg5 state does not install uninstalled package
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
pkg5

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.2.1.0
  config file = /home/autarch/.ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
None

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
Ubuntu 16.04, host is Solaris 11

##### SUMMARY
<!--- Explain the problem briefly -->
Specifying state as `latest` leads to packages not being installed at all.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Make sure all packages we want are installed
  pkg5:
    name: "{{ item }}"
    state: latest
  with_items:
    - gcc # installs C, C++, and Fortran compilers plus helper tools
    - /editor/gnu-emacs/gnu-emacs-no-x11
    - vim
    - autoconf
    - automake
    - bison
    - byaccj
    - elfutils
    - flex
    - libtool
    - /text/gnu-patch
    - /text/patchutils
    - swig
    - gnu-binutils
    - /file/gnu-coreutils
    - /text/gnu-diffutils
    - /file/gnu-findutils
    - git
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
I thought packages would be installed

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [solaris : Make sure all packages we want are installed] ******************
task path: /home/autarch/work/camel/ansible/roles/solaris/tasks/packages.yml:2
ok: [192.168.42.108] => (item=gcc) => {"changed": false, "item": "gcc", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/editor/gnu-emacs/gnu-emacs-no-x11) => {"changed": false, "item": "/editor/gnu-emacs/gnu-emacs-no-x11", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/editor/vim) => {"changed": false, "item": "/solaris/editor/vim", "msg": "", "results": []}
ok: [192.168.42.108] => (item=autoconf) => {"changed": false, "item": "autoconf", "msg": "", "results": []}
ok: [192.168.42.108] => (item=automake) => {"changed": false, "item": "automake", "msg": "", "results": []}
ok: [192.168.42.108] => (item=bison) => {"changed": false, "item": "bison", "msg": "", "results": []}
ok: [192.168.42.108] => (item=byaccj) => {"changed": false, "item": "byaccj", "msg": "", "results": []}
ok: [192.168.42.108] => (item=elfutils) => {"changed": false, "item": "elfutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=flex) => {"changed": false, "item": "flex", "msg": "", "results": []}
ok: [192.168.42.108] => (item=libtool) => {"changed": false, "item": "libtool", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/text/gnu-patch) => {"changed": false, "item": "/solaris/text/gnu-patch", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/text/patchutils) => {"changed": false, "item": "/text/patchutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=swig) => {"changed": false, "item": "swig", "msg": "", "results": []}
ok: [192.168.42.108] => (item=gnu-binutils) => {"changed": false, "item": "gnu-binutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/file/gnu-coreutils) => {"changed": false, "item": "/solaris/file/gnu-coreutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/text/gnu-diffutils) => {"changed": false, "item": "/solaris/text/gnu-diffutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/file/gnu-findutils) => {"changed": false, "item": "/solaris/file/gnu-findutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=git) => {"changed": false, "item": "git", "msg": "", "results": []}
```

Specifying "latest" for pkg5 state does not install uninstalled package
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
pkg5

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.2.1.0
  config file = /home/autarch/.ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
None

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
Ubuntu 16.04, host is Solaris 11

##### SUMMARY
<!--- Explain the problem briefly -->
Specifying state as `latest` leads to packages not being installed at all.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Make sure all packages we want are installed
  pkg5:
    name: "{{ item }}"
    state: latest
  with_items:
    - gcc # installs C, C++, and Fortran compilers plus helper tools
    - /editor/gnu-emacs/gnu-emacs-no-x11
    - vim
    - autoconf
    - automake
    - bison
    - byaccj
    - elfutils
    - flex
    - libtool
    - /text/gnu-patch
    - /text/patchutils
    - swig
    - gnu-binutils
    - /file/gnu-coreutils
    - /text/gnu-diffutils
    - /file/gnu-findutils
    - git
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
I thought packages would be installed

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [solaris : Make sure all packages we want are installed] ******************
task path: /home/autarch/work/camel/ansible/roles/solaris/tasks/packages.yml:2
ok: [192.168.42.108] => (item=gcc) => {"changed": false, "item": "gcc", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/editor/gnu-emacs/gnu-emacs-no-x11) => {"changed": false, "item": "/editor/gnu-emacs/gnu-emacs-no-x11", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/editor/vim) => {"changed": false, "item": "/solaris/editor/vim", "msg": "", "results": []}
ok: [192.168.42.108] => (item=autoconf) => {"changed": false, "item": "autoconf", "msg": "", "results": []}
ok: [192.168.42.108] => (item=automake) => {"changed": false, "item": "automake", "msg": "", "results": []}
ok: [192.168.42.108] => (item=bison) => {"changed": false, "item": "bison", "msg": "", "results": []}
ok: [192.168.42.108] => (item=byaccj) => {"changed": false, "item": "byaccj", "msg": "", "results": []}
ok: [192.168.42.108] => (item=elfutils) => {"changed": false, "item": "elfutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=flex) => {"changed": false, "item": "flex", "msg": "", "results": []}
ok: [192.168.42.108] => (item=libtool) => {"changed": false, "item": "libtool", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/text/gnu-patch) => {"changed": false, "item": "/solaris/text/gnu-patch", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/text/patchutils) => {"changed": false, "item": "/text/patchutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=swig) => {"changed": false, "item": "swig", "msg": "", "results": []}
ok: [192.168.42.108] => (item=gnu-binutils) => {"changed": false, "item": "gnu-binutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/file/gnu-coreutils) => {"changed": false, "item": "/solaris/file/gnu-coreutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/text/gnu-diffutils) => {"changed": false, "item": "/solaris/text/gnu-diffutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=/solaris/file/gnu-findutils) => {"changed": false, "item": "/solaris/file/gnu-findutils", "msg": "", "results": []}
ok: [192.168.42.108] => (item=git) => {"changed": false, "item": "git", "msg": "", "results": []}
```



Discussion:
cc @mavit
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
-needs_triage
cc @mavit
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
-needs_triage

Resolving patch:
diff --git a/lib/ansible/modules/packaging/os/pkg5.py b/lib/ansible/modules/packaging/os/pkg5.py
--- a/lib/ansible/modules/packaging/os/pkg5.py
+++ b/lib/ansible/modules/packaging/os/pkg5.py
@@ -127,7 +127,9 @@ def ensure(module, state, packages, params):
             'subcommand': 'install',
         },
         'latest': {
-            'filter': lambda p: not is_latest(module, p),
+            'filter': lambda p: (
+                not is_installed(module, p) or not is_latest(module, p)
+            ),
             'subcommand': 'install',
         },
         'absent': {


Test patch: