Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
18863078
Commit
18863078
authored
Aug 19, 2014
by
Ryan Petrello
Committed by
Abhijit Menon-Sen
Aug 23, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix a parsing bug that prevents IPv6 addresses from being used with `add_host`
Closes #8682
parent
47d9e7ca
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
2 deletions
+73
-2
lib/ansible/plugins/action/add_host.py
+26
-2
test/units/plugins/action/test_add_host.py
+47
-0
No files found.
lib/ansible/plugins/action/add_host.py
View file @
18863078
...
...
@@ -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
test/units/plugins/action/test_add_host.py
0 → 100644
View file @
18863078
import
unittest
from
ansible.plugins.action
import
add_host
class
TestAddHost
(
unittest
.
TestCase
):
def
test_hostname
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'some-remote-host'
)
assert
host
==
'some-remote-host'
assert
port
is
None
def
test_hostname_with_port
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'some-remote-host:80'
)
assert
host
==
'some-remote-host'
assert
port
==
'80'
def
test_parse_ip_host_and_port_v4
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'8.8.8.8'
)
assert
host
==
'8.8.8.8'
assert
port
is
None
def
test_parse_ip_host_and_port_v4_and_port
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'8.8.8.8:80'
)
assert
host
==
'8.8.8.8'
assert
port
==
'80'
def
test_parse_ip_host_and_port_v6
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'dead:beef:dead:beef:dead:beef:dead:beef'
)
assert
host
==
'dead:beef:dead:beef:dead:beef:dead:beef'
assert
port
is
None
def
test_parse_ip_host_and_port_v6_with_brackets
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'[dead:beef:dead:beef:dead:beef:dead:beef]'
)
assert
host
==
'dead:beef:dead:beef:dead:beef:dead:beef'
assert
port
is
None
def
test_parse_ip_host_and_port_v6_with_brackets_and_port
(
self
):
host
,
port
=
add_host
.
_parse_ip_host_and_port
(
'[dead:beef:dead:beef:dead:beef:dead:beef]:80'
)
assert
host
==
'dead:beef:dead:beef:dead:beef:dead:beef'
assert
port
==
'80'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment