-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplaybook.yml
191 lines (163 loc) · 5.44 KB
/
playbook.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
191
---
# based on Digital Ocean's installation guide
# https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04
- hosts: all
sudo: True
remote_user: root
vars:
ipv4_addr: '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
redis_user: redis
redis_group: redis
# change for production
redis_bind_addr: 127.0.0.1
redis_port: 6379
redis_tcp_sockets: 511
redis_socket: /var/run/redis/redis.sock
redis_socket_permissions: 770
redis_logfile: /var/log/redis/redis-server.log
# generate a 32-character password with apg:
# apg -m 32 -x 1 -a 1 -n 1
redis_password: redis
# install python 2.7 on Ubuntu > 14.x
# solves "/bin/sh: 1: /usr/bin/python: not found" issue with
# later Ubuntu releases
gather_facts: no
pre_tasks:
- name: 'install python2'
raw: sudo apt-get -y install python-simplejson
tasks:
- name: update apt cache
apt: update_cache=yes
- name: install required packages
apt: name={{ item }} state=present
with_items:
- build-essential
- tcl
- name: download redis src
# remote_src broken in ~ 2.1
# https://github.com/ansible/ansible-modules-core/issues/4752
unarchive:
src: http://download.redis.io/redis-stable.tar.gz
copy: no
dest: /tmp
mode: u=rwx,g=rx,o=rx
owner: root
creates: /tmp/redis-stable
- name: make redis
make: chdir=/tmp/redis-stable
- name: test and install redis
make: chdir=/tmp/redis-stable target={{ item }}
become: yes
with_items:
- test
- install
- name: create /etc/redis, data, logs, and socket directories
file: path={{ item }} state=directory
with_items:
- /etc/redis
- /var/lib/redis
- /var/log/redis
- /var/run/redis
- name: copy default redis.conf
# create a copy of the default redis.conf
copy: remote_src=True
src=/tmp/redis-stable/redis.conf
dest=/etc/redis/redis.conf
become: yes
- name: configure systemmd
lineinfile: dest=/etc/redis/redis.conf
regexp='^supervised no$'
line='supervised systemd'
state=present
- name: store redis data in /var/lib/redis
lineinfile: dest=/etc/redis/redis.conf
regexp='^dir ./$'
line='dir /var/lib/redis'
state=present
- name: secure redis
lineinfile:
dest: /etc/redis/redis.conf
regexp: '^# requirepass \w*$'
line: 'requirepass {{ redis_password }}'
state: present
- name: bind to redis.bind_addr
lineinfile:
dest: /etc/redis/redis.conf
regexp: '^bind {{ ipv4_addr }}$'
line: 'bind {{ redis_bind_addr }}'
state: present
- name: configure logging
lineinfile:
dest: /etc/redis/redis.conf
regexp: '^logfile ""$'
line: 'logfile {{ redis_logfile }}'
state: present
- name: Check rc.local exists
stat:
path: /etc/rc.local
register: rc_local
- name: Create rc.local if not present
when: rc_local.islnk is not defined
template:
src: templates/rc.local
dest: /etc/rc.local
mode: '0755'
- name: disable Transparent Huge Pages (THP) support
lineinfile: dest=/etc/rc.local
insertbefore='^exit 0$'
line='echo never > /sys/kernel/mm/transparent_hugepage/enabled'
state=present
become: yes
- name: fix TCP backlog
lineinfile: dest=/etc/rc.local
insertbefore='^exit 0$'
line='net.core.somaxconn={{ redis_tcp_sockets }}'
state=present
become: yes
- command: sysctl -w net.core.somaxconn={{ redis_tcp_sockets }}
become: yes
- name: enable low-memory background saves
lineinfile: dest=/etc/sysctl.conf
regexp=''
insertafter=EOF
line='vm.overcommit_memory = 1'
become: yes
- command: sysctl vm.overcommit_memory=1
become: yes
- name: create redis-as-a-service
template: src=templates/redis.service.j2
dest=/etc/systemd/system/redis.service
owner=root
group=root
mode=0644
- name: create redis group
group: name=redis state=present
- name: create redis user
user: name={{ redis_user }}
group={{ redis_group }}
shell=/bin/bash
createhome=no
- name: enable unix socket support
lineinfile: dest=/etc/redis/redis.conf
regexp='^unixsocket {{ redis_socket }}$'
line='unixsocket {{ redis_socket }}'
state=present
insertafter=EOF
create=True
- name: configure unix socket permissions
lineinfile: dest=/etc/redis/redis.conf
regexp='^unixsocketperm {{ redis_socket_permissions }}$'
line='unixsocketperm {{ redis_socket_permissions }}'
state=present
insertafter=EOF
create=True
- name: set permissions for redis
file: path={{ item }} state=touch owner=redis group=redis mode=0770
with_items:
- /etc/redis
- /var/lib/redis
- /var/log/redis
- /var/run/redis
become: yes
- name: start redis
service: name=redis state=restarted enabled=yes