diff --git a/ansible/roles/mongodb/defaults/main.yml b/ansible/roles/mongodb/defaults/main.yml index 9bf44a5831..59645ecaef 100644 --- a/ansible/roles/mongodb/defaults/main.yml +++ b/ansible/roles/mongodb/defaults/main.yml @@ -1,6 +1,19 @@ --- project_name: "mongodb" +mongodb_services: + mongodb: + container_name: mongodb + group: mongodb + enabled: true + privileged: True + image: "{{ mongodb_image_full }}" + volumes: + - "{{ node_config_directory }}/mongodb/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + - "mongodb:/var/lib/mongodb" + #################### # Docker diff --git a/ansible/roles/mongodb/handlers/main.yml b/ansible/roles/mongodb/handlers/main.yml new file mode 100644 index 0000000000..d992ee8a82 --- /dev/null +++ b/ansible/roles/mongodb/handlers/main.yml @@ -0,0 +1,38 @@ +--- +- name: Restart mongodb container + vars: + service_name: "mongodb" + service: "{{ mongodb_services[service_name] }}" + config_json: "{{ mongodb_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mongodb_conf: "{{ mongodb_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + mongodb_container: "{{ check_mongodb_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 }}" + privileged: "{{ service.privileged | default(False) }}" + volumes: "{{ service.volumes }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or mongodb_conf.changed | bool + or mongodb_container.changed | bool + notify: + - Waiting for the mongodb startup + - Checking current replication status + +- name: Waiting for the mongodb startup + wait_for: host={{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }} port={{ mongodb_port }} + +- name: Checking current replication status + command: "docker exec -t mongodb mongo {{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }} --quiet --eval rs.status().ok" + register: mongodb_replication_status + changed_when: false + delegate_to: "{{ groups['mongodb'][0] }}" + run_once: True + +- include: "bootstrap_cluster.yml" + when: mongodb_replication_status.stdout != "1" diff --git a/ansible/roles/mongodb/tasks/config.yml b/ansible/roles/mongodb/tasks/config.yml index 9501e38986..606a9d9b54 100644 --- a/ansible/roles/mongodb/tasks/config.yml +++ b/ansible/roles/mongodb/tasks/config.yml @@ -1,22 +1,51 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "mongodb" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mongodb_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "mongodb" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: mongodb_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mongodb_services }}" + notify: + - Restart mongodb container - name: Copying over mongodb.conf template: - src: "{{ item }}.j2" - dest: "{{ node_config_directory }}/mongodb/{{ item }}" - with_items: - - "mongodb.conf" + src: "{{ item.key }}.conf.j2" + dest: "{{ node_config_directory }}/mongodb/{{ item.key }}.conf" + register: mongodb_confs + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mongodb_services }}" + notify: + - Restart mongodb container + +- name: Check mongodb containers + kolla_docker: + action: "compare_container" + common_options: "{{ docker_common_options }}" + name: "{{ item.value.container_name }}" + image: "{{ item.value.image }}" + privileged: "{{ item.value.privileged|default(False) }}" + volumes: "{{ item.value.volumes }}" + register: check_mongodb_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mongodb_services }}" + notify: + - Restart mongodb container diff --git a/ansible/roles/mongodb/tasks/deploy.yml b/ansible/roles/mongodb/tasks/deploy.yml index 98daa4021c..9eca42dec5 100644 --- a/ansible/roles/mongodb/tasks/deploy.yml +++ b/ansible/roles/mongodb/tasks/deploy.yml @@ -3,4 +3,5 @@ - include: bootstrap.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/mongodb/tasks/pull.yml b/ansible/roles/mongodb/tasks/pull.yml index 642e132c03..d0a7d05684 100644 --- a/ansible/roles/mongodb/tasks/pull.yml +++ b/ansible/roles/mongodb/tasks/pull.yml @@ -3,5 +3,8 @@ kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ mongodb_image_full }}" - when: inventory_hostname in groups['mongodb'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mongodb_services }}" diff --git a/ansible/roles/mongodb/tasks/reconfigure.yml b/ansible/roles/mongodb/tasks/reconfigure.yml index 9bec471870..e078ef1318 100644 --- a/ansible/roles/mongodb/tasks/reconfigure.yml +++ b/ansible/roles/mongodb/tasks/reconfigure.yml @@ -1,64 +1,2 @@ --- -- name: Ensuring the containers up - kolla_docker: - name: "{{ item.name }}" - action: "get_container_state" - register: container_state - failed_when: container_state.Running == false - when: inventory_hostname in groups[item.group] - with_items: - - { name: mongodb, group: mongodb } - -- include: config.yml - -- name: Check the configs - command: docker exec {{ item.name }} /usr/local/bin/kolla_set_configs --check - changed_when: false - failed_when: false - register: check_results - when: inventory_hostname in groups[item.group] - with_items: - - { name: mongodb, group: mongodb } - -# 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: "{{ item.name }}" - action: "get_container_env" - register: container_envs - when: inventory_hostname in groups[item.group] - with_items: - - { name: mongodb, group: mongodb } - -- name: Remove the containers - kolla_docker: - name: "{{ item[0]['name'] }}" - action: "remove_container" - register: remove_containers - when: - - config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE' - - item[2]['rc'] == 1 - - inventory_hostname in groups[item[0]['group']] - with_together: - - [{ name: mongodb, group: mongodb }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" - -- include: start.yml - when: remove_containers.changed - -- name: Restart containers - kolla_docker: - name: "{{ item[0]['name'] }}" - action: "restart_container" - when: - - config_strategy == 'COPY_ALWAYS' - - item[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE' - - item[2]['rc'] == 1 - - inventory_hostname in groups[item[0]['group']] - with_together: - - [{ name: mongodb, group: mongodb }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/mongodb/tasks/start.yml b/ansible/roles/mongodb/tasks/start.yml deleted file mode 100644 index 89ad7570f9..0000000000 --- a/ansible/roles/mongodb/tasks/start.yml +++ /dev/null @@ -1,26 +0,0 @@ ---- -- name: Starting mongodb container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ mongodb_image_full }}" - name: "mongodb" - privileged: True - volumes: - - "{{ node_config_directory }}/mongodb/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - - "mongodb:/var/lib/mongodb" - -- name: Waiting for the mongodb startup - wait_for: host={{ api_interface_address }} port={{ mongodb_port }} - -- name: Checking current replication status - command: "docker exec -t mongodb mongo {{ api_interface_address }} --quiet --eval rs.status().ok" - register: mongodb_replication_status - changed_when: false - delegate_to: "{{ groups['mongodb'][0] }}" - run_once: True - -- include: "bootstrap_cluster.yml" - when: mongodb_replication_status.stdout != "1" diff --git a/ansible/roles/mongodb/tasks/upgrade.yml b/ansible/roles/mongodb/tasks/upgrade.yml index 1f16915ad9..dd26ecc34d 100644 --- a/ansible/roles/mongodb/tasks/upgrade.yml +++ b/ansible/roles/mongodb/tasks/upgrade.yml @@ -1,4 +1,5 @@ --- - include: config.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers