jeisenbath / ansible-collection-solarwinds-orion

An Ansible collection for managing nodes in Solarwinds Orion
11 stars 3 forks source link

Inventory Plugin Question #38

Closed Andyjb8 closed 1 month ago

Andyjb8 commented 1 month ago

Is there a way you can you use custom properties for creating groups? Adding a custom property "City" to keyed groups wasn't working for me, but Machine type was working

---
plugin: solarwinds.orion.orion_nodes_inventory
cache: false
strict: false
orion_hostname: 
orion_username: 
orion_password:

compose:
  ansible_host: IP_Address
hostname_field: Caption
hostvar_fields:
  - IP_Address
  - Vendor
  - MachineType

keyed_groups:
  - key: MachineType
    prefix: ''
    separator: ''

  - key: City
    prefix: ''
    separator: ''
jeisenbath commented 1 month ago

In the plugin's doc I have an example for doing that, you need to pull in the custom property first with hostvar_custom_properties before it's available for the keyed_groups. I haven't retested the example in a while, but I'm also pretty sure you need to remember there's a default hostvarprefix "orion" so make sure it's like:

- key: orion_City

unless you change the hostvar_prefix of course.

Andyjb8 commented 1 month ago

Another question, can this inventory plugin read vars from the environment? The reason I am asking is I would like to avoid defining solarwinds vars in multiple places, I tried defining the plugin orion_ vars in the inventory file like shown below to pull the solarwinds vars I already have defined in my group vars.

orion_hostname: "{{ solarwinds_server }}" orion_username: "{{ solarwinds_user }}" orion_password: "{{ solarwinds_password }}"

But the plugin didn't work. If the plugin can pull vars from the environment, then I think AAP (which I am working to standup and test) should be be able to use the plugin and pull defined credentials for it as well.

jeisenbath commented 1 month ago

No, I haven't added loading Environment variables for any of the plugins yet. I imagine that would be fairly simple to implement though.

Andyjb8 commented 1 month ago

Ok, that would be great if you can add that when you get a chance.

Also, when I have the "cache: true" option set it doesn't seem to working as it says Cache needs updating... Populating from source... See below.

Also where is the cache timeout configured? I don't see any options to configure cache timeout

ansible-playbook [core 2.15.12]
  config file = /home/admin/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /root/automation/collections/ansible_collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.9.18 (main, May 16 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
  jinja version = 3.1.4
  libyaml = True
Using /home/admin/ansible.cfg as config file
host_list declined parsing /home/admin/automation/inventory/hosts_orion.yml as it did not pass its verify_file() method
script declined parsing /home/admin/automation/inventory/hosts_orion.yml as it did not pass its verify_file() method
Using inventory plugin 'ansible_collections.solarwinds.orion.plugins.inventory.orion_nodes_inventory' to process inventory source '/home/admin/automation/inventory/hosts_orion.yml'
Getting cached data...
Cache needs updating...
Populating from source...
/usr/lib/python3.9/site-packages/urllib3/connectionpool.py:1061: InsecureRequestWarning: Unverified HTTPS request is being made to host '<my_server>'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
Using query "SELECT NodeID, node.Caption, node.IP_Address, node.Vendor, node.MachineType FROM Orion.Nodes as node "
Building inventory from query data...
Updating cache data...
Parsed /home/admin/automation/inventory/hosts_orion.yml inventory source with auto plugin
redirecting (type: callback) ansible.builtin.profile_tasks to ansible.posix.profile_tasks
jeisenbath commented 1 month ago

Caching settings also need to be set in the ansible.cfg, inventories can't use memory caching but have to use json (or maybe redis, I have only done json).

In case you also were wondering, when I last looked into it there's no current way to configure things like cache timeout differently per plugin. Only the setting in ansible.cfg matters.

Andyjb8 commented 1 month ago

What settings are required in ansible.cfg to cache the inventory plugin? I tried the following options in my ansible.cfg but its still now working for me:

[inventory]
cache=true
cache_plugin=jsonfile
cach_timeout=3600

However I also see the following under these settings in my ansible.cfg the following msg:

#this setting has been moved to the individual inventory plugins as a plugin option :ref: 'inventory_plugins'
# The existing configuration settings are still accepted with the inventory plugin adding additional options from inventory configuration 
# this message will b removed in 2.16

So should the plugin be updated to support these options? I have a large inventory and since its updating from the source every time and not caching it makes every job take longer.

jeisenbath commented 1 month ago

If you run "ansible-doc -t inventory solarwinds.orion.orion_nodes_inventory", I added the doc fragment for inventory_cache and it will list all of the caching settings.

You'll note from there, that the settings for cache_plugin, cache_timeout, cache_prefix, and cache_connection should go in the [defaults] section not [inventory] in your ansible.cfg. I can't find reference to why those are in the [inventory] section in the default ansible.cfg, but the official documentation online for ansible config settings still lists them as [defaults] also.

Andyjb8 commented 1 month ago

I got the inventory cache working, thanks!

jeisenbath commented 1 month ago

Great! I will close this issue, since it looks like you won't have further questions with it working.