Last active
August 30, 2020 09:32
-
-
Save zhangriyueming/f01107f6af42d18d698a43478dd16a5d to your computer and use it in GitHub Desktop.
This file contains 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
# Deployment playbook compatible with deployments created by capistrano | |
--- | |
# ci工作环境下的 deploy/同步到ansible本机 | |
# - hosts: ci | |
# remote_user: root | |
# tasks: | |
# - name: list the files in the folder # 列出ci服务器上deploy下的文件内容 | |
# command: ls /home/ops/deploy/ | |
# register: dir_out | |
# - name: copy from ci to local # 把deploy下的文件内容拷贝到ansible本机 | |
# fetch: | |
# src: /home/ops/deploy/{{item}} | |
# dest: /tmp/ | |
# force: yes | |
# with_items: '{{dir_out.stdout_lines}}' | |
# - name: print ip | |
# debug: var=hostvars[inventory_hostname]['ansible_eth0']['ipv4']['address'] | |
# 上传部署 | |
# - hosts: "{{ deploy | default(application) }}" | |
- hosts: deploy | |
remote_user: deploy | |
tasks: | |
- name: register hashid | |
shell: date +%Y%m%d%H%M%S | |
register: hashid | |
- name: Creates directory | |
file: | |
path: "{{ item }}" | |
state: directory | |
mode: 0775 | |
recurse: yes | |
loop: | |
- /srv/deploys/{{application}}/releases/{{hashid.stdout}} | |
- /srv/apps/ | |
- name: copy local to deploy # 上传ansible本机的文件到deploy组下 | |
copy: | |
src: "{{ lookup('env', 'PWD') }}/deploy/" | |
dest: /srv/deploys/{{application}}/releases/{{hashid.stdout}}/ | |
directory_mode: yes | |
- name: more than max_release # 只保留最新5个 | |
shell: ls -t /srv/deploys/{{application}}/releases/|tail -n +6 | |
register: files_to_delete | |
- name: "remove release folder" | |
file: | |
path: "/srv/deploys/{{application}}/releases/{{ item }}" | |
state: absent | |
with_items: "{{ files_to_delete.stdout_lines }}" | |
- name: shared link one # 创建软链接 | |
file: | |
src: /srv/deploys/{{application}}/releases/{{hashid.stdout}}/ | |
dest: /srv/deploys/{{application}}/shared | |
state: link | |
- name: deploy docker # 开始部署 | |
shell: | | |
sudo `which docker-compose` build --pull | |
sudo `which docker-compose` up -d --remove-orphans | |
args: | |
chdir /srv/deploys/{{application}}/releases/{{hashid.stdout}}/ | |
- name: shared link two # 创建软链接 | |
file: | |
src: /srv/deploys/{{application}}/releases/{{hashid.stdout}}/ | |
dest: /srv/apps/{{application}} | |
state: link |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment