-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathexample3b.yml
42 lines (35 loc) · 1.38 KB
/
example3b.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
- name: Build an image by running ansible in a container (improved)
hosts: vagrant
tasks:
- name: check for existence of our base image
shell: "docker images | grep built-by-ansible | grep ex3"
ignore_errors: yes
register: image_check
- name: determine the base image and temp container names
set_fact:
base_image: "{{'built-by-ansible:ex3' if image_check.rc == 0 else 'ansible/ubuntu14.04-ansible:latest'}}"
temp_container_name: ex3_build_{{lookup('pipe', 'date "+%Y%m%d%H%M%S"')}}
- name: upload the site directory to the docker host
synchronize: src=site dest=/tmp
- name: build site by running ansible in a docker container
command: "docker run
-v /tmp/site:/site
-w /site
--name={{temp_container_name}}
{{base_image}}
ansible-playbook playbook.yml -c local"
- name: create a docker image from the container
command: "docker commit
-c 'EXPOSE 80'
-c 'CMD [\"nginx\", \"-g\", \"daemon off;\"]'
{{temp_container_name}}
built-by-ansible:ex3"
- name: delete the container once the image has been successfully built
command: docker rm -f -v {{temp_container_name}}
- name: run the site in a docker container
docker:
name: site3
image: "built-by-ansible:ex3"
state: reloaded
publish_all_ports: yes