Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kubespray
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
Kubespray
Commits
44de04be
Commit
44de04be
authored
6 years ago
by
tikitavi
Committed by
Matthew Mosesohn
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
update inventory builder for public and private IP per node (#4323)
parent
33024731
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
contrib/inventory_builder/inventory.py
+15
-6
15 additions, 6 deletions
contrib/inventory_builder/inventory.py
contrib/inventory_builder/tests/test_inventory.py
+32
-0
32 additions, 0 deletions
contrib/inventory_builder/tests/test_inventory.py
with
47 additions
and
6 deletions
contrib/inventory_builder/inventory.py
+
15
−
6
View file @
44de04be
...
@@ -17,6 +17,8 @@
...
@@ -17,6 +17,8 @@
#
#
# Advanced usage:
# Advanced usage:
# Add another host after initial creation: inventory.py 10.10.1.5
# Add another host after initial creation: inventory.py 10.10.1.5
# Add range of hosts: inventory.py 10.10.1.3-10.10.1.5
# Add hosts with different ip and access ip: inventory.py 10.0.0.1,192.168.10.1 10.0.0.2,192.168.10.2 10.0.0.3,192.168.10.3
# Delete a host: inventory.py -10.10.1.3
# Delete a host: inventory.py -10.10.1.3
# Delete a host by id: inventory.py -node1
# Delete a host by id: inventory.py -node1
#
#
...
@@ -169,18 +171,23 @@ class KubesprayInventory(object):
...
@@ -169,18 +171,23 @@ class KubesprayInventory(object):
self
.
debug
(
"
Marked {0} for deletion.
"
.
format
(
realhost
))
self
.
debug
(
"
Marked {0} for deletion.
"
.
format
(
realhost
))
self
.
delete_host_by_ip
(
all_hosts
,
realhost
)
self
.
delete_host_by_ip
(
all_hosts
,
realhost
)
elif
host
[
0
].
isdigit
():
elif
host
[
0
].
isdigit
():
if
'
,
'
in
host
:
ip
,
access_ip
=
host
.
split
(
'
,
'
)
else
:
ip
=
host
access_ip
=
host
if
self
.
exists_hostname
(
all_hosts
,
host
):
if
self
.
exists_hostname
(
all_hosts
,
host
):
self
.
debug
(
"
Skipping existing host {0}.
"
.
format
(
host
))
self
.
debug
(
"
Skipping existing host {0}.
"
.
format
(
host
))
continue
continue
elif
self
.
exists_ip
(
all_hosts
,
host
):
elif
self
.
exists_ip
(
all_hosts
,
ip
):
self
.
debug
(
"
Skipping existing host {0}.
"
.
format
(
host
))
self
.
debug
(
"
Skipping existing host {0}.
"
.
format
(
ip
))
continue
continue
next_host
=
"
{0}{1}
"
.
format
(
HOST_PREFIX
,
next_host_id
)
next_host
=
"
{0}{1}
"
.
format
(
HOST_PREFIX
,
next_host_id
)
next_host_id
+=
1
next_host_id
+=
1
all_hosts
[
next_host
]
=
{
'
ansible_host
'
:
host
,
all_hosts
[
next_host
]
=
{
'
ansible_host
'
:
access_ip
,
'
ip
'
:
host
,
'
ip
'
:
ip
,
'
access_ip
'
:
host
}
'
access_ip
'
:
access_ip
}
elif
host
[
0
].
isalpha
():
elif
host
[
0
].
isalpha
():
raise
Exception
(
"
Adding hosts by hostname is not supported.
"
)
raise
Exception
(
"
Adding hosts by hostname is not supported.
"
)
...
@@ -235,7 +242,7 @@ class KubesprayInventory(object):
...
@@ -235,7 +242,7 @@ class KubesprayInventory(object):
all_hosts
=
self
.
yaml_config
[
'
all
'
][
'
hosts
'
].
copy
()
all_hosts
=
self
.
yaml_config
[
'
all
'
][
'
hosts
'
].
copy
()
for
host
in
all_hosts
.
keys
():
for
host
in
all_hosts
.
keys
():
if
host
not
in
hostnames
and
host
not
in
protected_names
:
if
host
not
in
hostnames
and
host
not
in
protected_names
:
self
.
debug
(
"
Host {0} removed from role all
"
)
self
.
debug
(
"
Host {0} removed from role all
"
.
format
(
host
)
)
del
self
.
yaml_config
[
'
all
'
][
'
hosts
'
][
host
]
del
self
.
yaml_config
[
'
all
'
][
'
hosts
'
][
host
]
def
add_host_to_group
(
self
,
group
,
host
,
opts
=
""
):
def
add_host_to_group
(
self
,
group
,
host
,
opts
=
""
):
...
@@ -346,6 +353,8 @@ print_ips - Write a space-delimited list of IPs from "all" group
...
@@ -346,6 +353,8 @@ print_ips - Write a space-delimited list of IPs from "all" group
Advanced usage:
Advanced usage:
Add another host after initial creation: inventory.py 10.10.1.5
Add another host after initial creation: inventory.py 10.10.1.5
Add range of hosts: inventory.py 10.10.1.3-10.10.1.5
Add hosts with different ip and access ip: inventory.py 10.0.0.1,192.168.10.1 10.0.0.2,192.168.10.2 10.0.0.3,192.168.10.3
Delete a host: inventory.py -10.10.1.3
Delete a host: inventory.py -10.10.1.3
Delete a host by id: inventory.py -node1
Delete a host by id: inventory.py -node1
...
...
This diff is collapsed.
Click to expand it.
contrib/inventory_builder/tests/test_inventory.py
+
32
−
0
View file @
44de04be
...
@@ -311,3 +311,35 @@ class TestInventory(unittest.TestCase):
...
@@ -311,3 +311,35 @@ class TestInventory(unittest.TestCase):
host_range
=
[
'
10.90.0.4-a.9b.c.e
'
]
host_range
=
[
'
10.90.0.4-a.9b.c.e
'
]
self
.
assertRaisesRegexp
(
Exception
,
"
Range of ip_addresses isn
'
t valid
"
,
self
.
assertRaisesRegexp
(
Exception
,
"
Range of ip_addresses isn
'
t valid
"
,
self
.
inv
.
range2ips
,
host_range
)
self
.
inv
.
range2ips
,
host_range
)
def
test_build_hostnames_different_ips_add_one
(
self
):
changed_hosts
=
[
'
10.90.0.2,192.168.0.2
'
]
expected
=
OrderedDict
([(
'
node1
'
,
{
'
ansible_host
'
:
'
192.168.0.2
'
,
'
ip
'
:
'
10.90.0.2
'
,
'
access_ip
'
:
'
192.168.0.2
'
})])
result
=
self
.
inv
.
build_hostnames
(
changed_hosts
)
self
.
assertEqual
(
expected
,
result
)
def
test_build_hostnames_different_ips_add_duplicate
(
self
):
changed_hosts
=
[
'
10.90.0.2,192.168.0.2
'
]
expected
=
OrderedDict
([(
'
node1
'
,
{
'
ansible_host
'
:
'
192.168.0.2
'
,
'
ip
'
:
'
10.90.0.2
'
,
'
access_ip
'
:
'
192.168.0.2
'
})])
self
.
inv
.
yaml_config
[
'
all
'
][
'
hosts
'
]
=
expected
result
=
self
.
inv
.
build_hostnames
(
changed_hosts
)
self
.
assertEqual
(
expected
,
result
)
def
test_build_hostnames_different_ips_add_two
(
self
):
changed_hosts
=
[
'
10.90.0.2,192.168.0.2
'
,
'
10.90.0.3,192.168.0.3
'
]
expected
=
OrderedDict
([
(
'
node1
'
,
{
'
ansible_host
'
:
'
192.168.0.2
'
,
'
ip
'
:
'
10.90.0.2
'
,
'
access_ip
'
:
'
192.168.0.2
'
}),
(
'
node2
'
,
{
'
ansible_host
'
:
'
192.168.0.3
'
,
'
ip
'
:
'
10.90.0.3
'
,
'
access_ip
'
:
'
192.168.0.3
'
})])
self
.
inv
.
yaml_config
[
'
all
'
][
'
hosts
'
]
=
OrderedDict
()
result
=
self
.
inv
.
build_hostnames
(
changed_hosts
)
self
.
assertEqual
(
expected
,
result
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment