# How to include group\_vars when using python api to run ansible-playbook.

**URL:** <https://forum.ansible.com/t/how-to-include-group-vars-when-using-python-api-to-run-ansible-playbook/29830>\
**Category:** Ansible Project\
**Tags:** azure, kubernetes\
**Created:** [March 6, 2019, 4:53am UTC](https://forum.ansible.com/t/how-to-include-group-vars-when-using-python-api-to-run-ansible-playbook/29830 "2019-03-06T04:53:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jace\_Liang](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Jace\_Liang](https://forum.ansible.com/u/Jace_Liang)\
**Post date:** [March 6, 2019, 4:53am UTC](https://forum.ansible.com/t/how-to-include-group-vars-when-using-python-api-to-run-ansible-playbook/29830/1 "2019-03-06T04:53:44Z")

</div>

Hi all,

I am trying to write a python script to run [kuberspray](https://github.com/kubernetes-sigs/kubespray)’s ansible-playbook.  
Basically, when using kubespray, all I need to do is get into kubespray’s folder and type command.

`  
ansible-playbook -i inventory/mycluster/hosts.ini cluster.yml

`

Tell ansible-playbook where the inventory and playbook file is.  
And ansible will include all the group\_vars in the inventory/mycluster/group\_vars automatically.

‘kubespray/inventory/mycluster/group\_vars’

Here is my python script trying to call ansible-playbook to do the same thing.

Based on some internet examples and the document from ansible webpage.

But when I run this script, it shows some error about the undefined variables.  
How to import all the variables inside the group\_vars?

`  
import os,sys,json  
import ansible.constants as C  
from ansible.parsing.dataloader import DataLoader  
from ansible.vars.manager import VariableManager  
from ansible.inventory.manager import InventoryManager  
from ansible.playbook import Play  
from ansible.executor.task\_queue\_manager import TaskQueueManager  
from ansible.executor.playbook\_executor import PlaybookExecutor  
from ansible.plugins.callback import CallbackBase  
from ansible.inventory.host import Host,Group  
from collections import namedtuple

inventoryPath = “/home/jace/x-k8s/kubespray/inventory/mycluster/hosts.ini”  
playbookPath = “/home/jace/x-k8s/kubespray/cluster.yml”

loader = DataLoader()  
myinven = InventoryManager(loader=loader,sources=[inventoryPath])  
print(myinven.get\_groups\_dict())

varmanager = VariableManager(loader=loader,inventory=myinven)

Options = namedtuple(‘Options’,[  
‘connection’,‘module\_path’, ‘forks’, ‘timeout’, ‘remote\_user’,  
‘ask\_pass’, ‘private\_key\_file’, ‘ssh\_common\_args’, ‘ssh\_extra\_args’, ‘sftp\_extra\_args’,  
‘scp\_extra\_args’, ‘become’, ‘become\_method’, ‘become\_user’, ‘ask\_value\_pass’, ‘verbosity’,  
‘check’, ‘listhosts’, ‘listtasks’, ‘listtags’, ‘syntax’,‘diff’  
])

options = Options(connection=‘smart’, module\_path=None, forks=100, timeout=10,  
remote\_user=‘root’, ask\_pass=False, private\_key\_file=None, ssh\_common\_args=None, ssh\_extra\_args=None,  
sftp\_extra\_args=None, scp\_extra\_args=None, become=None, become\_method=None,  
become\_user=‘root’, ask\_value\_pass=False, verbosity=None, check=False, listhosts=False,  
listtasks=False, listtags=False, syntax=False, diff=True)

passwords = {}  
playbook = PlaybookExecutor(playbooks=[playbookPath],  
inventory=myinven,  
variable\_manager=varmanager,  
loader=loader,  
options=options,  
passwords=passwords)  
playbook.run()  
`

Tree of group\_vars

`jace@ansible:~/x-k8s/kubespray/inventory/mycluster/group_vars$ tree . . ├── all │ ├── all.yml │ ├── azure.yml │ ├── coreos.yml │ ├── docker.yml │ ├── oci.yml │ └── openstack.yml ├── etcd.yml └── k8s-cluster ├── addons.yml ├── k8s-cluster.yml ├── k8s-net-calico.yml ├── k8s-net-canal.yml ├── k8s-net-cilium.yml ├── k8s-net-contiv.yml ├── k8s-net-flannel.yml ├── k8s-net-kube-router.yml └── k8s-net-weave.yml`

Ansible version : 2.7.8  
Python version : 3.5.2
