---
- name: Create
  hosts: localhost
  connection: local
  gather_facts: False
  vars:
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
    molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}"
    molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}"
    molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
  tasks:
    - name: Create builder image
      docker_image:
        name: builder
        path: .
        buildargs:
          IMAGE: "{{ item.image }}"
          NAME: "{{ item.name }}"
      with_items: "{{ molecule_yml.platforms }}"

    - name: Create molecule instance(s)
      docker_container:
        name: "{{ item.name }}"
        hostname: "{{ item.name }}"
        image: builder
        state: started
        recreate: True
        command: "tail -f /dev/null"
        privileged: "{{ item.privileged | default(omit) }}"
        volumes: "{{ item.volumes | default(omit) }}"
        capabilities: "{{ item.capabilities | default(omit) }}"
      with_items: "{{ molecule_yml.platforms }}"

---
- name: Create
  hosts: localhost
  connection: local
  gather_facts: False
  vars:
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
    molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}"
    molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}"
    molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
  tasks:
    - name: Get hash of our molecule folder, plus dockerfile
      stat:
        path: "./{{ item }}"
        get_md5: "no"
        checksum_algorithm: sha256
      register: file_hashes
      with_items:
        - Dockerfile
        - molecule.yml

    - name: Get a unique concatenated hash for docker tagging
      set_fact:
        docker_tag: "{{ docker_tag|default('') + item[0:8] }}"
      with_items: "{{ file_hashes.results| map(attribute='stat.checksum') | list }}"

    - name: Create builder image
      docker_image:
        name: "sd.local/{{ item.name }}:{{ docker_tag }}"
        path: .
        buildargs:
          IMAGE: "{{ item.image }}"
          NAME: "{{ item.name }}"
      with_items: "{{ molecule_yml.platforms }}"

    - name: Create molecule instance(s)
      docker_container:
        name: "{{ item.name }}"
        hostname: "{{ item.name }}"
        image: "sd.local/{{ item.name }}:{{ docker_tag }}"
        state: started
        recreate: True
        command: "tail -f /dev/null"
        privileged: "{{ item.privileged | default(omit) }}"
        volumes: "{{ item.volumes | default(omit) }}"
        capabilities: "{{ item.capabilities | default(omit) }}"
      with_items: "{{ molecule_yml.platforms }}"

Build rudimentary docker tag, instead of force
Build rudimentary docker tag, instead of force

Ansible has some shoddy detection logic for determining when to rebuild
an image or layers. I've seen issues in the past with `force: yes` and
highly recommend we peg to file hashes as an intermediate way to trigger
when to kick off a new docker image.
