101 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			YAML
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			YAML
		
	
	
	
	
	
| ---
 | |
| - name: "Install Netdata monitoring service"
 | |
|   hosts: all
 | |
| 
 | |
|   vars_files:
 | |
|     - vars/ports.yml
 | |
|     - vars/secrets.yml
 | |
| 
 | |
|   vars:
 | |
|     app_name: "netdata"
 | |
|     app_user: "{{ app_name }}"
 | |
|     base_dir: "/home/{{ app_user }}"
 | |
|     config_dir: "{{ (base_dir, 'config') | path_join }}"
 | |
|     config_go_d_dir: "{{ (config_dir, 'go.d') | path_join }}"
 | |
|     data_dir: "{{ (base_dir, 'data') | path_join }}"
 | |
| 
 | |
|   tasks:
 | |
|     - name: "Create user and environment"
 | |
|       ansible.builtin.import_role:
 | |
|         name: owner
 | |
|       vars:
 | |
|         owner_name: "{{ app_user }}"
 | |
|         owner_extra_groups: ["docker"]
 | |
| 
 | |
|     - name: "Create internal application directories"
 | |
|       ansible.builtin.file:
 | |
|         path: "{{ item }}"
 | |
|         state: "directory"
 | |
|         owner: "{{ app_user }}"
 | |
|         group: "{{ app_user }}"
 | |
|         mode: "0770"
 | |
|       loop:
 | |
|         - "{{ config_dir }}"
 | |
|         - "{{ config_go_d_dir }}"
 | |
|         - "{{ data_dir }}"
 | |
| 
 | |
|     - name: "Copy netdata config file"
 | |
|       ansible.builtin.template:
 | |
|         src: "files/{{ app_name }}/netdata.conf.j2"
 | |
|         dest: "{{ config_dir }}/netdata.conf"
 | |
|         owner: "{{ app_user }}"
 | |
|         group: "{{ app_user }}"
 | |
|         mode: "0640"
 | |
| 
 | |
|     - name: "Find all go.d plugin config files"
 | |
|       ansible.builtin.find:
 | |
|         paths: "files/{{ app_name }}/go.d"
 | |
|         file_type: file
 | |
|       delegate_to: localhost
 | |
|       register: go_d_source_files
 | |
| 
 | |
|     - name: "Template all go.d plugin config files"
 | |
|       ansible.builtin.template:
 | |
|         src: "{{ item.path }}"
 | |
|         dest: "{{ config_go_d_dir }}/{{ item.path | basename }}"
 | |
|         owner: "{{ app_user }}"
 | |
|         group: "{{ app_user }}"
 | |
|         mode: "0640"
 | |
|       loop: "{{ go_d_source_files.files }}"
 | |
| 
 | |
|     - name: "Find existing go.d config files on server"
 | |
|       ansible.builtin.find:
 | |
|         paths: "{{ config_go_d_dir }}"
 | |
|         file_type: file
 | |
|       register: go_d_existing_files
 | |
| 
 | |
|     - name: "Remove go.d config files that don't exist in source"
 | |
|       ansible.builtin.file:
 | |
|         path: "{{ item.path }}"
 | |
|         state: absent
 | |
|       loop: "{{ go_d_existing_files.files }}"
 | |
|       when: (item.path | basename) not in (go_d_source_files.files | map(attribute='path') | map('basename') | list)
 | |
| 
 | |
|     - name: "Grab docker group id."
 | |
|       ansible.builtin.shell:
 | |
|         cmd: |
 | |
|           set -o pipefail
 | |
|           grep docker /etc/group | cut -d ':' -f 3
 | |
|         executable: /bin/bash
 | |
|       register: netdata_docker_group_output
 | |
|       changed_when: netdata_docker_group_output.rc != 0
 | |
| 
 | |
|     - name: "Copy docker compose file"
 | |
|       ansible.builtin.template:
 | |
|         src: "./files/{{ app_name }}/docker-compose.yml.j2"
 | |
|         dest: "{{ base_dir }}/docker-compose.yml"
 | |
|         owner: "{{ app_user }}"
 | |
|         group: "{{ app_user }}"
 | |
|         mode: "0640"
 | |
| 
 | |
|     - name: "Run application with docker compose"
 | |
|       community.docker.docker_compose_v2:
 | |
|         project_src: "{{ base_dir }}"
 | |
|         state: "present"
 | |
|         remove_orphans: true
 | |
| 
 | |
|     - name: "Restart application with docker compose"
 | |
|       community.docker.docker_compose_v2:
 | |
|         project_src: "{{ base_dir }}"
 | |
|         state: "restarted"
 |