playbooks/iot/playbook.yml

235 lines
6.8 KiB
YAML
Raw Normal View History

#################################################
# DO Community Playbooks: Docker
#################################################
---
- hosts: all
become: true
vars_files:
- vars/default.yml
tasks:
- getent:
database: passwd
key: "{{ mqtt_user }}"
split: ":"
# - name: "{{ getent_passwd[user][1] }} : {{ getent_passwd[user][2] }}"
# docker_image:
# name: "{{ mqtt_imag }}"
# source: pull
# force_source: yes
- name: Pull Mosquitto image
docker_image:
name: "{{ mqtt_image }}"
source: pull
force_source: yes
- name: Ensure Mosquitto Directory exists
file:
path: "{{ mqtt_directory }}"
state: directory
group: 0 # "{{ mqtt_user }}"
owner: 0 # "{{ mqtt_user }}"
mode: 0755
- name: Ensure Mosquitto Config Directory exists
file:
path: "{{ mqtt_directory }}/config"
state: directory
group: 0 # "{{ mqtt_user }}"
owner: 0 # "{{ mqtt_user }}"
mode: 0755
- name: Copy the Mosquitto conf file
template:
src: "files/mosquitto.conf"
dest: "{{ mqtt_directory }}/config/mosquitto.conf"
group: 1883 # "{{ mqtt_user }}"
owner: 1883 # "{{ mqtt_user }}"
mode: 0646
# - name: Check if Mosquitto passwd file exists
# stat: path="{{ mqtt_directory }}/config/passwd"
# register: status
- name: Ensure Mosquitto passwd file exists
template:
src: "files/passwd"
dest: "{{ mqtt_directory }}/config/passwd"
group: 1883 # "{{ mqtt_user }}"
owner: 1883 # "{{ mqtt_user }}"
mode: 0646
- name: Encrypt the docker file
command: docker exec -i mosquitto mosquitto_passwd -U /mosquitto/config/passwd
- name: Ensure Mosquitto Log Directory exists
file:
path: "{{ mqtt_directory }}/log"
state: directory
group: 0 # "{{ mqtt_user }}"
owner: 0 # "{{ mqtt_user }}"
mode: 0755
- name: Ensure Mosquitto log file exists
file:
path: "{{ mqtt_directory }}/log/mosquitto.log"
state: touch
group: 0 # "{{ mqtt_user }}"
owner: 0 # "{{ mqtt_user }}"
mode: 0646
- name: Create Mosquitto container
docker_container:
name: "{{ mqtt_container }}"
image: "{{ mqtt_image }}"
state: started
restart_policy: "unless-stopped"
# user: 0:0 # "{{ getent_passwd[mqtt_user][1] }}:{{ getent_passwd[mqtt_user][2] }}"
published_ports:
- 127.0.0.1:9001:9001
- 1883:1883
volumes:
- "{{ mqtt_directory }}/config:/mosquitto/config"
- "{{ mqtt_directory }}/data:/mosquitto/data"
- "{{ mqtt_directory }}/log:/mosquitto/log"
- getent:
database: passwd
key: "{{ user }}"
split: ":"
- name: Pull InfluxDB image
docker_image:
name: "{{ influx_image }}"
source: pull
force_source: yes
- name: Ensure Influx Directory exists
file:
path: "{{ influx_directory }}"
state: directory
group: "{{ user }}"
owner: "{{ user }}"
mode: 0755
- name: Ensure Influx Data Directory exists
file:
path: "{{ influx_directory }}/data"
state: directory
group: "{{ user }}"
owner: "{{ user }}"
mode: 0755
- name: Ensure Influx Config Directory exists
file:
path: "{{ influx_directory }}/config"
state: directory
group: "{{ user }}"
owner: "{{ user }}"
mode: 0755
- name: Create InfluxDB container
docker_container:
name: "{{ influx_container }}"
image: "{{ influx_image }}"
state: started
restart_policy: "unless-stopped"
user: "{{ getent_passwd[user][1] }}:{{ getent_passwd[user][2] }}"
published_ports:
- 127.0.0.1:8086:8086
volumes:
- "{{ influx_directory }}/data:/var/lib/influxdb2"
- "{{ influx_directory }}/config:/etc/influxdb2"
- name: Pull Node Red image
docker_image:
name: "{{ nodered_image }}"
source: pull
force_source: yes
- name: Ensure Node Red Directory exists
file:
path: "{{ nodered_directory }}"
state: directory
group: "{{ user }}"
owner: "{{ user }}"
mode: 0755
- name: Create Node Red container
docker_container:
name: "{{ nodered_container }}"
image: "{{ nodered_image }}"
state: started
restart_policy: "unless-stopped"
user: "{{ getent_passwd[user][1] }}:{{ getent_passwd[user][2] }}"
env:
TZ: "Asia/Kolkata"
links:
- "{{ mqtt_container }}:mqtt"
- "{{ influx_container }}:influx"
published_ports:
- 127.0.0.1:1880:1880
volumes:
- "{{ nodered_directory }}:/data"
- name: Pull Grafana image
docker_image:
name: "{{ grafana_image }}"
source: pull
force_source: yes
- name: Ensure Grafana Directory exists
file:
path: "{{ grafana_directory }}"
state: directory
group: "{{ user }}"
owner: "{{ user }}"
mode: 0755
- name: Create Grafana container
docker_container:
name: "{{ grafana_container }}"
image: "{{ grafana_image }}"
state: started
restart_policy: "unless-stopped"
user: "{{ getent_passwd[user][1] }}:{{ getent_passwd[user][2] }}"
env:
2023-07-13 09:34:43 +00:00
GF_SERVER_ROOT_URL: "https://{{ http_host }}"
GF_INSTALL_PLUGINS: "grafana-clock-panel, grafana-simple-json-datasource, natel-discrete-panel, briangann-gauge-panel, vonage-status-panel, neocat-cal-heatmap-panel, natel-plotly-panel"
links:
- "{{ influx_container }}:influx"
published_ports:
- 127.0.0.1:3005:3000
volumes:
- "{{ grafana_directory }}:/var/lib/grafana"
- name: Check if Nginx conf file exists
stat: path="/etc/nginx/sites-available/{{ http_conf }}"
register: status
- name: No need to reload Nginx
debug: msg= {{ "No need to reload Nginx as sites-available entries have already been created" }}
- name: Set Nginx conf file
when: status.stat.exists == false
template:
src: "files/nginx.conf.j2"
dest: "/etc/nginx/sites-available/{{ http_conf }}"
- name: Enable new site
when: status.stat.exists == false
file:
src: "/etc/nginx/sites-available/{{ http_conf }}"
dest: "/etc/nginx/sites-enabled/{{ http_conf }}"
state: link
notify: Reload Nginx
handlers:
- name: Reload Nginx
service:
name: nginx
state: reloaded