ansible/ansible-modules-core — pull request #502 by smashwilson (2014-12-12 20:19:33+00:00)
Title: Docker: pull option

This adds a `pull` option to the Docker module, which has two possible settings:

* `missing` *(default)*. The named image and tag will only be pulled if missing from the host. This is backwards-compatible with the existing behavior.
* `always`. A pull will always be performed to check for more recent builds first. If an existing container is already running with an older version of that image, it will be stopped and restarted.

The use case that this captures is continuous delivery of a Docker image. You can use a task like this one:

```yaml
- name: Run the docker container
docker: pull=always image=smashwilson/minimal-sinatra name=minimal-sinatra ports=80:4567 state=running
```

... where "smashwilson/minimal-sinatra" is a container that's generated by an automated build on DockerHub, for example. Each time you execute the play, Ansible will ensure that the most recent build of that image is running. Without this, I find myself using ugly workarounds like:

```yaml
- name: pull the latest docker build
command: docker pull myimage
register: myimage
changed_when: outsideimage.stdout.find('Downloaded newer image') != -1
sudo: yes

- name: stop the older image
docker: image=myimage name=mycontainer state=absent
when: myimage | changed
sudo: yes

- name: launch the service
docker: image=myimage name=mycontainer state=running
sudo: yes
```

But you don't *always* want to attempt to pull the latest on every deployment. It would be inconvenient if your database container was unconditionally restarted, for example, and upgraded under your feet. For that reason and for backwards compatibility I gated this behind an module option.

This has been requested before, specifically in #27.