Last active
September 25, 2022 15:45
-
-
Save jtyr/2676e6fb3ea4bc6d7e62 to your computer and use it in GitHub Desktop.
Ansible Playbook to build multiple Docker images without any Dockerfile (requires Ansible v2.0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
### Inventory hosts file should contain this: | |
# test1 docker_source_image=centos:7 | |
# test2 docker_source_image=centos:7 | |
### | |
### Run the playbook like this: | |
# ansible-playbook -i hosts --diff ./docker_image_builder.yaml | |
### | |
##### | |
### The following play will apply to all containers | |
##### | |
- name: Make sure the Docker container is running | |
connection: local | |
hosts: all | |
tasks: | |
- name: Create Docker container | |
docker: | |
name: "{{ inventory_hostname }}" | |
image: "{{ docker_source_image }}" | |
command: /bin/sleep 10000 | |
state: started | |
##### | |
### Each of the following plays will apply strictly per container type (the invenotry hostname) | |
##### | |
- name: Configure the Docker container test1 | |
connection: docker | |
hosts: test1 | |
tasks: | |
- package: | |
name: mc | |
state: present | |
- name: Configure the Docker container test2 | |
connection: docker | |
hosts: test2 | |
tasks: | |
- package: | |
name: vim | |
state: present | |
##### | |
### The following play will apply to all containers | |
##### | |
- name: Create new image from the container, destroy the container and push the image into the repo | |
connection: local | |
hosts: all | |
vars: | |
# These vars can go into the inventory file OR host_vars OR group_vars OR vars_files | |
docker_author: Jiri Tyr | |
docker_repo: jtyr | |
docker_tag: 1 | |
docker_commit: true | |
docker_stop: true | |
docker_push: false | |
tasks: | |
- name: Make a new image from the Docker container | |
shell: docker commit --author "{{ docker_author }}" {{ inventory_hostname }} {{ docker_repo }}/{{ inventory_hostname }}:{{ docker_tag }} | |
when: docker_commit | |
- name: Stop and remove the Docker container | |
docker: | |
name: "{{ inventory_hostname }}" | |
image: "{{ docker_source_image }}" | |
state: absent | |
when: docker_stop | |
- name: Push the Docker image into the repo | |
shell: docker push {{ docker_repo }}/{{ inventory_hostname }}:{{ docker_tag }} | |
when: docker_push |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment