Home:ALL Converter>Ansible Inventory module override group variables in Python

Ansible Inventory module override group variables in Python

Ask Time:2015-08-07T09:19:44         Author:aaa90210

Json Formatter

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?

Author:aaa90210,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31868262/ansible-inventory-module-override-group-variables-in-python
yy