From 1a4af9bfc34e417d65e0eb81990d0f023a03c606 Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Sat, 5 Jun 2021 05:53:02 -0700 Subject: [PATCH] Reduce stormssh searches based on host (#2568) * Reduce stormssh searches based on host Due to the stormssh searches in the whole config values, we need to reduce the search results based on the full matching of the hosts * Removed whitespaces in the blank line * Added changelog fragment and tests for the fix. * Added newline at the end of the changelog fragment * Added newline at the end of the tests * Fixed bug with name in tests * Changed assertion for the existing host * Update changelogs/fragments/2568-ssh_config-reduce-stormssh-searches-based-on-host.yml Co-authored-by: Felix Fontein * Adjusted tests * New line at the end of the tests Co-authored-by: Anton Nikolaev Co-authored-by: Felix Fontein --- ...reduce-stormssh-searches-based-on-host.yml | 2 ++ plugins/modules/system/ssh_config.py | 2 ++ .../targets/ssh_config/tasks/main.yml | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 changelogs/fragments/2568-ssh_config-reduce-stormssh-searches-based-on-host.yml diff --git a/changelogs/fragments/2568-ssh_config-reduce-stormssh-searches-based-on-host.yml b/changelogs/fragments/2568-ssh_config-reduce-stormssh-searches-based-on-host.yml new file mode 100644 index 0000000000..2f3e400e7e --- /dev/null +++ b/changelogs/fragments/2568-ssh_config-reduce-stormssh-searches-based-on-host.yml @@ -0,0 +1,2 @@ +bugfixes: + - ssh_config - reduce stormssh searches based on host (https://github.com/ansible-collections/community.general/pull/2568/). diff --git a/plugins/modules/system/ssh_config.py b/plugins/modules/system/ssh_config.py index 943f6b44fc..be177baaaf 100644 --- a/plugins/modules/system/ssh_config.py +++ b/plugins/modules/system/ssh_config.py @@ -209,6 +209,8 @@ class SSHConfig(): hosts_removed = [] hosts_added = [] + hosts_result = [host for host in hosts_result if host['host'] == self.host] + if hosts_result: for host in hosts_result: if state == 'absent': diff --git a/tests/integration/targets/ssh_config/tasks/main.yml b/tests/integration/targets/ssh_config/tasks/main.yml index 12f277b455..bd5acc9e04 100644 --- a/tests/integration/targets/ssh_config/tasks/main.yml +++ b/tests/integration/targets/ssh_config/tasks/main.yml @@ -183,3 +183,39 @@ that: - not mut_ex.changed - "'parameters are mutually exclusive' in mut_ex.msg" + +- name: Add a full name host + community.general.ssh_config: + ssh_config_file: "{{ ssh_config_test }}" + host: "full_name" + hostname: full_name.com + identity_file: '{{ ssh_private_key }}' + port: '2223' + state: present + register: full_name + +- name: Check if changes are made + assert: + that: + - full_name is changed + - full_name.hosts_added == ["full_name"] + - full_name.hosts_changed == [] + - full_name.hosts_removed == [] + +- name: Add a host with name which is contained in full name host + community.general.ssh_config: + ssh_config_file: "{{ ssh_config_test }}" + host: "full" + hostname: full.com + identity_file: '{{ ssh_private_key }}' + port: '2223' + state: present + register: short_name + +- name: Check that short name host is added and full name host is not updated + assert: + that: + - short_name is changed + - short_name.hosts_added == ["full"] + - short_name.hosts_changed == [] + - short_name.hosts_removed == []