Serge,
Thanks for the reply! The chef code retrieves some information form the chef server that I want to make available as vars (either host vars or group vars, or whatever) to the ansible run.
Yeah, I see what you mean about some of the vars in my code . . . what you’re seeing is a hacked up attempt to get something working . . . here it is again, a bit cleaner (prints are included to show flow)
~/Code/mdansible⟫ cat inventory/vars_plugins/chefdb.py
import chef
#print(“In chefdb.py”)
class VarsModule(object):
def init(self, inventory):
“”" constructor “”"
Apparently nothing needed here
##print(“In init with %s” % dir(inventory) )
pass
def run(self, host, vault_password=None):
#print("Starting run . … ")
api = chef.autoconfigure()
#print(“In init, api = %s” % api )
dbag, dbag_item = ‘twelve_factor_environment’, ‘scratch-gmatz-sandbox-app-web’
#print(“dbag = %s” % dbag)
bag = chef.DataBag(dbag)
#print bag
item = bag[dbag_item]
return item.to_dict()
if name == ‘main’:
v = VarsModule(‘inv’)
print v.run(‘hostname’)
Here it is failing when run by ansible:
⟫ ansible-playbook -i inventory site.yml
Traceback (most recent call last):
File “/opt/ansible/bin/ansible-playbook”, line 324, in
sys.exit(main(sys.argv[1:]))
File “/opt/ansible/bin/ansible-playbook”, line 160, in main
inventory = ansible.inventory.Inventory(options.inventory, vault_password=vault_pass)
File “/opt/ansible/lib/ansible/inventory/init.py”, line 149, in init
host.vars = utils.combine_vars(host.vars, self.get_host_variables(host.name, vault_password=self._vault_password))
File “/opt/ansible/lib/ansible/inventory/init.py”, line 445, in get_host_variables
self._vars_per_host[hostname] = self._get_host_variables(hostname, vault_password=vault_password)
File “/opt/ansible/lib/ansible/inventory/init.py”, line 457, in _get_host_variables
vars_results = [ plugin.run(host, vault_password=vault_password) for plugin in self._vars_plugins if hasattr(plugin, ‘run’)]
File “/home/gmatz/Code/mdansible/inventory/vars_plugins/chefdb.py”, line 20, in run
bag = chef.DataBag(dbag)
File “/usr/local/lib/python2.7/dist-packages/chef/base.py”, line 58, in init
data = self.api[self.url]
TypeError: ‘NoneType’ object has no attribute ‘getitem’