diff --git a/changelogs/fragments/50337-fix-vultr_server-ssh_keys-handling.yml b/changelogs/fragments/50337-fix-vultr_server-ssh_keys-handling.yml new file mode 100644 index 0000000000..6cd0d4cfd3 --- /dev/null +++ b/changelogs/fragments/50337-fix-vultr_server-ssh_keys-handling.yml @@ -0,0 +1,2 @@ +bugfixes: + - vultr_server - fixed multiple ssh keys were not handled. diff --git a/lib/ansible/modules/cloud/vultr/vultr_server.py b/lib/ansible/modules/cloud/vultr/vultr_server.py index 9bdbc4baf6..19413074c5 100644 --- a/lib/ansible/modules/cloud/vultr/vultr_server.py +++ b/lib/ansible/modules/cloud/vultr/vultr_server.py @@ -99,6 +99,9 @@ EXAMPLES = ''' name: "{{ vultr_server_name }}" os: CentOS 7 x64 plan: 1024 MB RAM,25 GB SSD,1.00 TB BW + ssh_keys: + - my_key + - your_key region: Amsterdam state: present @@ -108,6 +111,7 @@ EXAMPLES = ''' name: "{{ vultr_server_name }}" os: CentOS 7 x64 plan: 1024 MB RAM,25 GB SSD,1.00 TB BW + ssh_key: my_key region: Amsterdam state: started @@ -382,13 +386,22 @@ class AnsibleVultrServer(Vultr): use_cache=True ) - def get_ssh_key(self): - return self.query_resource_by_key( - key='name', - value=self.module.params.get('ssh_key'), - resource='sshkey', - use_cache=True - ) + def get_ssh_keys(self): + ssh_key_names = self.module.params.get('ssh_keys') + if not ssh_key_names: + return [] + + ssh_keys = [] + for ssh_key_name in ssh_key_names: + ssh_key = self.query_resource_by_key( + key='name', + value=ssh_key_name, + resource='sshkey', + use_cache=True + ) + if ssh_key: + ssh_keys.append(ssh_key) + return ssh_keys def get_region(self): return self.query_resource_by_key( @@ -488,7 +501,7 @@ class AnsibleVultrServer(Vultr): 'OSID': self.get_os().get('OSID'), 'label': self.module.params.get('name'), 'hostname': self.module.params.get('hostname'), - 'SSHKEYID': self.get_ssh_key().get('SSHKEYID'), + 'SSHKEYID': ','.join([ssh_key['SSHKEYID'] for ssh_key in self.get_ssh_keys()]), 'enable_ipv6': self.get_yes_or_no('ipv6_enabled'), 'enable_private_network': self.get_yes_or_no('private_network_enabled'), 'auto_backups': self.get_yes_or_no('auto_backup_enabled'), diff --git a/test/legacy/roles/vultr_server/defaults/main.yml b/test/legacy/roles/vultr_server/defaults/main.yml index 04a6494d1b..dfe5b73bd3 100644 --- a/test/legacy/roles/vultr_server/defaults/main.yml +++ b/test/legacy/roles/vultr_server/defaults/main.yml @@ -2,3 +2,8 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) --- vultr_server_name: ansible-test-vm1 +vultr_server_ssh_keys: +- name: key1 + key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAgEAyWYItY+3w5b8PdGRoz0oY5mufqydW96naE+VM3JSvJFAUS08rAjQQpQ03ymoALeHQy6JVZbcgecxn6p0pAOINQdqufn4udPtOPCtMjNiPGpkSM9ah/6X5+kvyWMNrvlf+Ld4OOoszP5sAkgQzIbrFQAm41XknBUha0zkewZwfrVhain4pnDjV7wCcChId/Q/Gbi4xMtXkisznWcAJcueBs3EEZDKhJ5q0VeWSJEhYJDLFN1sOxF0AIUnMrOhfKQ/LjgREXPB6uCl899INUTXRNNjRpeMXyJ2wMMmOAbua2qEd1r13Bu1n+6A823Hzb33fyMXuqWnJwBJ4DCvMlGuEsfuOK+xk7DaBfLHbcM6fsPk0/4psTE6YLgC41remr6+u5ZWsY/faMtSnNPie8Z8Ov0DIYGdhbJjUXk1HomxRV9+ZfZ2Ob8iCwlaAQAyEUM6fs3Kxt8pBD8dx1HOkhsfBWPvuDr5y+kqE7H8/MuPDTc0QgH2pjUMpmw/XBwNDHshVEjrZvtICOjOLUJxcowLO1ivNYwPwowQxfisMy56LfYdjsOslBiqsrkAqvNGm1zu8wKHeqVN9w5l3yUELpvubfm9NKIvYcl6yWF36T0c5vE+g0DU/Jy4XpTj0hZG9QV2mRQcLJnd2pxQtJT7cPFtrn/+tgRxzjEtbDXummDV4sE= mail@renemoser.net" +- name: key2 + key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoQ9S7V+CufAgwoehnf2TqsJ9LTsu8pUA3FgpS2mdVwcMcTs++8P5sQcXHLtDmNLpWN4k7NQgxaY1oXy5e25x/4VhXaJXWEt3luSw+Phv/PB2+aGLvqCUirsLTAD2r7ieMhd/pcVf/HlhNUQgnO1mupdbDyqZoGD/uCcJiYav8i/V7nJWJouHA8yq31XS2yqXp9m3VC7UZZHzUsVJA9Us5YqF0hKYeaGruIHR2bwoDF9ZFMss5t6/pzxMljU/ccYwvvRDdI7WX4o4+zLuZ6RWvsU6LGbbb0pQdB72tlV41fSefwFsk4JRdKbyV3Xjf25pV4IXOTcqhy+4JTB/jXxrF torwalds@github.com" diff --git a/test/legacy/roles/vultr_server/tasks/main.yml b/test/legacy/roles/vultr_server/tasks/main.yml index 3638b614ac..d3269c78bc 100644 --- a/test/legacy/roles/vultr_server/tasks/main.yml +++ b/test/legacy/roles/vultr_server/tasks/main.yml @@ -37,11 +37,20 @@ - result is failed - 'result.msg == "missing required arguments: os, plan, region"' +- name: setup create ssh keys + vultr_ssh_key: + name: "{{ item.name }}" + ssh_key: "{{ item.key }}" + loop: "{{ vultr_server_ssh_keys }}" + - name: test create server in check mode vultr_server: name: "{{ vultr_server_name }}" os: CentOS 6 x64 plan: 1024 MB RAM,25 GB SSD,1.00 TB BW + ssh_keys: + - key1 + - key2 region: Amsterdam state: started register: result @@ -56,6 +65,9 @@ name: "{{ vultr_server_name }}" os: CentOS 6 x64 plan: 1024 MB RAM,25 GB SSD,1.00 TB BW + ssh_keys: + - key1 + - key2 region: Amsterdam state: started register: result @@ -74,6 +86,9 @@ name: "{{ vultr_server_name }}" os: CentOS 6 x64 plan: 1024 MB RAM,25 GB SSD,1.00 TB BW + ssh_keys: + - key1 + - key2 region: Amsterdam state: started register: result @@ -484,3 +499,10 @@ assert: that: - result is not changed + +- name: cleanup ssh keys + vultr_ssh_key: + name: "{{ item.name }}" + ssh_key: "{{ item.key }}" + state: absent + loop: "{{ vultr_server_ssh_keys }}"