Skip to content

Instantly share code, notes, and snippets.

@hvalls
Last active November 16, 2018 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hvalls/77cc1fee6f82845c96bd to your computer and use it in GitHub Desktop.
Save hvalls/77cc1fee6f82845c96bd to your computer and use it in GitHub Desktop.
Docker and Ansible deployment
---
- hosts: localhost
vars_files:
- '{{vars_file}}'
vars:
repo_url: '{{repo_url}}'
extension: '{{extension}}'
tasks:
- name: Download artifact from repository
maven_artifact: repository_url={{repo_url}} group_id={{group_id}} artifact_id={{artifact_id}} version={{version}}
extension={{extension}} dest={{dockerfile_path}}/build/{{artifact_id}}.{{extension}}
- name: Copy Dockerfile to downloaded artifact dir
copy: src={{dockerfile_path}}/Dockerfile dest={{dockerfile_path}}/build
- name: Copy dependencies (ficheros .properties)
copy: src={{item.path}} dest={{dockerfile_path}}/build/{{item.name}}
with_items:
- '{{dependencies}}'
- name: Build Docker image
docker_image: path={{dockerfile_path}}/build name={{image}} state=build
- name: Tag image
command: docker tag -f {{registry}}:{{image}} registr{{image}}:{{version}}
- name: Push image to private registry
command: docker push {{image}}:{{version}}
- name: Delete build directory
file: path={{dockerfile_path}}/build state=absent
#!/bin/bash
if [ $# -lt 1 ]
then
echo "Usage: $0 <vars_file>"
exit
fi
if [ ! -f $1 ]
then
echo "File $1 doesn't!"
exit
fi
ansible-playbook $(pwd)/create_dockerfile.yml --extra-vars="vars_file=$1"
---
- hosts: localhost
vars_files:
- "{{vars_file}}"
tasks:
- name: Generate and copy Dockerfile
template: src=/etc/company/deployment/templates/Dockerfile.j2 dest="/etc/company/{{artifact_id}}/Dockerfile"g
#!/bin/bash
if [ $# -lt 1 ]
then
echo "Usage: $0 <environment>"
exit
fi
ansible-playbook /etc/company/deployment/playbooks/create_vars_file.yml --extra-vars="environment=$1 path=$(pwd)"
#!/bin/bash
if [ $# -lt 3 ]
then
echo "Usage: $0 <app> <version> <environment>"
exit
fi
readonly REGISTRY="170.20.20.1:5000"
readonly BUILD_IMAGE="/etc/company/deployment/playbooks/build_image.yml"
readonly RUN_CONTAINER="/etc/company/deployment/playbooks/run_container.yml"
app_dir=/etc/company/$1
vars_file=$app_dir/vars-$3.yml
if [ ! -f $vars_file ]
then
echo "File $vars_file doesn't exist!"
exit
fi
ansible-playbook $BUILD_IMAGE --extra-vars "vars_file=$vars_file dockerfile_path=$app_dir version=$2 registry=$REGISTRY"
ansible-playbook $RUN_CONTAINER --extra-vars "vars_file=$vars_file version=$2 registry=$REGISTRY" --sudo --vault-password-file=vault.txt -vvvv
FROM {{registry}}/my-tomcat:1.0.0
ADD {{artifact_id}}.war /usr/local/tomcat/webapp/ROOT.war
{% for dependency in dependencies %}
ADD {{dependency.name}} {{dependency.path}}
{% endfor %}
CMD ["catalina.sh", "run"]
---
- hosts: "{{hosts_group}}"
serial: 1
vars_files:
- secret
- '{{vars_file}}'
remote_user: root
tasks:
- name: Run app container
docker:
name: "{{artifact_id}}"
insecure_registry: true
image: "{{registry}}/{{image}}:{{version}}"
pull: always
state: reloaded
ports:
- "{{port_mapping}}"
- name: Wait for server startup
local_action: wait_for host={{inventory_hostname}} port={{port_mapping.split(':')[0]}} timeout=50 delay=10
---
group_id: com.company
artifact_id: app
image: app_img
dependencies:
- { name: 'app.properties', path: "/etc/company/app/app.properties" }
- { name: 'login.properties', path: "/etc/company/login_service/app.properties" }
port_mapping: 8080:8080
hosts_group: test
---
group_id: #required
artifact_id: #required
image: #required
dependencies:
# - { name: 'app.properties', path: "/etc/company/app/app.properties" }
# - { name: 'login.properties', path: "/etc/company/login_service/app.properties" }
#required
port_mapping: xxxx:8080
hosts_group: {{environment}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment