Skip to content

Commit

Permalink
dynamic/custom inventory path
Browse files Browse the repository at this point in the history
This will make sure to use the inventory file defined in config. Also
handles dynamic inventory files.
  • Loading branch information
stelcheck committed Nov 8, 2018
1 parent 7dff821 commit 6388ef9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions buildconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ binary:
name: "dawn"

# The current version of the binary
version: "0.12.5"
version: "0.12.6"

# (Optional) URLs to call when attempting auto-update.
# Defaults:
Expand Down Expand Up @@ -78,7 +78,7 @@ image:
name: dawn

# Current image version
version: "0.12.5"
version: "0.12.6"

# Root folder where most files will be uploaded or mounted
root_folder: /dawn
Expand Down
32 changes: 25 additions & 7 deletions docker-image/scripts/pylib/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os

from ansible.config.manager import ConfigManager
from ansible.errors import (AnsibleFileNotFound, AnsibleParserError,
AnsibleUndefinedVariable)
from ansible.inventory.manager import InventoryManager
Expand Down Expand Up @@ -53,24 +54,38 @@ class AnsibleEnvironment():
_cache = {}

def __init__(self):
initial_dir = os.getcwd()
ansible_basedir = os.path.join(
os.environ.get("PROJECT_ENVIRONMENT_FILES_PATH"), "ansible")
inv_file = '/etc/ansible/hosts'

# Move to project directory
os.chdir(os.environ.get("PROJECT_ENVIRONMENT_FILES_PATH"))

# Load list of inventories from config:w
config = ConfigManager('/etc/ansible/ansible.cfg')
sources = config.data.get_setting('DEFAULT_HOST_LIST').value

loader = CustomLoader()
loader.set_basedir(ansible_basedir)

# load the inventory, set the basic playbook directory
self._inventory = InventoryManager(loader=loader, sources=[inv_file])
self._inventory = InventoryManager(loader=loader, sources=sources)
var_manager = VariableManager(loader=loader, inventory=self._inventory)
hostvars = HostVars(inventory=self._inventory, variable_manager=var_manager, loader=loader)
play = Play.load(dict(hosts=['all']), loader=loader, variable_manager=var_manager)

group = self._inventory.groups['all']
# Move back to directory of origin
os.chdir(initial_dir)

control_group = self._inventory.groups['control']
control_host = None
if len(group.get_hosts()) > 0:
control_host = group.get_hosts()[0]

if len(control_group.get_hosts()) > 0:
control_host = control_group.get_hosts()[0]

# Hostvars
hostvars = {}
for host in self._inventory.get_hosts():
hostvars[host.name] = host.vars

# make sure we load all magic variables on top of the global variables
self._vars = combine_vars(
Expand All @@ -79,7 +94,10 @@ def __init__(self):
task=Task(),
host=control_host
),
{'env': os.environ}
{
'hostvars': hostvars,
'env': os.environ
}
)

# create the template renderer
Expand Down

0 comments on commit 6388ef9

Please sign in to comment.