From b4632d2a241c79e17666029befdf8cb4ee40d301 Mon Sep 17 00:00:00 2001 From: caoyuan Date: Mon, 23 Jan 2017 23:34:10 +0800 Subject: [PATCH] Optimize reconfigure action for telegraf Change-Id: Ia658b0774ce009feb6b4e73363114f7922833835 Partially-implements: blueprint better-reconfigure --- ansible/roles/telegraf/defaults/main.yml | 16 ++++++++ ansible/roles/telegraf/handlers/main.yml | 25 ++++++++++++ ansible/roles/telegraf/tasks/config.yml | 50 ++++++++++++++++++++++-- ansible/roles/telegraf/tasks/deploy.yml | 3 +- ansible/roles/telegraf/tasks/pull.yml | 6 ++- ansible/roles/telegraf/tasks/start.yml | 18 --------- ansible/roles/telegraf/tasks/upgrade.yml | 3 +- 7 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 ansible/roles/telegraf/handlers/main.yml delete mode 100644 ansible/roles/telegraf/tasks/start.yml diff --git a/ansible/roles/telegraf/defaults/main.yml b/ansible/roles/telegraf/defaults/main.yml index 48ff1c557d..dc4b76afbb 100644 --- a/ansible/roles/telegraf/defaults/main.yml +++ b/ansible/roles/telegraf/defaults/main.yml @@ -1,6 +1,22 @@ --- project_name: "telegraf" +telegraf_services: + telegraf: + container_name: "telegraf" + image: "{{ telegraf_image_full }}" + enabled: true + pid_mode: "host" + group: "telegraf" + volumes: + - "{{ node_config_directory }}/telegraf/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + - "/sys:/rootfs/sys:ro" + - "/proc:/rootfs/proc:ro" + - "/var/run/docker.sock:/var/run/docker.sock:ro" + + #################### # Docker #################### diff --git a/ansible/roles/telegraf/handlers/main.yml b/ansible/roles/telegraf/handlers/main.yml new file mode 100644 index 0000000000..175abafe25 --- /dev/null +++ b/ansible/roles/telegraf/handlers/main.yml @@ -0,0 +1,25 @@ +--- +- name: Restart telegraf container + vars: + service_name: "telegraf" + service: "{{ telegraf_services[service_name] }}" + config_json: "{{ telegraf_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + telegraf_container: "{{ check_telegraf_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 }}" + pid_mode: "{{ service.pid_mode }}" + environment: + HOST_PROC: "/rootfs/proc" + HOST_SYS: "/rootfs/sys" + image: "{{ service.image }}" + volumes: "{{ service.volumes|reject('equalto', '')|list }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or telegraf_confs.changed | bool + or telegraf_plugin.changed | bool + or telegraf_container.changed | bool diff --git a/ansible/roles/telegraf/tasks/config.yml b/ansible/roles/telegraf/tasks/config.yml index 19f8697601..3a04581073 100644 --- a/ansible/roles/telegraf/tasks/config.yml +++ b/ansible/roles/telegraf/tasks/config.yml @@ -1,26 +1,68 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}/config" state: "directory" recurse: yes - with_items: - - "telegraf" - - "telegraf/config" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ telegraf_services }}" - name: Copying over default config.json files + vars: + service: "{{ telegraf_services['telegraf_services'] }}" template: src: "telegraf.json.j2" dest: "{{ node_config_directory }}/telegraf/config.json" + register: telegraf_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ telegraf_services }}" + notify: + - Restart telegraf container - name: Copying over telegraf config file + vars: + service: "{{ telegraf_services['telegraf'] }}" template: src: "telegraf.conf.j2" dest: "{{ node_config_directory }}/telegraf/telegraf.conf" + register: telegraf_confs + when: + - inventory_hostname in groups[service.group] + - service.enabled | bool + notify: + - Restart telegraf container - name: Copying over telegraf plugin files + vars: + service: "{{ telegraf_services['telegraf'] }}" copy: src: "{{ item }}" dest: "{{ node_config_directory }}/telegraf/config" + register: telegraf_plugin + when: + - inventory_hostname in groups[service.group] + - item.value.enabled | bool with_fileglob: - "{{ role_path }}/templates/config/*.conf" + notify: + - Restart telegraf container + +- name: Check telegraf 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_telegraf_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ telegraf_services }}" + notify: + - Restart telegraf container diff --git a/ansible/roles/telegraf/tasks/deploy.yml b/ansible/roles/telegraf/tasks/deploy.yml index 1f16915ad9..dd26ecc34d 100644 --- a/ansible/roles/telegraf/tasks/deploy.yml +++ b/ansible/roles/telegraf/tasks/deploy.yml @@ -1,4 +1,5 @@ --- - include: config.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/telegraf/tasks/pull.yml b/ansible/roles/telegraf/tasks/pull.yml index e1a5b45703..f4202b90e3 100644 --- a/ansible/roles/telegraf/tasks/pull.yml +++ b/ansible/roles/telegraf/tasks/pull.yml @@ -3,4 +3,8 @@ kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ telegraf_image_full }}" + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ telegraf_services }}" diff --git a/ansible/roles/telegraf/tasks/start.yml b/ansible/roles/telegraf/tasks/start.yml deleted file mode 100644 index b50cec4530..0000000000 --- a/ansible/roles/telegraf/tasks/start.yml +++ /dev/null @@ -1,18 +0,0 @@ ---- -- name: Starting telegraf container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ telegraf_image_full }}" - name: "telegraf" - environment: - HOST_PROC: "/rootfs/proc" - HOST_SYS: "/rootfs/sys" - volumes: - - "{{ node_config_directory }}/telegraf/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - - "/sys:/rootfs/sys:ro" - - "/proc:/rootfs/proc:ro" - - "/var/run/docker.sock:/var/run/docker.sock:ro" - pid_mode: "host" diff --git a/ansible/roles/telegraf/tasks/upgrade.yml b/ansible/roles/telegraf/tasks/upgrade.yml index 1f16915ad9..dd26ecc34d 100644 --- a/ansible/roles/telegraf/tasks/upgrade.yml +++ b/ansible/roles/telegraf/tasks/upgrade.yml @@ -1,4 +1,5 @@ --- - include: config.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers