diff --git a/ansible/roles/grafana/defaults/main.yml b/ansible/roles/grafana/defaults/main.yml index e101161c52..7a5a5ab5e0 100644 --- a/ansible/roles/grafana/defaults/main.yml +++ b/ansible/roles/grafana/defaults/main.yml @@ -1,6 +1,18 @@ --- project_name: "grafana" +grafana_services: + grafana: + container_name: grafana + group: grafana + enabled: true + image: "{{ grafana_image_full }}" + volumes: + - "{{ node_config_directory }}/grafana/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "grafana:/var/lib/grafana/" + - "kolla_logs:/var/log/kolla/" + #################### # Database #################### diff --git a/ansible/roles/grafana/handlers/main.yml b/ansible/roles/grafana/handlers/main.yml new file mode 100644 index 0000000000..69156bf6e8 --- /dev/null +++ b/ansible/roles/grafana/handlers/main.yml @@ -0,0 +1,21 @@ +--- +- name: Restart grafana container + vars: + service_name: "grafana" + service: "{{ grafana_services[service_name] }}" + config_json: "{{ grafana_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + grafana_conf: "{{ grafana_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + grafana_container: "{{ check_grafana_containers.results|selectattr('item.key', 'equalto', service_name)|first }}" + kolla_docker: + action: "recreate_or_restart_container" + common_options: "{{ docker_common_options }}" + name: "{{ service.container_name }}" + image: "{{ service.image }}" + volumes: "{{ service.volumes }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or grafana_ini.changed | bool + or grafana_container.changed | bool diff --git a/ansible/roles/grafana/tasks/config.yml b/ansible/roles/grafana/tasks/config.yml index ca3ddabc23..6984ad3e9d 100644 --- a/ansible/roles/grafana/tasks/config.yml +++ b/ansible/roles/grafana/tasks/config.yml @@ -1,27 +1,55 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "grafana" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ grafana_services }}" - name: Copying over config.json files template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "grafana" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: grafana_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ grafana_services }}" + notify: + - Restart grafana container - name: Copying over grafana.ini merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/grafana.ini.j2" - - "{{ node_custom_config }}/{{ item }}.ini" - - "{{ node_custom_config }}/grafana/{{ inventory_hostname }}/{{ item }}.ini" + - "{{ node_custom_config }}/{{ item.key }}.ini" + - "{{ node_custom_config }}/grafana/{{ inventory_hostname }}/{{ item.key }}.ini" dest: "{{ node_config_directory }}/grafana/grafana.ini" - with_items: - - "grafana" + register: grafana_confs + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ grafana_services }}" + notify: + - Restart grafana container + +- name: Check grafana containers + kolla_docker: + action: "compare_container" + common_options: "{{ docker_common_options }}" + name: "{{ item.value.container_name }}" + image: "{{ item.value.image }}" + volumes: "{{ item.value.volumes }}" + register: check_grafana_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ grafana_services }}" + notify: + - Restart grafana container diff --git a/ansible/roles/grafana/tasks/deploy.yml b/ansible/roles/grafana/tasks/deploy.yml index 98b45ee90c..44340130de 100644 --- a/ansible/roles/grafana/tasks/deploy.yml +++ b/ansible/roles/grafana/tasks/deploy.yml @@ -5,5 +5,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['grafana'] -- include: start.yml - when: inventory_hostname in groups['grafana'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/grafana/tasks/reconfigure.yml b/ansible/roles/grafana/tasks/reconfigure.yml index b8ca4ed270..e078ef1318 100644 --- a/ansible/roles/grafana/tasks/reconfigure.yml +++ b/ansible/roles/grafana/tasks/reconfigure.yml @@ -1,47 +1,2 @@ --- -- name: Ensuring the containers up - kolla_docker: - name: "grafana" - action: "get_container_state" - register: container_state - failed_when: container_state.Running == false - when: inventory_hostname in groups['grafana'] - -- include: config.yml - -- name: Check the configs - command: docker exec grafana /usr/local/bin/kolla_set_configs --check - changed_when: false - failed_when: false - register: check_results - when: inventory_hostname in groups['grafana'] - -# NOTE(jeffrey4l): when config_strategy == 'COPY_ALWAYS' -# and container env['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE', -# just remove the container and start again -- name: Containers config strategy - kolla_docker: - name: "grafana" - action: "get_container_env" - register: container_envs - when: inventory_hostname in groups['grafana'] - -- name: Remove the containers - kolla_docker: - name: "grafana" - action: "remove_container" - register: remove_containers - when: - - config_strategy == "COPY_ONCE" - - inventory_hostname in groups['grafana'] - -- include: start.yml - when: remove_containers.changed - -- name: Restart containers - kolla_docker: - name: "grafana" - action: "restart_container" - when: - - config_strategy == 'COPY_ALWAYS' - - inventory_hostname in groups['grafana'] +- include: deploy.yml diff --git a/ansible/roles/grafana/tasks/start.yml b/ansible/roles/grafana/tasks/start.yml deleted file mode 100644 index 0d3114dab7..0000000000 --- a/ansible/roles/grafana/tasks/start.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -- name: Starting grafana container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ grafana_image_full }}" - name: "grafana" - volumes: - - "{{ node_config_directory }}/grafana/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "grafana:/var/lib/grafana/" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['grafana'] diff --git a/ansible/roles/grafana/tasks/upgrade.yml b/ansible/roles/grafana/tasks/upgrade.yml index 1f16915ad9..dd26ecc34d 100644 --- a/ansible/roles/grafana/tasks/upgrade.yml +++ b/ansible/roles/grafana/tasks/upgrade.yml @@ -1,4 +1,5 @@ --- - include: config.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers