def persist(name, value, config='/etc/sysctl.conf'):
    '''
    Assign and persist a simple sysctl parameter for this minion

    CLI Example:

    .. code-block:: bash

        salt '*' sysctl.persist net.inet.icmp.icmplim 50
    '''
    nlines = []
    edited = False
    value = six.text_type(value)

    # create /etc/sysctl.conf if not present
    if not os.path.isfile(config):
        try:
            with salt.utils.files.fopen(config, 'w+'):
                pass
        except (IOError, OSError):
            msg = 'Could not create {0}'
            raise CommandExecutionError(msg.format(config))

    with salt.utils.files.fopen(config, 'r') as ifile:
        for line in ifile:
            line = salt.utils.stringutils.to_unicode(line)
            m = re.match(r'{0}(\??=)'.format(name), line)
            if not m:
                nlines.append(line)
                continue
            else:
                key, rest = line.split('=', 1)
                if rest.startswith('"'):
                    _, rest_v, rest = rest.split('"', 2)
                elif rest.startswith('\''):
                    _, rest_v, rest = rest.split('\'', 2)
                else:
                    rest_v = rest.split()[0]
                    rest = rest[len(rest_v):]
                if rest_v == value:
                    return 'Already set'
                new_line = '{0}{1}{2}{3}'.format(name, m.group(1), value, rest)
                nlines.append(new_line)
                edited = True

    if not edited:
        newline = '{0}={1}'.format(name, value)
        nlines.append("{0}\n".format(newline))

    with salt.utils.files.fopen(config, 'wb') as ofile:
        ofile.writelines(
            salt.utils.data.encode(nlines)
        )

    assign(name, value)

    return 'Updated'
def persist ( name , value , config = '/etc/sysctl.conf' ) : nlines = [ ] edited = False value = six . text_type ( value ) if not os . path . isfile ( config ) : try : with salt . utils . files . fopen ( config , 'w+' ) : pass except ( IOError , OSError ) : msg = 'Could not create {0}' raise CommandExecutionError ( msg . format ( config ) ) with salt . utils . files . fopen ( config , 'r' ) as ifile : for line in ifile : line = salt . utils . stringutils . to_unicode ( line ) m = re . match ( r'{0}(\??=)' . format ( name ) , line ) if not m : nlines . append ( line ) continue else : key , rest = line . split ( '=' , 1 ) if rest . startswith ( '"' ) : _ , rest_v , rest = rest . split ( '"' , 2 ) elif rest . startswith ( '\'' ) : _ , rest_v , rest = rest . split ( '\'' , 2 ) else : rest_v = rest . split ( ) [ 0 ] rest = rest [ len ( rest_v ) : ] if rest_v == value : return 'Already set' new_line = '{0}{1}{2}{3}' . format ( name , m . group ( 1 ) , value , rest ) nlines . append ( new_line ) edited = True if not edited : newline = '{0}={1}' . format ( name , value ) nlines . append ( "{0}\n" . format ( newline ) ) with salt . utils . files . fopen ( config , 'wb' ) as ofile : ofile . writelines ( salt . utils . data . encode ( nlines ) ) assign ( name , value ) return 'Updated'
Assign and persist a simple sysctl parameter for this minion

    CLI Example:

    .. code-block:: bash

        salt '*' sysctl.persist net.inet.icmp.icmplim 50