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
3fb2d385
Commit
3fb2d385
authored
May 06, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reinstate external inventory script support this time using the new more OO-ey inventory system.
Next up: YAML format.
parent
d9bec842
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
6 deletions
+28
-6
lib/ansible/inventory.py
+26
-0
test/TestInventory.py
+2
-6
No files found.
lib/ansible/inventory.py
View file @
3fb2d385
...
...
@@ -18,9 +18,12 @@
#############################################
import
fnmatch
import
os
import
constants
as
C
import
subprocess
from
ansible.inventory_parser
import
InventoryParser
from
ansible.inventory_script
import
InventoryScript
from
ansible.group
import
Group
from
ansible.host
import
Host
from
ansible
import
errors
...
...
@@ -36,11 +39,18 @@ class Inventory(object):
# FIXME: re-support YAML inventory format
# FIXME: re-support external inventory script (?)
self
.
host_list
=
host_list
self
.
groups
=
[]
self
.
_restriction
=
None
self
.
_is_script
=
False
if
host_list
:
if
type
(
host_list
)
==
list
:
self
.
groups
=
self
.
_groups_from_override_hosts
(
host_list
)
elif
os
.
access
(
host_list
,
os
.
X_OK
):
self
.
_is_script
=
True
self
.
parser
=
InventoryScript
(
filename
=
host_list
)
self
.
groups
=
self
.
parser
.
groups
.
values
()
else
:
self
.
parser
=
InventoryParser
(
filename
=
host_list
)
self
.
groups
=
self
.
parser
.
groups
.
values
()
...
...
@@ -93,6 +103,22 @@ class Inventory(object):
return
group
.
get_variables
()
def
get_variables
(
self
,
hostname
):
if
self
.
_is_script
:
# TODO: move this to inventory_script.py
host
=
self
.
get_host
(
hostname
)
cmd
=
subprocess
.
Popen
(
[
self
.
host_list
,
"--host"
,
hostname
],
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
(
out
,
err
)
=
cmd
.
communicate
()
results
=
utils
.
parse_json
(
out
)
results
[
'inventory_hostname'
]
=
hostname
groups
=
[
g
.
name
for
g
in
host
.
get_groups
()
if
g
.
name
!=
'all'
]
results
[
'group_names'
]
=
groups
return
results
host
=
self
.
get_host
(
hostname
)
if
host
is
None
:
raise
Exception
(
"host not found:
%
s"
%
hostname
)
...
...
test/TestInventory.py
View file @
3fb2d385
...
...
@@ -108,7 +108,6 @@ class TestInventory(unittest.TestCase):
### Inventory API tests
def
test_script
(
self
):
raise
SkipTest
inventory
=
self
.
script_inventory
()
hosts
=
inventory
.
list_hosts
()
...
...
@@ -119,7 +118,6 @@ class TestInventory(unittest.TestCase):
assert
sorted
(
hosts
)
==
sorted
(
expected_hosts
)
def
test_script_all
(
self
):
raise
SkipTest
inventory
=
self
.
script_inventory
()
hosts
=
inventory
.
list_hosts
(
'all'
)
...
...
@@ -127,7 +125,6 @@ class TestInventory(unittest.TestCase):
assert
sorted
(
hosts
)
==
sorted
(
expected_hosts
)
def
test_script_norse
(
self
):
raise
SkipTest
inventory
=
self
.
script_inventory
()
hosts
=
inventory
.
list_hosts
(
"norse"
)
...
...
@@ -135,7 +132,6 @@ class TestInventory(unittest.TestCase):
assert
sorted
(
hosts
)
==
sorted
(
expected_hosts
)
def
test_script_combined
(
self
):
raise
SkipTest
inventory
=
self
.
script_inventory
()
hosts
=
inventory
.
list_hosts
(
"norse:greek"
)
...
...
@@ -143,7 +139,6 @@ class TestInventory(unittest.TestCase):
assert
sorted
(
hosts
)
==
sorted
(
expected_hosts
)
def
test_script_restrict
(
self
):
raise
SkipTest
inventory
=
self
.
script_inventory
()
restricted_hosts
=
[
'hera'
,
'poseidon'
,
'thor'
]
...
...
@@ -160,10 +155,11 @@ class TestInventory(unittest.TestCase):
assert
sorted
(
hosts
)
==
sorted
(
expected_hosts
)
def
test_script_vars
(
self
):
raise
SkipTest
inventory
=
self
.
script_inventory
()
vars
=
inventory
.
get_variables
(
'thor'
)
print
"VARS=
%
s"
%
vars
assert
vars
==
{
'hammer'
:
True
,
'group_names'
:
[
'norse'
],
'inventory_hostname'
:
'thor'
}
...
...
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