How to include group_vars when using python api to run ansible-playbook.

33 views
Skip to first unread message

Jace Liang

unread,
Mar 5, 2019, 11:53:44 PM3/5/19
to Ansible Project
Hi all,

I am trying to write a python script to run kuberspray'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
Reply all
Reply to author
Forward
0 new messages