Home > other >  state is present but all of the following are missing: source
state is present but all of the following are missing: source

Time:10-10

I have Ansible script to build docker image:

---
- hosts: localhost
  tasks:
  - name: build docker image
    docker_image:
      name: bionic
      path: /
      force: yes
    tags: deb

and Dockerfile:

FROM ubuntu:bionic
RUN export DEBIAN_FRONTEND=noninteractive; \
  apt-get -qq update && \
  apt-get -qq install \
  software-properties-common git curl wget openjdk-8-jre-headless debhelper devscripts
WORKDIR /workspace

when I run next command: ansible-playbook build.yml -vvv I received next exception.

<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/devpc/.ansible/tmp/ansible-tmp-1633701673.999151-517949-133730725910177/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
    "module_args": {
        "api_version": "auto",
        "archive_path": null,
        "build": null,
        "ca_cert": null,
        "client_cert": null,
        "client_key": null,
        "debug": false,
        "docker_host": "unix://var/run/docker.sock",
        "force": true,
        "force_absent": false,
        "force_source": false,
        "force_tag": false,
        "load_path": null,
        "name": "xroad-deb-bionic",
        "path": "/",
        "pull": null,
        "push": false,
        "repository": null,
        "source": null,
        "ssl_version": null,
        "state": "present",
        "tag": "latest",
        "timeout": 60,
        "tls": false,
        "tls_hostname": null,
        "use_ssh_client": false,
        "validate_certs": false
    }
},
"msg": "state is present but all of the following are missing: source"
}

Could you please give me a hint how to debug and understand what this error mean? Thanks for your time and consideration.

CodePudding user response:

The error says a source: key must be present in the docker_image: block.

More specifically, most things in Ansible default to state: present. When you request a docker_image to be present, there are a couple of ways to get it (pulling it from a registry, building it from source, unpacking it from a tar file). Which way to do this is specified by the source: control, but Ansible does not have a default value for this.

If you're building an image, you need to specify source: build. Having done that, there are also a set of controls under build:. In particular, the path to the Docker image context (probably not /) goes there and not directly under docker_image:.

This leaves you with something like:

---
- hosts: localhost
  tasks:
  - name: build docker image
    docker_image:
      name: bionic
      source: build  # <-- add
      build:         # <-- add
        path: /home/user/src/something  # <-- move under build:
      force: yes
    tags: deb
  • Related