Repository: ansible/ansible

Issue:
OpenStack is missing list images functionality
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
os_image_facts

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.1.0
  config file = SOMETHING/PRIVATE/ansible.cfg
  configured module search path = ['SOMETHING/PRIVATE/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = SOMETHING/PRIVATE/.local/lib/python3.5/site-packages/ansible
  executable location = SOMETHING/PRIVATE/.local/bin/ansible
  python version = 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
```
DEFAULT_HOST_LIST( SOMETHING/PRIVATE/osimages/ansible.cfg) = ['SOMETHING/PRIVATE/osimages/inventory']
```
-->

##### 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.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Ubuntu 16.04 & N/A

##### SUMMARY
<!--- Explain the problem briefly -->
os_image_facts defines **image** (a name or id) as required, which is not the case with (for example) **name** in os_networks_facts and others. This means that there is no way to get a list of images on an OpenStack controller.
Shade actually provide a list_images function (https://github.com/openstack-infra/shade/blob/master/shade/openstackcloud.py#L2121) that could easily make os_image_facts get a list of images when no image id or name is provided.

##### 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 -->

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
The image attribute in os_image_facts should be optional, and os_image_facts should return a list of images when the attribute is not set.

##### ACTUAL RESULTS
The image attribute is required in os_image_facts, meaning that one has to either make a raw REST call or somehow _know_ the name of an image to use.

<!--- Paste verbatim command output between quotes below -->
```

```

OpenStack is missing list images functionality
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
os_image_facts

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.1.0
  config file = SOMETHING/PRIVATE/ansible.cfg
  configured module search path = ['SOMETHING/PRIVATE/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = SOMETHING/PRIVATE/.local/lib/python3.5/site-packages/ansible
  executable location = SOMETHING/PRIVATE/.local/bin/ansible
  python version = 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
```
DEFAULT_HOST_LIST( SOMETHING/PRIVATE/osimages/ansible.cfg) = ['SOMETHING/PRIVATE/osimages/inventory']
```
-->

##### 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.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Ubuntu 16.04 & N/A

##### SUMMARY
<!--- Explain the problem briefly -->
os_image_facts defines **image** (a name or id) as required, which is not the case with (for example) **name** in os_networks_facts and others. This means that there is no way to get a list of images on an OpenStack controller.
Shade actually provide a list_images function (https://github.com/openstack-infra/shade/blob/master/shade/openstackcloud.py#L2121) that could easily make os_image_facts get a list of images when no image id or name is provided.

##### 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 -->

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
The image attribute in os_image_facts should be optional, and os_image_facts should return a list of images when the attribute is not set.

##### ACTUAL RESULTS
The image attribute is required in os_image_facts, meaning that one has to either make a raw REST call or somehow _know_ the name of an image to use.

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
cc @dagnello @emonty @j2sol @juliakreger @rcarrillocruz @shrews @thingee
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@mkrizek @dagnello @emonty @j2sol @juliakreger @rcarrillocruz @Shrews @Thingee
Any thoughts on this? I am happy to work on it and provide a PR, since I believe this to be genuinely useful.
@dcotruta Seems like a sensible change to me. Thanks for volunteering! Please make sure that any changes are backward compatible. And it's helpful to show an example playbook and run here once you've made the changes. Thanks again!
cc @dagnello @emonty @j2sol @juliakreger @rcarrillocruz @shrews @thingee
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@mkrizek @dagnello @emonty @j2sol @juliakreger @rcarrillocruz @Shrews @Thingee
Any thoughts on this? I am happy to work on it and provide a PR, since I believe this to be genuinely useful.
@dcotruta Seems like a sensible change to me. Thanks for volunteering! Please make sure that any changes are backward compatible. And it's helpful to show an example playbook and run here once you've made the changes. Thanks again!

Resolving patch:
diff --git a/lib/ansible/modules/cloud/openstack/os_image_facts.py b/lib/ansible/modules/cloud/openstack/os_image_facts.py
--- a/lib/ansible/modules/cloud/openstack/os_image_facts.py
+++ b/lib/ansible/modules/cloud/openstack/os_image_facts.py
@@ -28,7 +28,7 @@
    image:
      description:
         - Name or ID of the image
-     required: true
+     required: false
    availability_zone:
      description:
        - Ignored. Present for backwards compatibility
@@ -49,6 +49,14 @@
 - name: Show openstack facts
   debug:
     var: openstack_image
+
+# Show all available Openstack images
+- name: Retrieve all available Openstack images
+  os_image_facts:
+
+- name: Show images
+  debug:
+    var: openstack_image
 '''
 
 RETURN = '''
@@ -134,16 +142,21 @@
 def main():
 
     argument_spec = openstack_full_argument_spec(
-        image=dict(required=True),
+        image=dict(required=False),
     )
     module_kwargs = openstack_module_kwargs()
     module = AnsibleModule(argument_spec, **module_kwargs)
 
     shade, cloud = openstack_cloud_from_module(module)
     try:
-        image = cloud.get_image(module.params['image'])
-        module.exit_json(changed=False, ansible_facts=dict(
-            openstack_image=image))
+        if module.params['image']:
+            image = cloud.get_image(module.params['image'])
+            module.exit_json(changed=False, ansible_facts=dict(
+                openstack_image=image))
+        else:
+            images = cloud.list_images()
+            module.exit_json(changed=False, ansible_facts=dict(
+                openstack_image=images))
 
     except shade.OpenStackCloudException as e:
         module.fail_json(msg=str(e))


Test patch: