Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying ansible global group variables via (python)

Tags:

python

ansible

I'm trying to query global group variables set in Ansible. I seem to be getting an empty dictionary and I'm not sure what else I can do. My code looks like this:

def __init__(self, inventory_path=None):
    self.loader = DataLoader()
    self.variable_manager = VariableManager()
    self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory_path)
    self.variable_manager.set_inventory(self.inventory)

when I then try to get group vars as below:

    inventory_asg_groups = filter(lambda g: 'asg' in g, self.inventory.groups)
    for group in inventory_asg_groups:
        print(self.inventory.get_group_vars(self.inventory.get_group(group)))

I get an empty dictionary:

{}

when I just do a:

    print(self.inventory.localhost.vars)

I get this:

{'ansible_python_interpreter': '/usr/local/opt/python/bin/python2.7', 'ansible_connection': 'local'}

I know the inventory is being loaded, since I list all the groups in the inventory. How do I get the variables listed in group_vars/all via the python ansible api?

like image 923
adele dazim Avatar asked Aug 28 '16 03:08

adele dazim


People also ask

How are global variables defined in Ansible?

Variable Scopes Global: this is set by config, environment variables and the command line. Play: each play and contained structures, vars entries, include_vars, role defaults and vars. Host: variables directly associated to a host, like inventory, facts or registered task outputs.

What is Host_vars and Group_vars in Ansible?

The host_vars is a similar folder to group_vars in the repository structure. It contains data models that apply to individual hosts/devices in the hosts. ini file. Hence, there is a YAML file created per device containing specific information about that device.

How do you read an Ansible variable?

To define a variable in a playbook, simply use the keyword vars before writing your variables with indentation. To access the value of the variable, place it between the double curly braces enclosed with quotation marks. In the above playbook, the greeting variable is substituted by the value Hello world!

What is Ansible_host?

ansible_host. The name of the host to connect to, if different from the alias you wish to give to it. ansible_port. The connection port number, if not the default (22 for ssh) ansible_user.


1 Answers

This actually works if you have group-specific variables defined (group_vars/.yml). This is because get_group_vars directly read 'all' you can use self.inventory.get_group_vars(self.inventory.get_group('all')).

Full example below:

dirt/hosts

# hosts
[asg_2]

localhost1

[asg_1]
localhost2

[something]
localhost3

dir/group_vars/all.yml

setting_something: "5"

dir/group_vars/ags_1.yml

setting_something: "6500"

dir/ansible_loader.py

from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory

class AnsibleLoader:
    def __init__(self, inventory_path=None):
        self.loader = DataLoader()
        self.variable_manager = VariableManager()
        self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory_path)
        self.variable_manager.set_inventory(self.inventory)

        inventory_asg_groups = filter(lambda g: 'asg' in g, self.inventory.groups)

        print(inventory_asg_groups)
        for group in inventory_asg_groups:
            print('vars for: %s' % group)
            print(self.inventory.get_group_vars(self.inventory.get_group(group)))

        print(self.inventory.get_group_vars(self.inventory.get_group('all')))


AnsibleLoader(inventory_path='hosts')

Output

[u'asg_2', u'asg_1']
vars for: asg_2
{}
vars for: asg_1
{u'setting_something': u'6500'}
{u'setting_something': u'5'}

So it prints values for asg_1 as it has file in group_vars, but not for asg_2. Last line is accessing 'all'

All done with ansible 2.0, not sure which version you use.

like image 176
Eduard Avatar answered Sep 27 '22 22:09

Eduard