From 708a9f54c68be2aebf350bdad77f25f148d5f381 Mon Sep 17 00:00:00 2001 From: Joshua Hoffmann Date: Wed, 13 Nov 2024 15:01:46 +0100 Subject: [PATCH] add missing condition to ensure a failed unit only has its failed state reset if it actually is in a failed state --- tasks/manage_node_state.yml | 5 +++ tasks/setup_cluster.yml | 2 +- tests/ansible.cfg | 2 + tests/create.yml | 58 ++++++++++++++++++++++++ tests/destroy.yml | 17 ++++++++ tests/inventory/openstack.yaml | 12 +++++ tests/playbook.yml | 63 ++++++++++++++++++++++++++ tests/prepare.yml | 80 ++++++++++++++++++++++++++++++++++ tests/test_requirements.yml | 11 +++++ tests/testing.yml | 10 +++++ tests/vars/main.yml | 13 ++++++ tests/vars/mysqld_exporter.yml | 5 +++ 12 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 tests/ansible.cfg create mode 100644 tests/create.yml create mode 100644 tests/destroy.yml create mode 100644 tests/inventory/openstack.yaml create mode 100644 tests/playbook.yml create mode 100644 tests/prepare.yml create mode 100644 tests/test_requirements.yml create mode 100644 tests/testing.yml create mode 100644 tests/vars/main.yml create mode 100644 tests/vars/mysqld_exporter.yml diff --git a/tasks/manage_node_state.yml b/tasks/manage_node_state.yml index c8e8369..f32ba6f 100644 --- a/tasks/manage_node_state.yml +++ b/tasks/manage_node_state.yml @@ -6,13 +6,18 @@ become: true when: inventory_hostname == item +- name: manage_node_state | Populate service facts + ansible.builtin.service_facts: + - name: manage_node_state | reset failed status (otherwise service can't be stopped) command: "systemctl reset-failed {{ mariadb_systemd_service_name }}" # noqa command-instead-of-module become: true check_mode: false changed_when: false + ignore_errors: true when: - '"stopped" in mariadb_systemd_service_state' + - '"failed" in ansible_facts.services[mariadb_systemd_service_name].state' - inventory_hostname == item - name: manage_node_state | ensure node is fully stopped before continuing diff --git a/tasks/setup_cluster.yml b/tasks/setup_cluster.yml index 3e4aa01..482e1a0 100644 --- a/tasks/setup_cluster.yml +++ b/tasks/setup_cluster.yml @@ -48,7 +48,7 @@ galera_extra_wsrep_provider_options: > {{ galera_extra_wsrep_provider_options | combine({ - 'ist.recv_addr': galera_ist_recv_addr + ":" + galera_ist_recv_addr_port, + 'ist.recv_addr': galera_ist_recv_addr + ":" + galera_ist_recv_addr_port|string, 'ist.recv_bind': galera_ist_recv_bind }) }} diff --git a/tests/ansible.cfg b/tests/ansible.cfg new file mode 100644 index 0000000..154ac15 --- /dev/null +++ b/tests/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +inventory = ./inventory \ No newline at end of file diff --git a/tests/create.yml b/tests/create.yml new file mode 100644 index 0000000..eb08460 --- /dev/null +++ b/tests/create.yml @@ -0,0 +1,58 @@ +--- + +- name: Creating Galera Test Scenario + hosts: localhost + vars: + openstack_cloud: testing + openstack_ssh_key: testing + openstack_flavor_name: gp1a.4 + openstack_image_name_deb12: debian-12-generic-amd64 + openstack_image_name_deb11: debian-11-generic-amd64 + openstack_network_name: network-k0stesting_generic + tasks: + - name: Include variables + ansible.builtin.include_vars: "vars/main.yml" + + - name: Fetch Image (Deb12) + openstack.cloud.image_info: + cloud: "{{ openstack_cloud }}" + image: "{{ openstack_image_name_deb12 }}" + register: _openstack_image_deb12 + + - name: Fetch Flavor + openstack.cloud.compute_flavor_info: + cloud: "{{ openstack_cloud }}" + name: "{{ openstack_flavor_name }}" + register: _openstack_flavor + + - name: Fetch Network + openstack.cloud.networks_info: + cloud: "{{ openstack_cloud }}" + name: "{{ openstack_network_name }}" + register: _openstack_network + + - name: Ensure SSH Key exists + openstack.cloud.keypair: + name: "{{ openstack_ssh_key }}" + cloud: "{{ openstack_cloud }}" + public_key_file: "{{ ansible_env.HOME }}/.ssh/testing.pub" + + - name: Create Galera Instances + openstack.cloud.server: + name: "{{ item }}" + state: present + image: "{{ _openstack_image_deb12.images[0].id }}" + key_name: "{{ openstack_ssh_key }}" + flavor: "{{ _openstack_flavor.flavors[0].id }}" + network: "{{ _openstack_network.networks[0].id }}" + terminate_volume: true + security_groups: + - 1aa4fd40-1650-4151-84a7-3f5ecc4c4b23 + cloud: "{{ openstack_cloud }}" + metadata: + group: "{{ the_role_name }}" + auto_ip: false + loop: + - galera-1 + - galera-2 + - galera-3 diff --git a/tests/destroy.yml b/tests/destroy.yml new file mode 100644 index 0000000..db0c6c0 --- /dev/null +++ b/tests/destroy.yml @@ -0,0 +1,17 @@ +--- + +- name: Destroying Test Scenario + hosts: localhost + vars: + openstack_cloud: testing + tasks: + + - name: Destroy Instances + openstack.cloud.server: + state: absent + name: "{{ item }}" + cloud: "{{ openstack_cloud }}" + loop: + - galera-1 + - galera-2 + - galera-3 diff --git a/tests/inventory/openstack.yaml b/tests/inventory/openstack.yaml new file mode 100644 index 0000000..23383bb --- /dev/null +++ b/tests/inventory/openstack.yaml @@ -0,0 +1,12 @@ +--- +plugin: openstack.cloud.openstack +expand_hostvars: true +private: true +legacy_groups: false +keyed_groups: + - key: openstack.metadata.group + default_value: galera + parent_group: all +leading_separator: false +only_clouds: + - testing diff --git a/tests/playbook.yml b/tests/playbook.yml new file mode 100644 index 0000000..692ab8b --- /dev/null +++ b/tests/playbook.yml @@ -0,0 +1,63 @@ +--- + +- name: Creating Test Environment for Galera + hosts: mariadb_galera_cluster + order: sorted + pre_tasks: + - name: Set hostname + ansible.builtin.hostname: + name: "{{ inventory_hostname }}" + become: true + - name: Reload facts + ansible.builtin.setup: + - name: Refresh Inventory + ansible.builtin.meta: refresh_inventory + - name: Set Hostnames + ansible.builtin.lineinfile: + path: /etc/hosts + line: "{{ hostvars[item].ansible_facts.default_ipv4.address }} {{ hostvars[item].ansible_facts.fqdn }}" + become: true + loop: "{{ ansible_play_hosts }}" + - name: Set Hostnames IPv6 + ansible.builtin.lineinfile: + path: /etc/hosts + line: "{{ hostvars[item].ansible_facts.default_ipv6.address }} {{ hostvars[item].ansible_facts.fqdn }}" + become: true + loop: "{{ ansible_play_hosts }}" + - name: Include variables + ansible.builtin.include_vars: "vars/main.yml" + - name: Load mysqld_exporter vars + ansible.builtin.include_vars: + file: mysqld_exporter.yml + roles: + - role: adit.mariadb_galera_cluster + tags: + - mariadb_galera_cluster + +- name: MySQL Clustercheck + hosts: mariadb_galera_cluster + order: sorted + pre_tasks: + - name: Include variables + ansible.builtin.include_vars: "vars/main.yml" + - name: Load mysqld_exporter vars + ansible.builtin.include_vars: + file: mysqld_exporter.yml + roles: + - role: adit.mysql_clustercheck + tags: + - mysql_clustercheck + +- name: MySQL Clustercheck + hosts: mariadb_galera_cluster + order: sorted + pre_tasks: + - name: Include variables + ansible.builtin.include_vars: "vars/main.yml" + - name: Load mysqld_exporter vars + ansible.builtin.include_vars: + file: mysqld_exporter.yml + roles: + - role: prometheus.prometheus.mysqld_exporter + tags: + - prometheus_mysqld_exporter diff --git a/tests/prepare.yml b/tests/prepare.yml new file mode 100644 index 0000000..7649207 --- /dev/null +++ b/tests/prepare.yml @@ -0,0 +1,80 @@ +--- + +- name: Linking role + hosts: localhost + vars: + requirements_yml_file: '../requirements.yml' + test_requirements_yml_file: './test_requirements.yml' + tasks: + - name: Include variables + ansible.builtin.include_vars: "vars/main.yml" + + - name: Removing existing link + ansible.builtin.file: + path: '~/.ansible/roles/adit.{{ the_role_name }}' + state: absent + - name: Linking role + ansible.builtin.file: + state: link + path: '~/.ansible/roles/adit.{{ the_role_name }}' + src: "{{ playbook_dir }}/.." + - name: Check if requirements_yml_file exists + ansible.builtin.stat: + path: "{{ requirements_yml_file }}" + register: requirements_yml + + - name: Include requirements_yml_file as variables + ansible.builtin.include_vars: "{{ requirements_yml_file }}" + when: + - requirements_yml.stat.exists + + - name: Remove roles + ansible.builtin.file: + path: "~/.ansible/roles/{{ item.name }}" + state: absent + with_items: + - "{{ roles }}" + when: + - roles is defined + + - name: Install roles + ansible.builtin.command: + cmd: "ansible-galaxy install -r {{ requirements_yml_file }}" + register: ansible_galaxy_output + with_items: + - "{{ roles }}" + when: + - roles is defined + changed_when: ansible_galaxy_output.rc != 0 + +######################### Role-Requirements for test-Playbooks + - name: Check if test_requirements_yml_file exists + ansible.builtin.stat: + path: "{{ test_requirements_yml_file }}" + register: test_requirements_yml + + - name: "Install required roles for test-environment" + when: + - test_requirements_yml.stat.exists + block: + - name: Include test_requirements_yml_file as variables + ansible.builtin.include_vars: "{{ test_requirements_yml_file }}" + + - name: Remove roles + ansible.builtin.file: + path: "~/.ansible/roles/{{ item.name }}" + state: absent + with_items: + - "{{ roles }}" + when: + - roles is defined + + - name: Install roles + ansible.builtin.command: + cmd: "ansible-galaxy install -r {{ test_requirements_yml_file }}" + register: ansible_galaxy_output + with_items: + - "{{ roles }}" + when: + - roles is defined + changed_when: ansible_galaxy_output.rc != 0 diff --git a/tests/test_requirements.yml b/tests/test_requirements.yml new file mode 100644 index 0000000..8c55fb8 --- /dev/null +++ b/tests/test_requirements.yml @@ -0,0 +1,11 @@ +--- +roles: + - name: adit.mysql_clustercheck + src: https://group_549_bot_5c252634c40b20e971f823cea50a2254:glpat-S_q-wS-awXTNPyKt-nqq@git.aditsystems.de/adit/infrastructure/ansible/mysql_clustercheck.git + scm: git + version: 0.1.1 +collections: + - name: community.docker + version: "*" # Need to ensure that the latest version is installed or Molecule fails + - name: prometheus.prometheus + version: 0.17.0 diff --git a/tests/testing.yml b/tests/testing.yml new file mode 100644 index 0000000..2cfc0e0 --- /dev/null +++ b/tests/testing.yml @@ -0,0 +1,10 @@ +--- + +- name: Test stuff + hosts: mariadb_galera_cluster + tags: + - testing + tasks: + - name: Print ssh command + ansible.builtin.debug: + msg: 'ssh {{ ansible_default_ipv4.address }}' diff --git a/tests/vars/main.yml b/tests/vars/main.yml new file mode 100644 index 0000000..1d02cd8 --- /dev/null +++ b/tests/vars/main.yml @@ -0,0 +1,13 @@ +# tests/vars/main.yml +# defintion of needed variables +--- + +db_path: "{{ kv_path }}/database" +galera_cluster_bind_interface: '{{ ansible_default_ipv4.interface }}' +galera_cluster_name: "testcluster" +galera_cluster_nodes_group: "{{ the_role_name }}" +galera_mysql_first_node: "{{ groups[the_role_name][0] }}" +kv_path: "/customer/{{ customer }}/mariadb" +mariadb_debian_repo_key_url: "https://mariadb.org/mariadb_release_signing_key.asc" +the_role_name: 'mariadb_galera_cluster' +user_path: "{{ kv_path }}/user" diff --git a/tests/vars/mysqld_exporter.yml b/tests/vars/mysqld_exporter.yml new file mode 100644 index 0000000..d12d945 --- /dev/null +++ b/tests/vars/mysqld_exporter.yml @@ -0,0 +1,5 @@ +--- + +# MySQLd Exporter Vars +mysqld_exporter_username: "test" +mysqld_exporter_password: "test"