mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 14:40:19 -07:00
Fix a parsing bug that prevents IPv6 addresses from being used with add_host
Closes #8682
This commit is contained in:
parent
47d9e7ca93
commit
1886307845
2 changed files with 73 additions and 2 deletions
|
@ -20,6 +20,8 @@
|
|||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import re
|
||||
|
||||
from ansible.plugins.action import ActionBase
|
||||
|
||||
class ActionModule(ActionBase):
|
||||
|
@ -38,8 +40,8 @@ class ActionModule(ActionBase):
|
|||
new_name = self._task.args.get('name', self._task.args.get('hostname', None))
|
||||
#vv("creating host via 'add_host': hostname=%s" % new_name)
|
||||
|
||||
if ":" in new_name:
|
||||
new_name, new_port = new_name.split(":")
|
||||
new_name, new_port = _parse_ip_host_and_port(new_name)
|
||||
if new_port:
|
||||
self._task.args['ansible_ssh_port'] = new_port
|
||||
|
||||
groups = self._task.args.get('groupname', self._task.args.get('groups', self._task.args.get('group', '')))
|
||||
|
@ -58,4 +60,26 @@ class ActionModule(ActionBase):
|
|||
|
||||
return dict(changed=True, add_host=dict(host_name=new_name, groups=new_groups, host_vars=host_vars))
|
||||
|
||||
def _parse_ip_host_and_port(hostname):
|
||||
"""
|
||||
Attempt to parse the hostname and port from a hostname, e.g.,
|
||||
|
||||
some-host-name
|
||||
some-host-name:80
|
||||
8.8.8.8
|
||||
8.8.8.8:80
|
||||
2001:db8:0:1
|
||||
[2001:db8:0:1]:80
|
||||
"""
|
||||
if hostname.count(':') > 1:
|
||||
match = re.match(
|
||||
'\[(?P<ip>[^\]]+)\](:(?P<port>[0-9]+))?',
|
||||
hostname
|
||||
)
|
||||
if match:
|
||||
return match.group('ip'), match.group('port')
|
||||
else:
|
||||
return hostname, None
|
||||
elif ':' in hostname:
|
||||
return hostname.split(':')
|
||||
return hostname, None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue