Download - Ansible Project Deploy (phpbenelux 2015)
![Page 1: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/1.jpg)
ANSIBLE-PROJECT-DEPLOYa re-usable Ansible role to deploy projects
![Page 2: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/2.jpg)
ABOUT ME
2
Ramon de la Fuente
Future500 B.V.
@f_u_e_n_t_e
SweetlakePHP
![Page 3: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/3.jpg)
WHY ANSIBLE?• Easy. Period.
3
“I wrote Ansible because none of the existing tools fit my brain.”
- Michael de Haan
![Page 4: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/4.jpg)
WHY ANSIBLE?• Easy. Period.
• No unnecessary complexity → No agent!
4
![Page 5: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/5.jpg)
WHY ANSIBLE?• Easy. Period.
• No unnecessary complexity → No agent!
• Built for re-use and sharing.
5
![Page 6: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/6.jpg)
WHY ANSIBLE?• Easy. Period.
• No unnecessary complexity → No agent!
• Built for re-use and sharing.
• Extendable in your own language.
6
![Page 7: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/7.jpg)
THE PROBLEM• Continuous deployment
7
![Page 8: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/8.jpg)
THE PROBLEM• Continuous deployment
• Easy maintenance of the deploy procedure.
8
![Page 9: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/9.jpg)
THE PROBLEM• Continuous deployment
• Easy maintenance of the deploy procedure.
• Small learning curve.
9
![Page 10: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/10.jpg)
THE PROBLEM• Continuous deployment
• Easy maintenance of the deploy procedure.
• Small learning curve.
• Reuse between projects with little effort.
10
![Page 11: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/11.jpg)
WHAT IS A DEPLOY?Directory structure:
.
!"" releases | !"" 20140415234508 | #"" 20140415235146 !"" shared | !"" sessions | !"" source | #"" uploads #"" current -> releases/20140415235146
11
![Page 12: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/12.jpg)
WHAT IS A DEPLOY?Directory structure:
.
!"" releases | !"" 20140415234508 | #"" 20140415235146 !"" shared | !"" sessions | !"" source | #"" uploads #"" current -> releases/20140415235146
12
![Page 13: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/13.jpg)
WHAT IS A DEPLOY?1. Update the codebase + configuration
13
![Page 14: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/14.jpg)
WHAT IS A DEPLOY?1. Update the codebase + configuration
2. Install dependencies
14
![Page 15: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/15.jpg)
WHAT IS A DEPLOY?1. Update the codebase + configuration
2. Install dependencies
3. Preserve shared resources
15
![Page 16: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/16.jpg)
WHAT IS A DEPLOY?1. Update the codebase + configuration
2. Install dependencies
3. Preserve shared resources
4. Build tasks
16
![Page 17: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/17.jpg)
WHAT IS A DEPLOY?1. Update the codebase + configuration
2. Install dependencies
3. Preserve shared resources
4. Build tasks
5. Finalize
17
![Page 18: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/18.jpg)
THE ROLE
18
https://galaxy.ansible.com/list#/roles/732project_deploy
![Page 19: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/19.jpg)
GETTING THE ROLEInstallation with ansible-galaxy command:
$ ansible-galaxy install f500.project_deploy,v2.1.0
Optional: create a galaxy file for all roles:
f500.nginx
f500.mariadb55
f500.php
f500.project_deploy,v2.1.0
$ ansible-galaxy install -r ansible/galaxy.txt
19
![Page 20: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/20.jpg)
ROLE WALKTHROUGH---
- name: Initialize
deploy_helper: "path={{ project_root }} state=present"
20
![Page 21: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/21.jpg)
ROLE WALKTHROUGH Deploy module variables:
deploy_helper:
project_path
current_path
releases_path
shared_path
previous_release
previous_release_path
new_release
new_release_path
unfinished_filename
21
![Page 22: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/22.jpg)
ROLE WALKTHROUGH Deploy module variables:
deploy_helper:
project_path: /path/to/project/
current_path: /path/to/project/current
releases_path: /path/to/project/releases
shared_path: /path/to/project/shared
previous_release: 20140415234508
previous_release_path: /path/to/project/releases/20140415234508
new_release: 20140415235146
new_release_path: /path/to/project/releases/20140415235146
unfinished_filename: DEPLOY_UNFINISHED
Used as:
{{ deploy_helper.new_release }}
22
![Page 23: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/23.jpg)
1. UPDATE THE CODEBASE- name: Clone project files
git: repo={{ project_git_repo }}
dest={{ project_source_path }}
version={{ project_version }}
when: project_deploy_strategy == 'git'
- name: Rsync project files
synchronize: src={{ project_local_path }}
dest={{ project_source_path }}
rsync_timeout={{ project_deploy_synchronize_timeout }}
recursive=yes
when: project_deploy_strategy == 'synchronize'
23
![Page 24: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/24.jpg)
1. UPDATE THE CODEBASE- name: Clone project files
git: repo={{ project_git_repo }}
dest={{ project_source_path }}
version={{ project_version }}
when: project_deploy_strategy == 'git'
- name: Rsync project files
synchronize: src={{ project_local_path }}
dest={{ project_source_path }}
rsync_timeout={{ project_deploy_synchronize_timeout }}
recursive=yes
when: project_deploy_strategy == 'synchronize'
24
![Page 25: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/25.jpg)
1. UPDATE THE CODEBASE- name: Clone project files
git: repo={{ project_git_repo }}
dest={{ project_source_path }}
version={{ project_version }}
when: project_deploy_strategy == 'git'
- name: Rsync project files
synchronize: src={{ project_local_path }}
dest={{ project_source_path }}
rsync_timeout={{ project_deploy_synchronize_timeout }}
recursive=yes
when: project_deploy_strategy == 'synchronize'
25
![Page 26: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/26.jpg)
1. UPDATE THE CODEBASE- name: Write unfinished file
file: path={{ project_source_path }}/{{ deploy_helper.unfinished_filename }}
state=touch
- name: Copy files to new build dir
command: "cp -pr {{project_source_path}} {{deploy_helper.new_release_path}}"
- name: Remove unwanted files/folders from new release
file: path={{ deploy_helper.new_release_path }}/{{ item }} state=absent
with_items: project_unwanted_items
26
![Page 27: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/27.jpg)
1. UPDATE THE CONFIG FILES- name: Copy project files
copy: src={{ item.src }}
dest={{ deploy_helper.new_release_path }}/{{ item.dest }}
mode={{ item.mode|default('0644') }}
with_items: project_files
- name: Copy project templates
template: src={{ item.src }}
dest={{ deploy_helper.new_release_path }}/{{ item.dest }}
mode={{ item.mode|default('0644') }}
with_items: project_templates
27
![Page 28: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/28.jpg)
2. INSTALL DEPENDENCIES- name: Do composer install
command: "{{ project_command_for_composer_install }} chdir=…"
environment: project_environment
when: project_has_composer
- name: Do npm install
command: "{{ project_command_for_npm_install }} chdir=…"
environment: project_environment
when: project_has_npm
- name: Do bower install
command: "{{ project_command_for_bower_install }} chdir=…"
environment: project_environment
when: project_has_bower
28
![Page 29: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/29.jpg)
2. INSTALL DEPENDENCIES- name: Do composer install
command: "{{ project_command_for_composer_install }} chdir=…"
environment: project_environment
when: project_has_composer
- name: Do npm install
command: "{{ project_command_for_npm_install }} chdir=…"
environment: project_environment
when: project_has_npm
- name: Do bower install
command: "{{ project_command_for_bower_install }} chdir=…"
environment: project_environment
when: project_has_bower
29
![Page 30: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/30.jpg)
3. SHARED RESOURCES- name: Ensure shared sources are present
file: path='{{ deploy_helper.shared_path }}/{{ item.src }}'
state={{ item.type }}
with_items: project_shared_children
- name: Ensure shared paths are absent
file: path='{{ deploy_helper.new_release_path }}/{{ item.path }}'
state=absent
with_items: project_shared_children
- name: Create shared symlinks
file: path='{{ deploy_helper.new_release_path }}/{{ item.path }}'
src='{{ deploy_helper.shared_path }}/{{ item.src }}'
state=link"
with_items: project_shared_children
30
![Page 31: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/31.jpg)
4. BUILD STEPS- name: Run post_build_commands in the new_release_path
command: "{{ item }} chdir={{ deploy_helper.new_release_path }}"
with_items: project_post_build_commands
environment: project_environment
31
project_post_build_commands:
- "app/console cache:clear"
- "app/console assets:install"
- "app/console assetic:dump"
![Page 32: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/32.jpg)
5. FINALIZE- name: Finalize the deploy deploy_helper: path={{ project_root }}
release={{ deploy_helper.new_release }}
state=finalize when: project_finalize
32
![Page 33: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/33.jpg)
33
![Page 34: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/34.jpg)
IT’S NOT COMPLICATED!• Only 98 lines
• Number of tasks: 22
• Variables to configure: 28
34
![Page 35: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/35.jpg)
MINIMAL PLAYBOOK
1. Set minimum variables
2. Add the role to “roles” section
35
![Page 36: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/36.jpg)
MINIMAL PLAYBOOK- name: Deploy the application
hosts: production
remote_user: deploy
sudo: no
vars:
project_root: /var/www/my_project
project_git_repo: [email protected]:me/my_project.git
project_deploy_strategy: git
roles:
- f500.project_deploy
36
![Page 37: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/37.jpg)
EXAMPLE PLAYBOOK- name: Deploy the application
hosts: production
remote_user: "{{ production_deploy_user }}"
sudo: no
vars:
project_root: "{{ sweetlakephp_root }}"
project_git_repo: "{{ sweetlakephp_github_repo }}"
project_deploy_strategy: git
37
![Page 38: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/38.jpg)
EXAMPLE PLAYBOOK- name: Deploy the application
hosts: production
remote_user: "{{ production_deploy_user }}"
sudo: no
vars:
project_root: "{{ sweetlakephp_root }}"
project_git_repo: "{{ sweetlakephp_github_repo }}"
project_deploy_strategy: git
project_environment:
SYMFONY_ENV: "prod"
38
![Page 39: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/39.jpg)
EXAMPLE PLAYBOOK project_environment:
SYMFONY_ENV: "prod"
project_shared_children:
- path: "/app/sessions"
src: "sessions"
- path: "/web/uploads"
src: "uploads"
project_templates:
- name: parameters.yml
src: "templates/parameters_prod.yml.j2"
dest: "/app/config/parameters_prod.yml"
39
![Page 40: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/40.jpg)
EXAMPLE PLAYBOOK project_environment:
SYMFONY_ENV: "prod"
project_shared_children:
- path: "/app/sessions"
src: "sessions"
- path: "/web/uploads"
src: "uploads"
project_templates:
- name: parameters.yml
src: "templates/parameters_prod.yml.j2"
dest: "/app/config/parameters.yml"
40
![Page 41: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/41.jpg)
EXAMPLE PLAYBOOK project_has_composer: yes
project_post_build_commands:
- "php vendor/sensio/…/DistributionBundle/…/bin/build_bootstrap.php”
- "app/console cache:clear"
- "app/console doctrine:migrations:migrate --no-interaction"
- "app/console assets:install"
- "app/console assetic:dump"
roles:
- f500.project_deploy
41
![Page 42: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/42.jpg)
EXAMPLE PLAYBOOK project_has_composer: yes
project_post_build_commands:
- "php vendor/sensio/…/DistributionBundle/…/bin/build_bootstrap.php”
- "app/console cache:clear"
- "app/console doctrine:migrations:migrate --no-interaction"
- "app/console assets:install"
- "app/console assetic:dump"
roles:
- f500.project_deploy
42
![Page 43: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/43.jpg)
EXAMPLE PLAYBOOK
43
project_has_composer: yes
project_post_build_commands:
- "php vendor/sensio/…/DistributionBundle/…/bin/build_bootstrap.php”
- "app/console cache:clear"
- "app/console doctrine:migrations:migrate --no-interaction"
- "app/console assets:install"
- "app/console assetic:dump"
roles:
- f500.project_deploy
![Page 44: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/44.jpg)
WHAT DOESN’T IT DO?
44
• Rollbacks (the cake rollback is a lie)
![Page 45: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/45.jpg)
WHAT DOESN’T IT DO?
45
• Rollbacks
• Set maintenance mode
(the cake rollback is a lie)
![Page 46: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/46.jpg)
WHAT DOESN’T IT DO?
46
• Rollbacks
• Set maintenance mode
• DB migrations
(the cake rollback is a lie)
![Page 47: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/47.jpg)
WHAT’S NEXT?
47
• Injecting your own tasks in addition to commands
![Page 48: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/48.jpg)
WHAT’S NEXT?
48
• Injecting your own tasks in addition to commands
• Copy vendor folders from previous release ✓
![Page 49: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/49.jpg)
WHAT’S NEXT?
49
• Injecting your own tasks in addition to commands
• Copy vendor folders from previous release
• Setfacl support
✓
![Page 50: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/50.jpg)
WHAT’S NEXT?
50
• Injecting your own tasks in addition to commands
• Copy vendor folders from previous release
• Setfacl support
• Your ideas?
✓
![Page 51: Ansible Project Deploy (phpbenelux 2015)](https://reader034.vdocuments.us/reader034/viewer/2022042716/55a7869f1a28ab70188b45f7/html5/thumbnails/51.jpg)
THANK YOU!
51
Feedback: joind.in 13405
f500/ansible-project_deploy
(But I’m also just a human. You could talk to me and tell me what you think…)
https://github.com/