salt icon indicating copy to clipboard operation
salt copied to clipboard

transient/incosistant custom grains

Open aabognah opened this issue 10 years ago • 4 comments

Description of Issue/Question

I am using custom grains module to define roles, based on those roles certain states are applied. The roles are managed in a text file on the master that is then read by the custom grains module. but it seems that the module sometimes fails to read the file and that causes the custom grains to be cleared!.

Q1. Why are the custom grains cleared when the module cant read the file? shouldn't custom grains module just update the grains and not clear them?

Q2. where are grains from custom grains module saved? I tried to add logic in the module to read existing grains and reapply them if file not found. is that even possible?

Setup

The roles_file on the master looks like this:

server1: web_server, db_server, dev_server
server2: prod_server, ...

I define these roles in file on the master (in gitfs not locally). the custom grains module then fetches that file, reads it, and applies the roles. it seems one of these steps fails and the custom grains module is not able to get the file. That results is the roles grains being cleared!.

The custom grains in base/_grains/set_roles.py looks like this:

def set_roles():
  # initialize hosts dictionary
  grains = {}

  # read roles data from roles_file
  _get_file('salt://files/roles_file','/tmp/roles_file')

  with open ('/tmp/roles_file' ,'r') as inf:
    for line in inf:
      # get host and roles from line
      host , roles = line.partition(":")[::2]
      if host != _get_hostname():
        continue
      # get roles list for this host after stripping white spaces 
      grains['roles'] = [x.strip() for x in (roles.rstrip()).split(',')]

  return grains

The _get_file function looks like this:

def _get_file(path, dest, saltenv='base'):
    # Get the fileclient object
    client = salt.fileclient.get_file_client(__opts__)
    # Call get_file
    return client.get_file(path, dest, False, saltenv)

Versions Report

(Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)

Salt Version: Salt: 2016.3.3

Dependency Versions: cffi: 0.8.6 cherrypy: 3.2.2 dateutil: 1.5 gitdb: 0.6.4 gitpython: 1.0.1 ioflo: Not Installed Jinja2: 2.7.2 libgit2: 0.21.0 libnacl: Not Installed M2Crypto: 0.21.1 Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.4.8 mysql-python: 1.2.3 pycparser: 2.14 pycrypto: 2.6.1 pygit2: 0.21.4 Python: 2.7.5 (default, Aug 29 2016, 10:12:21) python-gnupg: Not Installed PyYAML: 3.11 PyZMQ: 15.3.0 RAET: Not Installed smmap: 0.9.0 timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.1.4

System Versions: dist: redhat 7.2 Maipo machine: x86_64 release: 3.10.0-327.36.3.el7.x86_64 system: Linux version: Red Hat Enterprise Linux Server 7.2 Maipo

aabognah avatar Nov 04 '16 17:11 aabognah

Hello @aabognah

Unfortunately I do not know the answers for these questions, but I will try to get some answers.

Thanks! Daniel

gtmanfred avatar Nov 08 '16 15:11 gtmanfred

For Q1, I believe that the grains are cleared because we don't update the grains dictionary on a refresh. We dump the whole dictionary of grains and regenerate it.

For Q2, if you set grains_cache: True in the minion config, it will store the grains cache at /var/cache/salt/minion/grains.cache.p as a msgpack object.

gtmanfred avatar Nov 10 '16 15:11 gtmanfred

Thanks @gtmanfred

aabognah avatar Nov 30 '16 22:11 aabognah

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Aug 02 '18 22:08 stale[bot]