Ansible Version : 2.3.1.0
Python Version : 2.7.5
Command : ansible-playbook -i inventories/DEV/ playbooks/patching.yml -e "ticketnum=12345 perform_action=stop ansible_ssh_user=abc" -k
Setup:
project
|_____ pythonengine
|______ engine.py
|_____ playbooks
|_____ patching.yml
|_____ inventories
|_____ DEV
|____hosts
|____group_vars
|____all
|___ all.yml
|____host_vars
|____host1
|___ all.yml
|_____ QA
|____hosts
|____group_vars
|____all
|___ all.yml
|____host_vars
|____host2
|___ all.yml
The playbook executes perfectly fine, As part of automation while calling the playbook from Python API [ engine.py ], Its unable to get the variables defined under group_vars and host_vars.
here is the code :
def runPlaybook(self, playbookYML, hostfile, remoteUser, remoteUserpwd, ticketnum, patchAction, envt):
try:
loader = Dataloader()
inventory = Inventory(loader=loader, variable_manager=VariableManager(), host_list=hostfile)
variable_manager = VariableManager()
variable_manager.extra_vars = { 'ticketnum': ticketnum, 'perform_action': patchAction, 'ansible_ssh_user': remoteUser }
options = namedtuple('Options', ['listtags','listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'fork', 'remote_user', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'verbosity', 'check', 'diff', 'become', 'become_method','become_user'])
options = Options(listtags=False,listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, fork=100, remote_user=remoteUser, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, verbosity=None, check=False, diff=False, become=False, become_method=sudo,become_user=remoteUser)
passwords = {'conn_pass': remoteUserPwd}
pbex = PlaybookExecutor(playbook=[playbookYML], inventory=inventory, variable_manager=variable_manager, loader=loader, option=options, passwords=passwords)
results = pbex.run
except Exception as e:
logger.error(traceback.print_exc())
raise
The above code is failing to get the variables defined under group_vars and host_vars, defined under the inventories/[DEV or QA ]
Please help me in getting these multi stage environment variables to get loaded.