Ahti Kitsik
Building tools with Python, Java, Go, JS. For enquiries email inbox@ahtik.com

writings biography

Lazy file building with Ansible

04 Apr 2014 by @ahtik

There are times when Ansible change detection can not possibly know if a task needs to be ran again or not.

One such case is when a task generates a file from a source file and timestamps are not reliable and generated file can already exist. Maybe there are more correct ways but my approach is to calculate md5 sum for the source file and after successful generation store it in target host /tmp dir. Next time we just have to check if the source file md5 hash is the same as last time the target file was generated.

This helps with time-consuming tasks such as generating a Docker image.

I should add that this hashing is only needed if you do not know which files are generated in the process of building the task. As with Docker, the image gets a unique ID that is not known for the "created" attribute.

  - name: Dockerfile md5sum calc
    shell: md5sum /var/myproject/dockerbase/Dockerfile | cut -d' ' -f1
    register: dockerfilemd5
    changed_when: false # Never report as changed to keep the play run results clean
    tags: docker

  - name: md5 hash for dockerfile
    debug: msg=
    tags: docker

  - name: Prepare docker image
    shell: chdir=/var/myproject/dockerbase/ creates=/tmp/docker-image-created- docker build -t mgworker . && echo "done" > /tmp/docker-image-created-
    tags: docker

@ahtik is on twitter!