-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrate_site.yml
190 lines (189 loc) · 9.7 KB
/
migrate_site.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
---
#Commentaires
# Requis : variables.yml , fourni les arguments d'import / export
#ansible-playbook migrate_site.yml
- hosts: localhost
tasks:
- name: "Charger les variables de migration"
include_vars: variables.yml
check_mode: no
- include: tasks/add_host.yml server={{ source }}
- include: tasks/add_host.yml server={{ target }}
- debug: msg="Migration de {{ source.domain }}"
- pause: prompt="Are you sure to continue ? Press return to continue. Press Ctrl+c and then "a" to abort' "
- name: Generate an OpenSSH keypair with the default values (4096 bits, rsa)
community.crypto.openssh_keypair:
path: /tmp/remote_id_ssh_rsa
type: rsa
comment: "alternc migrator {{ source.host }} -- {{ target.host }}"
- hosts: "{{ hostvars.localhost.source.host }}"
tasks:
- debug: msg="Connexion au serveur cible {{ ansible_host }}"
- name: Copy the key from master to the destination
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: '600'
with_items:
- { src: "/tmp/remote_id_ssh_rsa", dest: "~/.ssh/remote_id_rsa"}
- { src: "/tmp/remote_id_ssh_rsa.pub", dest: "~/.ssh/remote_id_rsa.pub"}
- name: Add the public key into Authorized_keys file to source server
ansible.posix.authorized_key:
state: present
user: "{{ hostvars.localhost.source.user }}"
key: "{{ lookup('file', '/tmp/remote_id_ssh_rsa.pub') }}"
- apt: name=python-mysqldb update_cache=yes state=present
become: true
- name: "Activer le mode proxy"
apache2_module: state=present name=proxy_http
when: hostvars.localhost.source.proxy|default(1)|bool
- include: tasks/get_path.yml
vars:
domain: "{{ hostvars.localhost.source.domain }}"
alternc_user: "{{ hostvars.localhost.source.alternc }}"
when: hostvars.localhost.source.path is undefined
- hosts: "{{ hostvars.localhost.target.host }}"
tasks:
- debug: msg="Connexion au serveur cible {{ ansible_host }}"
- name: Create .ssh directory
file:
path: ~/.ssh/
state: directory
mode: '700'
- name: Copy the key from master to the destination
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: '600'
with_items:
- { src: "/tmp/remote_id_ssh_rsa", dest: "~/.ssh/remote_id_rsa"}
- { src: "/tmp/remote_id_ssh_rsa.pub", dest: "~/.ssh/remote_id_rsa.pub"}
- name: Add the public key into Authorized_keys file to target server
ansible.posix.authorized_key:
state: present
user: "{{ hostvars.localhost.target.user }}"
key: "{{ lookup('file', '/tmp/remote_id_ssh_rsa.pub') }}"
- apt: name=python-mysqldb update_cache=yes state=present
become: true
- apt: name=sshpass update_cache=yes state=present
become: true
- include: tasks/get_path.yml
vars:
domain: "{{ hostvars.localhost.source.domain }}"
alternc_user: "{{ hostvars.localhost.target.alternc|default(hostvars.localhost.source.alternc) }}"
when: hostvars.localhost.target.path is undefined
- include: tasks/rsync.yml
vars:
source:
pass: "{{ hostvars.localhost.source.pass|default(omit) }}"
port: "{{ hostvars.localhost.source.port|default(22) }}"
user: "{{ hostvars.localhost.source.user }}"
host: "{{ hostvars.localhost.source.host }}"
path: "{{ hostvars.localhost.source.path|default(hostvars[hostvars.localhost.source.host]['path']) }}/"
target:
path: "{{ hostvars.localhost.target.path|default(hostvars[hostvars.localhost.target.host]['path']) }}/"
- hosts: "{{ hostvars.localhost.source.host }}"
tasks:
- name: "Activer la maintenance sur le site {{ hostvars.localhost.source.domain }}"
copy: src=templates/maintenance.html dest={{ hostvars.localhost.source.path|default(hostvars[hostvars.localhost.source.host]['path']) }}/{{ hostvars.localhost.source.cms_path|default('') }}/maintenance.html
- name: "Creer le htaccess si absent"
file: path={{ hostvars.localhost.source.path|default(hostvars[hostvars.localhost.source.host]['path']) }}/{{ hostvars.localhost.source.cms_path|default('') }}/.htaccess state=touch
- name: "Activation de la maintenance"
blockinfile:
dest: "{{ hostvars.localhost.source.path|default(hostvars[hostvars.localhost.source.host]['path']) }}/{{ hostvars.localhost.source.cms_path|default('') }}/.htaccess"
block: |
ErrorDocument 503 "Site en maintenance"
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteRule .* maintenance.html [L,R=503]
insertbefore: BOF
- name: "Export de la base mysql {{ hostvars.localhost.source.database.name }}"
mysql_db: >
login_user={{ hostvars.localhost.source.database.user }}
login_password={{ hostvars.localhost.source.database.pass }}
login_host={{ hostvars.localhost.source.database.host }}
name={{ hostvars.localhost.source.database.name }}
config_file=/dev/null
state=dump
target=/tmp/{{ hostvars.localhost.source.database.name }}.sql
when: hostvars.localhost.source.database is defined
- hosts: "{{ hostvars.localhost.target.host }}"
tasks:
- include: tasks/rsync.yml
vars:
source:
pass: "{{ hostvars.localhost.source.pass|default(omit) }}"
port: "{{ hostvars.localhost.source.port|default(22) }}"
user: "{{ hostvars.localhost.source.user }}"
host: "{{ hostvars.localhost.source.host }}"
path: "/tmp/{{ hostvars.localhost.source.database.name }}.sql"
target:
path: "/tmp/{{ hostvars.localhost.source.database.name }}.sql"
when: hostvars.localhost.source.database is defined
- name: "Corriger les DEFINER SQL"
command: sed -i 's#DEFINER.*DEFINER##g' /tmp/{{ hostvars.localhost.source.database.name }}.sql
args:
chdir: "/tmp/"
when: hostvars.localhost.target.database is defined
- mysql_db: >
config_file=""
login_user={{ hostvars.localhost.target.database.user | default(hostvars.localhost.source.database.user) }}
login_password={{ hostvars.localhost.target.database.pass | default(hostvars.localhost.source.database.pass) }}
login_host={{ hostvars.localhost.target.database.host | default(hostvars.localhost.source.database.host) }}
name={{ hostvars.localhost.target.database.name | default(hostvars.localhost.source.database.name) }}
config_file=/dev/null
state=import
single_transaction=yes
target=/tmp/{{ hostvars.localhost.source.database.name }}.sql
when: hostvars.localhost.source.database is defined or hostvars.localhost.target.database is defined
- name: "Synchroniser les informations manquantes depuis l'import initial"
include: tasks/rsync.yml
vars:
source:
pass: "{{ hostvars.localhost.source.pass|default(omit) }}"
port: "{{ hostvars.localhost.source.port|default(22) }}"
user: "{{ hostvars.localhost.source.user }}"
host: "{{ hostvars.localhost.source.host }}"
path: "{{ hostvars.localhost.source.path|default(hostvars[hostvars.localhost.source.host]['path']) }}/"
target:
path: "{{ hostvars.localhost.target.path|default(hostvars[hostvars.localhost.target.host]['path']) }}/"
- name: "Corriger tous les appels aux anciens chemin avec le nouveau"
command: find . -type f -exec sed -i 's#{{ hostvars.localhost.source.path|default(hostvars[hostvars.localhost.source.host]['path']) }}/#{{ hostvars.localhost.target.path|default(hostvars[hostvars.localhost.target.host]['path']) }}/#g' {} +
args:
chdir: "{{ hostvars.localhost.target.path|default(hostvars[hostvars.localhost.target.host]['path']) }}/"
- name: "Corriger la connexion a la db"
include: "{{ item }}"
vars:
path: "{{ hostvars.localhost.target.path|default(hostvars[hostvars.localhost.target.host]['path']) }}/{{ hostvars.localhost.source.cms_path|default('') }}"
database: "{{ hostvars.localhost.source.database | combine(hostvars.localhost.target.database) }}"
when: hostvars.localhost.source.database is defined or hostvars.localhost.target.database is defined
with_first_found:
- files:
- tasks/db/{{ hostvars.localhost.source.cms }}.yml
skip: true
ignore_errors: True>
- name: "Corriger les droits sur le repertoire"
command: /usr/lib/alternc/fixperms.sh -l {{ hostvars.localhost.target.alternc|default(hostvars.localhost.source.alternc) }}
- hosts: "{{ hostvars.localhost.source.host }}"
tasks:
- stat:
path: /etc/apache2/conf.d
register: apache2confd
- stat:
path: /etc/apache2/conf-enabled
register: apache2confenabled
- name: Fetch Apache version
shell: apache2ctl -v |head -n 1 |sed 's/[^[:digit:]\.]//g'
register: apache_version
- name: "Activation du mode proxy : conf.d"
template: src=templates/proxy.j2 dest=/etc/apache2/conf.d/000-proxy-{{ hostvars.localhost.source.domain }}.conf
when: apache2confd.stat.isdir is defined and apache2confd.stat.isdir and hostvars.localhost.source.proxy|default(1)|bool
- name: "Activation du mode proxy : conf-enabled"
template: src=templates/proxy.j2 dest=/etc/apache2/conf-enabled/000-proxy-{{ hostvars.localhost.source.domain }}.conf
when: apache2confenabled.stat.isdir is defined and apache2confenabled.stat.isdir and hostvars.localhost.source.proxy|default(1)|bool
- name: "Relancer apache"
shell: apache2ctl restart
when: ansible_distribution == 'Debian'
- name: "Relancer apache"
shell: /etc/init.d/apache2 restart
when: ansible_distribution == 'Ubuntu'