-
Notifications
You must be signed in to change notification settings - Fork 1
/
CentOS7x_Install-Mosquitto-playbook.yml
223 lines (181 loc) · 8.04 KB
/
CentOS7x_Install-Mosquitto-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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
################################################################################
# description: Installs Mosquitto on CentOS7x
# usage: ansible-playbook CentOS7x_Install-Mosquitto-playbook.yml --extra-vars 'HostOrGroup=YourServerOrGroupNameGoesHere'
# author: Ernest G. Wilson II <[email protected]> (https://github.com/ernestgwilsonii)
# license: MIT
################################################################################
# Ansible Playbook options
# REF: http://docs.ansible.com/ansible/playbooks.html
#####################################################
- name: Install Mosquitto on CentOS7x
hosts: "{{ HostOrGroup|default ('FATAL ERROR --> HostOrGroup NOT SET! You must specify either a Host or a Group name!') }}"
serial: "100%"
gather_facts: False
tasks:
# Install or configure repository used on the OS by yum
# REF: https://docs.ansible.com/ansible/yum_repository_module.html
##################################################################
# /etc/yum.repos.d/mosquitto.repo
# https://mosquitto.org/download/
- name: Install repository Mosquitto
yum_repository:
name: mosquitto
description: Mosquitto Repository
file: mosquitto
baseurl: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/
enabled: yes
gpgcheck: yes
gpgkey: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7//repodata/repomd.xml.key
# Install yum packages (listed in alphabetical order)
# REF: http://docs.ansible.com/ansible/yum_module.html
######################################################
- name: Install mosquitto
yum:
name: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/x86_64/mosquitto-1.4.10-3.1.x86_64.rpm
state: present
- name: Install libmosquitto1
yum:
name: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/x86_64/libmosquitto1-1.4.10-3.1.x86_64.rpm
state: present
- name: Install mosquitto-clients
yum:
name: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/x86_64/mosquitto-clients-1.4.10-3.1.x86_64.rpm
state: present
- name: Install libmosquitto-devel
yum:
name: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/x86_64/libmosquitto-devel-1.4.10-3.1.x86_64.rpm
state: present
- name: Install libmosquittopp1
yum:
name: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/x86_64/libmosquittopp1-1.4.10-3.1.x86_64.rpm
state: present
- name: Install libmosquittopp-devel
yum:
name: http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/x86_64/libmosquitto-devel-1.4.10-3.1.x86_64.rpm
state: present
# Restart firewalld to make sure it is running OK before changing any rules
# REF: http://docs.ansible.com/ansible/service_module.html
##########################################################
- name: Ensure the firewalld service is enabled and re-started before changing any rules
service:
name: firewalld
enabled: yes
state: restarted
# Update the firewalld configuration
# REF: http://docs.ansible.com/ansible/firewalld_module.html
############################################################
- name: Update firewall to allow port TCP 1883 for incoming mosquitto connections
firewalld:
port: 1883/tcp
permanent: true
immediate: yes
state: enabled
zone: public
- name: Update firewall to allow port TCP 8883 for incoming encrypted mosquitto connections
firewalld:
port: 8883/tcp
permanent: true
immediate: yes
state: enabled
zone: public
- name: Update firewall to allow port TCP 9001 for incoming websockets mosquitto connections
firewalld:
port: 9001/tcp
permanent: true
immediate: yes
state: enabled
zone: public
- name: Update firewall to allow port TCP 9883 for incoming encrypted websockets mosquitto connections
firewalld:
port: 9883/tcp
permanent: true
immediate: yes
state: enabled
zone: public
# command - Executes a command on a remote node
# REF: http://docs.ansible.com/ansible/command_module.html
##########################################################
- name: Get the hostname from the remote OS and use it to set a variable
command: hostname
register: hostname
- debug: msg="Remote hostname is {{hostname.stdout}}"
# Use the copy module to copy various files into place
# REF: http://docs.ansible.com/ansible/copy_module.html
#######################################################
# /usr/lib/systemd/system/mosquitto.service
- name: Copy mosquitto.service from Ansible files/Mosquitto/mosquitto.service to Mosquitto remote /usr/lib/systemd/system/mosquitto.service
copy:
src=files/Mosquitto/mosquitto.service
dest=/usr/lib/systemd/system/mosquitto.service
owner=root
group=root
mode=0400
- name: Copy and decrypt ca.pem from Ansible files/Mosquitto/certs/ca.pem to Mosquitto remote /etc/mosquitto/certs/ca.pem
copy:
src=files/Mosquitto/certs/ca.crt
dest=/etc/mosquitto/certs/ca.crt
owner=mosquitto
group=root
mode=0444
- name: Copy and decrypt ca.key from Ansible files/Mosquitto/certs/ca.key to Mosquitto remote /etc/mosquitto/certs/ca.key
copy:
src=files/Mosquitto/certs/ca.key
dest=/etc/mosquitto/certs/ca.key
owner=root
group=root
mode=0400
- name: Copy and decrypt ca.srl from Ansible files/Mosquitto/certs/ca.srl to Mosquitto remote /etc/mosquitto/certs/ca.srl
copy:
src=files/Mosquitto/certs/ca.srl
dest=/etc/mosquitto/certs/ca.srl
owner=root
group=root
mode=0640
- name: Copy and decrypt generate-CA.sh from Ansible files/Mosquitto/certs/generate-CA.sh to Mosquitto remote /etc/mosquitto/certs/generate-CA.sh
copy:
src=files/Mosquitto/certs/generate-CA.sh
dest=/etc/mosquitto/certs/generate-CA.sh
owner=root
group=root
mode=0540
# Execute raw command(s)
# REF: http://docs.ansible.com/ansible/raw_module.html
##########################################################
- name: Generate certs on remote via /etc/mosquitto/certs/generate-CA.sh
raw: cd /etc/mosquitto/certs && /etc/mosquitto/certs/generate-CA.sh
# Use the template module to populate files with data
# REF: http://docs.ansible.com/ansible/template_module.html
###########################################################
# /etc/mosquitto/mosquitto.conf
- name: Fill in the variables and copy templates/Mosquitto/mosquitto.conf.j2 from Ansible to Mosquitto remote /etc/mosquitto/mosquitto.conf
template:
src=templates/Mosquitto/mosquitto.conf.j2
dest=/etc/mosquitto/mosquitto.conf
owner=root
group=root
mode=0644
# Start mosquitto
# REF: http://docs.ansible.com/ansible/service_module.html
##########################################################
- name: Ensure the mosquitto service is enabled and started
service:
name=mosquitto.service
enabled=yes
state=restarted
# Remote SSH command line trouble-shooting commands:
# systemctl status mosquitto
# cat /usr/lib/systemd/system/mosquitto.service
# cat /etc/mosquitto/mosquitto.conf
# tail -f /var/log/messages
# ss -ntlup | grep 1883
# ss -ntlup | grep 8883
# ss -ntlup | grep 9001
# ss -ntlup | grep 9883
# lsof -P | grep IPv4 | sort -t: -k2,2n | grep 1883
# mosquitto_sub -h localhost -t test # Subscribe to a topic in one SSH session
# mosquitto_pub -h localhost -t test -m "hello world" # Publish to a topic in another SSH session
# mosquitto_sub -h localhost -v -t "#" # Subscribe to all
# mosquitto_sub --cafile /etc/mosquitto/certs/ca.crt -h localhost -p 8883 -v -t "#" # Encrypted subscribe to all
# mosquitto_pub --cafile /etc/mosquitto/certs/ca.crt -h localhost -t "test" -m "message" -p 8883 # Encrypted publish
# mosquitto_sub -t \$SYS/broker/bytes/\# -v --cafile /etc/mosquitto/certs/ca.crt -p 8883 # Encrypted read bytes