I am running a Ansible Paybook using the Playbook and Inventory modules in Python. E.g.
import ansible.inventory as ai
inventory = make_inventory(hosts,basedir="ansible")
playbook = PlayBook(
playbook = "ansible/site.yml",
private_key_file = "key.pem",
remote_user = "ec2-user",
remote_pass = None,
become = True,
callbacks = playbook_cb,
runner_callbacks = runner_cb,
stats = stats,
inventory = inventory,
check=False
)
playbook.run()
def make_inventory(hosts,basedir):
inventory = ai.Inventory()
myGroup = ai.group.Group(name="mygroup")
allGroup = inventory.get_group("all")
for h in hosts:
host = ai.host.Host(name=h.ip_address,port=22)
mygroup.add_host(host)
allGroup.add_host(host)
inventory.add_group(mygroup)
inventory.set_playbook_basedir(basedir)
return inventory
This code build an Inventory module from group_vars in the ansible directory, but I would like to override some of those variable in the Inventory before passing it to the Playbook. How can I do this?