---
- name: Stand up application container host
  hosts: web
  sudo: true
  user: ubuntu
  pre_tasks:
    - name: Install wget
      apt: name=wget state=latest
    - name: Install docker
      shell: wget -qO- https://get.docker.com/ | sh
    - name: Install pip
      apt: name=python-pip
    - name: Install docker python
      pip: name=docker-py version=1.2.2
    - name: Ensure docker is running and enabled
      service: name=docker state=started enabled=true
  post_tasks:
    - name: Remove unused docker images
      shell: docker rmi `docker images -q`
      ignore_errors: yes
  roles:
    - role: postgres
    - role: webapp

---
- name: Stand up application container host
  hosts: web
  sudo: true
  user: ubuntu
  pre_tasks:
    - name: Install wget
      apt: name=wget state=latest
    - name: Install docker
      shell: wget -qO- https://get.docker.com/ | sh
    - name: Install pip
      apt: name=python-pip
    - name: Install docker python
      pip: name=docker-py version=1.2.2
    - name: Ensure docker is running and enabled
      service: name=docker state=started enabled=true
  post_tasks:
    - name: Remove unused docker containers
      shell: docker rm `docker ps -a -q`
      ignore_errors: yes
    - name: Remove unused docker images
      shell: docker rmi `docker images -q`
      ignore_errors: yes
  roles:
    - role: postgres
    - role: webapp

Delete unused containers with ansible
[TASK] Delete unused containers with ansible
