'How can I reference a Dockerfile relative to the ansible role task YAML file?

I have an Ansible repo with the following directory path

.
├── kube-init.sh
├── provisioning
│   ├── group_vars
│   │   └── all.yml
│   ├── roles
│   │   └── app_deploy
│   │       ├── files
│   │       │   ├── secretfile
│   │       ├── tasks
│   │       │   ├── docker
│   │       │   │   ├── Dockerfile
│   │       │   └── main.yml
│   │       └── vars
│   │           └── main.yaml
│   └── site.yml
├── README.md

I'm trying to define a docker build image task at provisioning/roles/app_deploy/tasks/main.yaml as follows

- name: Build image and with build args
  vars:
    ansible_python_interpreter: /usr/bin/python3
  docker_image:
    name: app-name
    build:
      path: docker
      dockerfile: Dockerfile
      args:
        log_volume: /var/log/svm
        listen_port: 8080
    state: present
    source: build

I can't quite get the Dockerfile/context to be made available to the ansible task. Played around with various combinations of relative/absolute values of path and dockerfile combinations.

I thought the most obvious choice would be to skip using dockerfile and just use path as ./docker which didn't work to my surprise.

Using Ansible with Python3 with Docker SDK above v5

A bit more context. I'm actually using Vagrant with Openstack plugin to provision the new compute with my Ansible tasks. The plugin copies the contents of the repo (Ansible) on to the target machine at the path /home/vagrant, and runs the provisioning scripts from there.



Solution 1:[1]

I figured out the right path to pass for the docker module the following way. I added a debug task below to figure out the current working directory on which my app_deploy/tasks/main.yml runs from as

- name: Find out playbook's path
  shell: pwd
  register: playbook_path_output
- debug: var=playbook_path_output.stdout

which was returning me provisioning/roles. I realized that the path to be from where site.yml was located which defines one of the roles to be app_deploy.

So with that, I modified the docker module path as

- name: Build image and with build args
  vars:
    ansible_python_interpreter: /usr/bin/python3
  docker_image:
    name: app-name
    build:
      path: ./roles/app_deploy/tasks/docker/
      args:
        log_volume: /var/log/svm
        listen_port: 8080
    state: present
    source: build

But per Zeitounator's comment, the above might not be a right practice to store the Dockerfile and its constituent files inside a particular role directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1