salt icon indicating copy to clipboard operation
salt copied to clipboard

Need help in putting api url for range rosters.

Open Set4now opened this issue 9 years ago • 18 comments

Hi, we have developed a roster module for range. We have backen DB where the host info will be stored. that wuill have a API url. our custom module will fetch the data and use it as a roster for range. which is working fine.

But am hard coding the URL in the custom module. But i need to put this in the master file, as we have for ext_pillar, git.. Please guide me how to achieve this.

snippet of my code.

def targets(tgt, tgt_type='glob', **kwargs):

  url="http://10.X>X>X>/api/v1/viewentities?where={%22EntityId%22:%22" + tgt + "%22}"

===================

i was thinking to get something like this but seems not working.

url_list=[]
def cmdb_url(config)
  url_list.append(cmdb_url)

def targets(tgt, tgt_type='glob', **kwargs):
   url="{}?where={%22EntityId%22:%22{}%22}".format(url_list[0], tgt)

And in the /etc/salt/master

  #cmdb_url:
  #  - cricloudroster_test:
 #      config: http://10.X>X>X>/api/v1/viewentities

Set4now avatar Jul 10 '17 11:07 Set4now

@Set4now if you are just trying to grab a key value from master config you can use config.get. If you look at other examples throughout salts code you can see some great examples.

Ch3LL avatar Jul 10 '17 21:07 Ch3LL

i have tried the same..this is my entry in master file. config: cmdb_url: http://10.236.220.130:5056/api/v1/viewentities

salt master config.get config:cmdb_url -l debug [DEBUG ] LazyLoaded nested.output master: [DEBUG ] jid 20170711101735404400 found all minions set(['master'])

Showing nothing...

Set4now avatar Jul 11 '17 10:07 Set4now

following one of the link. https://github.com/saltstack/salt/issues/12916

Set4now avatar Jul 11 '17 10:07 Set4now

hey...please ignore... i figured it out... i either had to change pillar_opts to True in master file or set key: value on minion file. But what i noticed is salt(config.get)['key:value) cant be used in roster modules. so had to use python subprocess like this.

cmdburl=subprocess.check_output(['salt', 'master', 'config.get', 'config:cmdb_url']).split(' ')

Set4now avatar Jul 11 '17 12:07 Set4now

are you not able to pull this information from __opts__['config']['cmdb_url']?

__opts__ for the master config file should be available in the roster files.

gtmanfred avatar Jul 11 '17 17:07 gtmanfred

When you do

salt master config.get config:cmdb_url -l debug

You're:

  • Talking to the Salt Master process
  • Asking it to send out a job for the Minion named master
  • Get that Minion to deliver the Minion config.get

However, Salt SSH uses the master config; you need to put it in in there and use it in the roster, either through __salt__['config.get']() (not sure if that exists at all, you might need to import some salt.util directly for that) or __opts__

The-Loeki avatar Jul 12 '17 06:07 The-Loeki

can you tell me real quick which module from salt to import?

Set4now avatar Jul 12 '17 07:07 Set4now

salt.config.master_config ? Not sure by heart; I think __opts__ seems better in your case.

Check the source tree if you want; it's a lot to swallow at first, but it's hierarchy is extremely easy to follow. Want to know what the module function config.get does? That's in salt/modules/config.py. Want to know what the util does? That's in salt/utils/func.py :) And for a readout of aforementioned config it's in salt/config/

The-Loeki avatar Jul 12 '17 07:07 The-Loeki

Also btw, be aware that pillar_opts: True exposes ALL MASTER CONFIG to all minions as pillar data. If there's keys in there etc. they'll be passed on as well.

The-Loeki avatar Jul 12 '17 07:07 The-Loeki

Yea...i have decided to not use piialr-opts. but seems import salt.config.master_config doesnt seem to work

Set4now avatar Jul 12 '17 08:07 Set4now

okay seems like i found a way.. import salt.config opts = salt.config.minion_config('/etc/salt/master') print opts["config"]["cmdb_url"]

any better idea then this... i have tried using __opt__ but its saying...undefined

Set4now avatar Jul 12 '17 08:07 Set4now

first of all its __opts__ but I'm guessing you had that; second possibility is that you're trying to access it outside of your functions, which won't work because it's not set at import time:

import log

log = logging.getLogger(__name__)

log.error(__opts__)  # b0rks/None/something

def targets(**kwargs):
  log.info(__opts__)  # should work

The-Loeki avatar Jul 12 '17 08:07 The-Loeki

opts what i meant!! :-)

import urllib
import requests
import json
import subprocess
import salt
import salt.config

def targets(tgt, tgt_type='glob', **kwargs):

  #cmdburl=subprocess.check_output(['salt', 'master', 'config.get', 'config:cmdb_url']).split(' ')
  opts=salt.config.minion_config('/etc/salt/master')
  baseurl=__opts__["config"]["cmdb_url"]

Set4now avatar Jul 12 '17 08:07 Set4now

do to some reason its showing opts in bold but in real its what u said

Set4now avatar Jul 12 '17 09:07 Set4now

you are using 2x _ on each side aren't you?

The-Loeki avatar Jul 12 '17 09:07 The-Loeki

_ _ opts _ _ w/spaces added

The-Loeki avatar Jul 12 '17 09:07 The-Loeki

Also, please learn the triple quote syntax; as you can see it's much more legible to use when pasting code.

The-Loeki avatar Jul 12 '17 09:07 The-Loeki

https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code

The-Loeki avatar Jul 12 '17 09:07 The-Loeki