Access group_vars from custom module

40 views
Skip to first unread message

Alexandru Obretin

unread,
Oct 3, 2019, 8:52:23 AM10/3/19
to Ansible Development
Hello,

I am developing some custom Ansible modules used for configuring network TAPs and they basically rely on a HTTP connection. Therefore, I have designed a httapi plugin module and I want to get the group_vars information associated to the current host directly from my custom module_utils file. In other words, just before I send HTTP requests to my equipment I need to read the group_vars for that particular IP address in order to set the user/password/timeout and other variables to the request.

So far I have achieved this task using "ansible-invetory --list" but this does not consider scenarios like using a specific inventory file other than /etc/ansible/hosts or defining vars directly inside the playbook.

Is there another way to access the group vars from my module_utils/network custom implementation or from plugins/httpapi?

Regards,
Alex

Alan Rominger

unread,
Oct 3, 2019, 9:02:49 AM10/3/19
to Alexandru Obretin, Ansible Development
The call pattern I'd suggest for ansible-inventory would be like

ansible-inventory -i my_inventory.yml --list --export

With the export option, group_vars should show under the group. I don't understand the rest of your problem about expecting vars from module_utils, but I hope this gets you closer on a few of your points.

Alan
github: AlanCoding


--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/cd616b92-4e5e-4017-9541-f595738103d9%40googlegroups.com.

Alexandru Obretin

unread,
Oct 3, 2019, 9:12:25 AM10/3/19
to Ansible Development
Thanks for the answer Alan.

A broader context of my scenario is the following:

1. In order to send HTTP requests to my equipment I have to be logged in, to be authorized.
2. The username and the password are passed as variables inside the inventory file.
3. Inside the login function in my custom httpapi pluign I try to read the host variables to get the username and the password using ansible-inventory --list or ansible-inventory --list -i path_to_host if my command has the invetory passed with -i.
4. This works good usually, but when I also enable the ANSIBLE_DEBUG variable, besides the regular output of ansible-inventory --list I also get some extra information that will generate a failure inside a json.loads() call. My workaround is to split the response after the string "done with get_vars()" and I am interested in finding a more generic approach instead of this workaround with split after a certain text that can be changed in time.

Regards,
Alex


joi, 3 octombrie 2019, 16:02:49 UTC+3, Alan Rominger a scris:
The call pattern I'd suggest for ansible-inventory would be like

ansible-inventory -i my_inventory.yml --list --export

With the export option, group_vars should show under the group. I don't understand the rest of your problem about expecting vars from module_utils, but I hope this gets you closer on a few of your points.

Alan
github: AlanCoding


On Thu, Oct 3, 2019 at 8:52 AM Alexandru Obretin <alexandru...@gmail.com> wrote:
Hello,

I am developing some custom Ansible modules used for configuring network TAPs and they basically rely on a HTTP connection. Therefore, I have designed a httapi plugin module and I want to get the group_vars information associated to the current host directly from my custom module_utils file. In other words, just before I send HTTP requests to my equipment I need to read the group_vars for that particular IP address in order to set the user/password/timeout and other variables to the request.

So far I have achieved this task using "ansible-invetory --list" but this does not consider scenarios like using a specific inventory file other than /etc/ansible/hosts or defining vars directly inside the playbook.

Is there another way to access the group vars from my module_utils/network custom implementation or from plugins/httpapi?

Regards,
Alex

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansibl...@googlegroups.com.

Alan Rominger

unread,
Oct 3, 2019, 9:31:24 AM10/3/19
to Alexandru Obretin, Ansible Development
Try the environment variable ANSIBLE_VERBOSE_TO_STDERR, and redirect in bash (> or 2>) as necessary. I think there's also a flag to pipe the JSON into a file.

Alan
github: AlanCoding


To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/5e80ba85-27f1-4522-b5cf-15cc2e2f6428%40googlegroups.com.

Magnus Lubeck

unread,
Oct 3, 2019, 12:02:24 PM10/3/19
to Ansible Development
I think you have missed part of the ansible concept, and is mixing up where you loop through the variables. It sounds like you want to call a program that is looping through something you mention in a group_var file, whereas the plugin should just take a single parameter, and do its thing atomically with just the set of options that has been explicitly sent to it.

What you need to do, is to write a wrapper (playbook or role, which is using your plugin).

* The way that your plugin gets data, is per command line parameters. Your module will not really know much about the group vars at all. A plugin is not meant to loop over the ansible host file, or your inventory, an array from group_vars or anything like it. That is done in a role or a playbook.

In your example, you should rather use your playbook or role as (just making up an example):

name: do stuff
  myPlugin: credential={{ some_variable }} target_url={{ item }}
  with_items:

or

name: do stuff
  myPlugin: credential={{ some_variable }} target_url={{ item }}
  with_items: "{{ some_array_in_my_group_vars }}"


//magnus
Reply all
Reply to author
Forward
0 new messages