FROM quay.io/ansible/ansible-runner:stable-2.9-devel as galaxy
ADD ansible.cfg ~/.ansible.cfg
ADD requirements.yml /build/
RUN ansible-galaxy role install -r /build/requirements.yml --roles-path /usr/share/ansible/roles
RUN ansible-galaxy collection install -r /build/requirements.yml --collections-path /usr/share/ansible/collections
RUN mkdir -p /usr/share/ansible/roles /usr/share/ansible/collections
FROM quay.io/ansible/python-builder:latest as builder
ADD requirements_combined.txt /tmp/src/requirements.txt
RUN assemble
FROM quay.io/ansible/ansible-runner:stable-2.9-devel
RUN whoami
RUN cat /etc/os-release
COPY --from=galaxy /usr/share/ansible/roles /usr/share/ansible/roles
COPY --from=galaxy /usr/share/ansible/collections /usr/share/ansible/collections
COPY --from=builder /output/ /output/
RUN /output/install-from-bindep && rm -rf /output/wheels
RUN echo This is a post-install command!
RUN ls -la /etc
大抵來說,
使用了 multi stage build 以減少 docker image 的大小
使用 ansible-galaxy 安裝相依的 role/collection
會看到 additional_build_steps 裡描述的步驟
好,那建置出 docker image 以後,怎麼使用呢?
假設 playbook 是放在 project 目錄下,那麼就這樣執行
docker run --rm -v /runner/project:$(pwd)/project -it example:latest ansible-playbook -i localhost, -c local /runner/project/test.yml
這邊稍微取了點巧,只簡單用 local connection (-i localhost, -c local) 在本機執行,你也可以在這邊使用自己的 inventory。