salt.wheel.key NameError: global name '__opts__' is not defined 2016.3.1

Created on 14 Jul 2016  路  3Comments  路  Source: saltstack/salt

Description of Issue/Question

Using the salt.wheel.key functions results in stacktrace __opts__ is not defined, maybe I'm doing it wrong?

Steps to reproduce:

my code:

# mykeys.py
import salt.wheel.key
all_keys = salt.wheel.key.list_all()
print all_keys

stacktrace:

Traceback (most recent call last):
  File "mykeys.py", line 3, in <module>
    all_keys = salt.wheel.key.list_all()
  File "/usr/lib/python2.7/dist-packages/salt/wheel/key.py", line 32, in list_all
    skey = salt.key.Key(__opts__)
NameError: global name '__opts__' is not defined

How I got it working

in salt/wheel/key.py I changed list_all() to take __opts__ as a parameter

def list_all(__opts__): 
    '''
    List all the keys
    '''
    skey = salt.key.Key(__opts__)
    return skey.all_keys()

and changed my code to:

# mykeys.py
import salt.wheel.key
__opts__ = salt.config.master_config('/etc/salt/master')
all_keys = salt.wheel.key.list_all(__opts__)
print all_keys

which results in the expected output (indented for readability):

{
    'local': ['master.pem', 'master.pub'],
    'minions_rejected': [],
    'minions_denied': [],
    'minions_pre': [],
    'minions': ['minion1', 'minion2', 'minion3']
}

I'm wondering I'm simply doing it wrong in the first place?

Versions Report

Master

Salt Version:
           Salt: 2016.3.1

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 1.5
          gitdb: 0.5.4
      gitpython: 0.3.2 RC1
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: 0.9.1
   msgpack-pure: Not Installed
 msgpack-python: 0.3.0
   mysql-python: 1.2.3
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.6 (default, Jun 22 2015, 17:58:13)
   python-gnupg: Not Installed
         PyYAML: 3.10
          PyZMQ: 14.0.1
           RAET: Not Installed
          smmap: 0.8.2
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5

System Versions:
           dist: Ubuntu 14.04 trusty
        machine: x86_64
        release: 3.13.0-85-generic
         system: Linux
        version: Ubuntu 14.04 trusty
Question fixed-pending-your-verification

Most helpful comment

Hi @Inveracity - instead of using the salt.wheel.key module directly, which inherits it's __opts__ dict from a running master, you can use the WheelClient to run commands used in the wheel key modules, such as listing keys directly in a Python interpreter:

>>> import salt.config
>>> import salt.wheel
>>> opts = salt.config.master_config('/etc/salt/master')
>>> wheel = salt.wheel.WheelClient(opts)
>>> all_keys = wheel.cmd('key.list_all')
local:
    - master.pem
    - master.pub
minions:
    - rallytime
minions_denied:
minions_pre:
minions_rejected:
>>> print all_keys
{'local': ['master.pem', 'master.pub'], 'minions_rejected': [], 'minions_denied': [], 'minions_pre': [], 'minions': ['rallytime']}

All 3 comments

Hi @Inveracity - instead of using the salt.wheel.key module directly, which inherits it's __opts__ dict from a running master, you can use the WheelClient to run commands used in the wheel key modules, such as listing keys directly in a Python interpreter:

>>> import salt.config
>>> import salt.wheel
>>> opts = salt.config.master_config('/etc/salt/master')
>>> wheel = salt.wheel.WheelClient(opts)
>>> all_keys = wheel.cmd('key.list_all')
local:
    - master.pem
    - master.pub
minions:
    - rallytime
minions_denied:
minions_pre:
minions_rejected:
>>> print all_keys
{'local': ['master.pem', 'master.pub'], 'minions_rejected': [], 'minions_denied': [], 'minions_pre': [], 'minions': ['rallytime']}

excellent!

I managed to completely miss the wheelclient part of the docs, shame on me

thank you! :smile:

@Inveracity No worries! Happy to help!

Was this page helpful?
0 / 5 - 0 ratings