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
33e79203
Commit
33e79203
authored
Sep 16, 2015
by
James Cammarata
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'quinot-topic/crash_empty_inventory' into devel
parents
24ffa5f2
c8f2483d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
51 deletions
+45
-51
lib/ansible/inventory/__init__.py
+12
-11
lib/ansible/inventory/dir.py
+5
-5
lib/ansible/inventory/ini.py
+11
-14
lib/ansible/inventory/script.py
+17
-21
No files found.
lib/ansible/inventory/__init__.py
View file @
33e79203
...
@@ -86,11 +86,18 @@ class Inventory(object):
...
@@ -86,11 +86,18 @@ class Inventory(object):
self
.
parser
=
None
self
.
parser
=
None
# Always create the 'all' and 'ungrouped' groups, even if host_list is
# empty: in this case we will subsequently an the implicit 'localhost' to it.
ungrouped
=
Group
(
name
=
'ungrouped'
)
all
=
Group
(
'all'
)
all
.
add_child_group
(
ungrouped
)
self
.
groups
=
dict
(
all
=
all
,
ungrouped
=
ungrouped
)
if
host_list
is
None
:
if
host_list
is
None
:
pass
pass
elif
isinstance
(
host_list
,
list
):
elif
isinstance
(
host_list
,
list
):
all
=
Group
(
'all'
)
self
.
groups
=
[
all
]
for
h
in
host_list
:
for
h
in
host_list
:
(
host
,
port
)
=
parse_address
(
h
,
allow_ranges
=
False
)
(
host
,
port
)
=
parse_address
(
h
,
allow_ranges
=
False
)
all
.
add_host
(
Host
(
host
,
port
))
all
.
add_host
(
Host
(
host
,
port
))
...
@@ -99,9 +106,9 @@ class Inventory(object):
...
@@ -99,9 +106,9 @@ class Inventory(object):
if
self
.
_loader
.
is_directory
(
host_list
):
if
self
.
_loader
.
is_directory
(
host_list
):
# Ensure basedir is inside the directory
# Ensure basedir is inside the directory
host_list
=
os
.
path
.
join
(
self
.
host_list
,
""
)
host_list
=
os
.
path
.
join
(
self
.
host_list
,
""
)
self
.
parser
=
InventoryDirectory
(
loader
=
self
.
_loader
,
filename
=
host_list
)
self
.
parser
=
InventoryDirectory
(
loader
=
self
.
_loader
,
groups
=
self
.
groups
,
filename
=
host_list
)
else
:
else
:
self
.
parser
=
get_file_parser
(
host_list
,
self
.
_loader
)
self
.
parser
=
get_file_parser
(
host_list
,
self
.
groups
,
self
.
_loader
)
vars_loader
.
add_directory
(
self
.
basedir
(),
with_subdir
=
True
)
vars_loader
.
add_directory
(
self
.
basedir
(),
with_subdir
=
True
)
if
self
.
parser
:
if
self
.
parser
:
...
@@ -388,13 +395,7 @@ class Inventory(object):
...
@@ -388,13 +395,7 @@ class Inventory(object):
new_host
.
set_variable
(
"ansible_python_interpreter"
,
sys
.
executable
)
new_host
.
set_variable
(
"ansible_python_interpreter"
,
sys
.
executable
)
new_host
.
set_variable
(
"ansible_connection"
,
"local"
)
new_host
.
set_variable
(
"ansible_connection"
,
"local"
)
new_host
.
ipv4_address
=
'127.0.0.1'
new_host
.
ipv4_address
=
'127.0.0.1'
self
.
get_group
(
"ungrouped"
)
.
add_host
(
new_host
)
ungrouped
=
self
.
get_group
(
"ungrouped"
)
if
ungrouped
is
None
:
self
.
add_group
(
Group
(
'ungrouped'
))
ungrouped
=
self
.
get_group
(
'ungrouped'
)
self
.
get_group
(
'all'
)
.
add_child_group
(
ungrouped
)
ungrouped
.
add_host
(
new_host
)
return
new_host
return
new_host
def
clear_pattern_cache
(
self
):
def
clear_pattern_cache
(
self
):
...
...
lib/ansible/inventory/dir.py
View file @
33e79203
...
@@ -34,7 +34,7 @@ from ansible.inventory.script import InventoryScript
...
@@ -34,7 +34,7 @@ from ansible.inventory.script import InventoryScript
__all__
=
[
'get_file_parser'
]
__all__
=
[
'get_file_parser'
]
def
get_file_parser
(
hostsfile
,
loader
):
def
get_file_parser
(
hostsfile
,
groups
,
loader
):
# check to see if the specified file starts with a
# check to see if the specified file starts with a
# shebang (#!/), so if an error is raised by the parser
# shebang (#!/), so if an error is raised by the parser
# class we can show a more apropos error
# class we can show a more apropos error
...
@@ -55,7 +55,7 @@ def get_file_parser(hostsfile, loader):
...
@@ -55,7 +55,7 @@ def get_file_parser(hostsfile, loader):
if
loader
.
is_executable
(
hostsfile
):
if
loader
.
is_executable
(
hostsfile
):
try
:
try
:
parser
=
InventoryScript
(
loader
=
loader
,
filename
=
hostsfile
)
parser
=
InventoryScript
(
loader
=
loader
,
groups
=
groups
,
filename
=
hostsfile
)
processed
=
True
processed
=
True
except
Exception
as
e
:
except
Exception
as
e
:
myerr
.
append
(
"The file
%
s is marked as executable, but failed to execute correctly. "
%
hostsfile
+
\
myerr
.
append
(
"The file
%
s is marked as executable, but failed to execute correctly. "
%
hostsfile
+
\
...
@@ -64,7 +64,7 @@ def get_file_parser(hostsfile, loader):
...
@@ -64,7 +64,7 @@ def get_file_parser(hostsfile, loader):
if
not
processed
:
if
not
processed
:
try
:
try
:
parser
=
InventoryINIParser
(
loader
=
loader
,
filename
=
hostsfile
)
parser
=
InventoryINIParser
(
loader
=
loader
,
groups
=
groups
,
filename
=
hostsfile
)
processed
=
True
processed
=
True
except
Exception
as
e
:
except
Exception
as
e
:
if
shebang_present
and
not
loader
.
is_executable
(
hostsfile
):
if
shebang_present
and
not
loader
.
is_executable
(
hostsfile
):
...
@@ -81,13 +81,13 @@ def get_file_parser(hostsfile, loader):
...
@@ -81,13 +81,13 @@ def get_file_parser(hostsfile, loader):
class
InventoryDirectory
(
object
):
class
InventoryDirectory
(
object
):
''' Host inventory parser for ansible using a directory of inventories. '''
''' Host inventory parser for ansible using a directory of inventories. '''
def
__init__
(
self
,
loader
,
filename
=
C
.
DEFAULT_HOST_LIST
):
def
__init__
(
self
,
loader
,
groups
=
dict
(),
filename
=
C
.
DEFAULT_HOST_LIST
):
self
.
names
=
os
.
listdir
(
filename
)
self
.
names
=
os
.
listdir
(
filename
)
self
.
names
.
sort
()
self
.
names
.
sort
()
self
.
directory
=
filename
self
.
directory
=
filename
self
.
parsers
=
[]
self
.
parsers
=
[]
self
.
hosts
=
{}
self
.
hosts
=
{}
self
.
groups
=
{}
self
.
groups
=
groups
self
.
_loader
=
loader
self
.
_loader
=
loader
...
...
lib/ansible/inventory/ini.py
View file @
33e79203
...
@@ -38,7 +38,7 @@ class InventoryParser(object):
...
@@ -38,7 +38,7 @@ class InventoryParser(object):
with their associated hosts and variable settings.
with their associated hosts and variable settings.
"""
"""
def
__init__
(
self
,
loader
,
filename
=
C
.
DEFAULT_HOST_LIST
):
def
__init__
(
self
,
loader
,
groups
=
dict
(),
filename
=
C
.
DEFAULT_HOST_LIST
):
self
.
_loader
=
loader
self
.
_loader
=
loader
self
.
filename
=
filename
self
.
filename
=
filename
...
@@ -47,10 +47,7 @@ class InventoryParser(object):
...
@@ -47,10 +47,7 @@ class InventoryParser(object):
self
.
hosts
=
{}
self
.
hosts
=
{}
self
.
patterns
=
{}
self
.
patterns
=
{}
self
.
groups
=
dict
(
self
.
groups
=
groups
all
=
Group
(
name
=
'all'
),
ungrouped
=
Group
(
name
=
'ungrouped'
)
)
# Read in the hosts, groups, and variables defined in the
# Read in the hosts, groups, and variables defined in the
# inventory file.
# inventory file.
...
@@ -64,15 +61,6 @@ class InventoryParser(object):
...
@@ -64,15 +61,6 @@ class InventoryParser(object):
self
.
_parse
(
data
)
self
.
_parse
(
data
)
# Finally, add all top-level groups (including 'ungrouped') as
# children of 'all'.
for
group
in
self
.
groups
.
values
():
if
group
.
depth
==
0
and
group
.
name
!=
'all'
:
self
.
groups
[
'all'
]
.
add_child_group
(
group
)
# Note: we could discard self.hosts after this point.
def
_raise_error
(
self
,
message
):
def
_raise_error
(
self
,
message
):
raise
AnsibleError
(
"
%
s:
%
d: "
%
(
self
.
filename
,
self
.
lineno
)
+
message
)
raise
AnsibleError
(
"
%
s:
%
d: "
%
(
self
.
filename
,
self
.
lineno
)
+
message
)
...
@@ -186,6 +174,15 @@ class InventoryParser(object):
...
@@ -186,6 +174,15 @@ class InventoryParser(object):
elif
decl
[
'state'
]
==
'children'
:
elif
decl
[
'state'
]
==
'children'
:
raise
AnsibleError
(
"
%
s:
%
d: Section [
%
s:children] includes undefined group:
%
s"
%
(
self
.
filename
,
decl
[
'line'
],
decl
[
'parent'
],
decl
[
'name'
]))
raise
AnsibleError
(
"
%
s:
%
d: Section [
%
s:children] includes undefined group:
%
s"
%
(
self
.
filename
,
decl
[
'line'
],
decl
[
'parent'
],
decl
[
'name'
]))
# Finally, add all top-level groups as children of 'all'.
# We exclude ungrouped here because it was already added as a child of
# 'all' at the time it was created.
for
group
in
self
.
groups
.
values
():
if
group
.
depth
==
0
and
group
.
name
not
in
(
'all'
,
'ungrouped'
):
self
.
groups
[
'all'
]
.
add_child_group
(
group
)
def
_parse_group_name
(
self
,
line
):
def
_parse_group_name
(
self
,
line
):
'''
'''
Takes a single line and tries to parse it as a group name. Returns the
Takes a single line and tries to parse it as a group name. Returns the
...
...
lib/ansible/inventory/script.py
View file @
33e79203
...
@@ -36,9 +36,10 @@ from ansible.module_utils.basic import json_dict_bytes_to_unicode
...
@@ -36,9 +36,10 @@ from ansible.module_utils.basic import json_dict_bytes_to_unicode
class
InventoryScript
:
class
InventoryScript
:
''' Host inventory parser for ansible using external inventory scripts. '''
''' Host inventory parser for ansible using external inventory scripts. '''
def
__init__
(
self
,
loader
,
filename
=
C
.
DEFAULT_HOST_LIST
):
def
__init__
(
self
,
loader
,
groups
=
dict
(),
filename
=
C
.
DEFAULT_HOST_LIST
):
self
.
_loader
=
loader
self
.
_loader
=
loader
self
.
groups
=
groups
# Support inventory scripts that are not prefixed with some
# Support inventory scripts that are not prefixed with some
# path information but happen to be in the current working
# path information but happen to be in the current working
...
@@ -57,7 +58,7 @@ class InventoryScript:
...
@@ -57,7 +58,7 @@ class InventoryScript:
self
.
data
=
stdout
self
.
data
=
stdout
# see comment about _meta below
# see comment about _meta below
self
.
host_vars_from_top
=
None
self
.
host_vars_from_top
=
None
self
.
groups
=
self
.
_parse
(
stderr
)
self
.
_parse
(
stderr
)
def
_parse
(
self
,
err
):
def
_parse
(
self
,
err
):
...
@@ -77,11 +78,7 @@ class InventoryScript:
...
@@ -77,11 +78,7 @@ class InventoryScript:
self
.
raw
=
json_dict_bytes_to_unicode
(
self
.
raw
)
self
.
raw
=
json_dict_bytes_to_unicode
(
self
.
raw
)
all
=
Group
(
'all'
)
group
=
None
groups
=
dict
(
all
=
all
)
group
=
None
for
(
group_name
,
data
)
in
self
.
raw
.
items
():
for
(
group_name
,
data
)
in
self
.
raw
.
items
():
# in Ansible 1.3 and later, a "_meta" subelement may contain
# in Ansible 1.3 and later, a "_meta" subelement may contain
...
@@ -95,10 +92,10 @@ class InventoryScript:
...
@@ -95,10 +92,10 @@ class InventoryScript:
self
.
host_vars_from_top
=
data
[
'hostvars'
]
self
.
host_vars_from_top
=
data
[
'hostvars'
]
continue
continue
if
group_name
!=
all
.
name
:
if
group_name
not
in
self
.
groups
:
group
=
groups
[
group_name
]
=
Group
(
group_name
)
group
=
self
.
groups
[
group_name
]
=
Group
(
group_name
)
else
:
group
=
all
group
=
self
.
groups
[
group_name
]
host
=
None
host
=
None
if
not
isinstance
(
data
,
dict
):
if
not
isinstance
(
data
,
dict
):
...
@@ -124,10 +121,7 @@ class InventoryScript:
...
@@ -124,10 +121,7 @@ class InventoryScript:
"data for variables:
\n
%
s"
%
(
group_name
,
data
))
"data for variables:
\n
%
s"
%
(
group_name
,
data
))
for
k
,
v
in
iteritems
(
data
[
'vars'
]):
for
k
,
v
in
iteritems
(
data
[
'vars'
]):
if
group
.
name
==
all
.
name
:
group
.
set_variable
(
k
,
v
)
all
.
set_variable
(
k
,
v
)
else
:
group
.
set_variable
(
k
,
v
)
# Separate loop to ensure all groups are defined
# Separate loop to ensure all groups are defined
for
(
group_name
,
data
)
in
self
.
raw
.
items
():
for
(
group_name
,
data
)
in
self
.
raw
.
items
():
...
@@ -135,14 +129,16 @@ class InventoryScript:
...
@@ -135,14 +129,16 @@ class InventoryScript:
continue
continue
if
isinstance
(
data
,
dict
)
and
'children'
in
data
:
if
isinstance
(
data
,
dict
)
and
'children'
in
data
:
for
child_name
in
data
[
'children'
]:
for
child_name
in
data
[
'children'
]:
if
child_name
in
groups
:
if
child_name
in
self
.
groups
:
groups
[
group_name
]
.
add_child_group
(
groups
[
child_name
])
self
.
groups
[
group_name
]
.
add_child_group
(
self
.
groups
[
child_name
])
for
group
in
groups
.
values
():
# Finally, add all top-level groups as children of 'all'.
if
group
.
depth
==
0
and
group
.
name
!=
'all'
:
# We exclude ungrouped here because it was already added as a child of
all
.
add_child_group
(
group
)
# 'all' at the time it was created.
return
groups
for
group
in
self
.
groups
.
values
():
if
group
.
depth
==
0
and
group
.
name
not
in
(
'all'
,
'ungrouped'
):
self
.
groups
[
'all'
]
.
add_child_group
(
group
)
def
get_host_variables
(
self
,
host
):
def
get_host_variables
(
self
,
host
):
""" Runs <script> --host <hostname> to determine additional host variables """
""" Runs <script> --host <hostname> to determine additional host variables """
...
...
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