salt.modules.ini_manage

Edit ini files

maintainer:

<akilesh1597@gmail.com>

maturity:

new

depends:

re

platform:

all

(for example /etc/sysctl.conf)

salt.modules.ini_manage.get_ini(file_name, separator='=', encoding=None)

Retrieve the whole structure from an ini file and return it as a dictionary.

Parameters:
  • file_name (str) -- The full path to the ini file.

  • separator (str) --

    The character used to separate keys and values. Standard ini files use the "=" character. The default is =.

    New in version 2016.11.0.

  • encoding (str) --

    A string value representing encoding of the target ini file. If None is passed, it uses the system default which is likely utf-8. Default is None

    New in version 3006.6.

Returns:

A dictionary containing the sections along with the values and

names contained in each section

Return type:

dict

API Example:

import salt.client
with salt.client.get_local_client() as sc:
    sc.cmd('target', 'ini.get_ini', [path_to_ini_file])

CLI Example:

salt '*' ini.get_ini /path/to/ini
salt.modules.ini_manage.get_option(file_name, section, option, separator='=', encoding=None)

Get value of a key from a section in an ini file. Returns None if no matching key was found.

Parameters:
  • file_name (str) -- The full path to the ini file.

  • section (str) -- A string value representing the section of the ini that the option is in. If the option is not in a section, leave this empty.

  • option (str) -- A string value representing the option to search for.

  • separator (str) --

    The character used to separate keys and values. Standard ini files use the "=" character. The default is =.

    New in version 2016.11.0.

  • encoding (str) --

    A string value representing encoding of the target ini file. If None is passed, it uses the system default which is likely utf-8. Default is None

    New in version 3006.6.

Returns:

The value as defined in the ini file, or None if empty or not

found

Return type:

str

API Example:

import salt.client
with salt.client.get_local_client() as sc:
    sc.cmd('target', 'ini.get_option', [path_to_ini_file, section_name, option])

CLI Example:

salt '*' ini.get_option /path/to/ini section_name option_name
salt.modules.ini_manage.get_section(file_name, section, separator='=', encoding=None)

Retrieve a section from an ini file. Returns the section as a dictionary. If the section is not found, an empty dictionary is returned.

Parameters:
  • file_name (str) -- The full path to the ini file.

  • section (str) -- A string value representing name of the section to search for.

  • separator (str) --

    The character used to separate keys and values. Standard ini files use the "=" character. The default is =.

    New in version 2016.11.0.

  • encoding (str) --

    A string value representing encoding of the target ini file. If None is passed, it uses the system default which is likely utf-8. Default is None

    New in version 3006.6.

Returns:

A dictionary containing the names and values of all items in the

section of the ini file. If the section is not found, an empty dictionary is returned

Return type:

dict

API Example:

import salt.client
with salt.client.get_local_client() as sc:
    sc.cmd('target', 'ini.get_section', [path_to_ini_file, section_name])

CLI Example:

salt '*' ini.get_section /path/to/ini section_name
salt.modules.ini_manage.remove_option(file_name, section, option, separator='=', encoding=None)

Remove a key/value pair from a section in an ini file. Returns the value of the removed key, or None if nothing was removed.

Parameters:
  • file_name (str) -- The full path to the ini file.

  • section (str) -- A string value representing the section of the ini that the option is in. If the option is not in a section, leave this empty.

  • option (str) -- A string value representing the option to search for.

  • separator (str) --

    The character used to separate keys and values. Standard ini files use the "=" character. The default is =.

    New in version 2016.11.0.

  • encoding (str) --

    A string value representing encoding of the target ini file. If None is passed, it uses the system default which is likely utf-8. Default is None

    New in version 3006.6.

Returns:

A string value representing the option that was removed or None

if nothing was removed

Return type:

str

API Example:

import salt
sc = salt.client.get_local_client()
sc.cmd('target', 'ini.remove_option', [path_to_ini_file, section_name, option])

CLI Example:

salt '*' ini.remove_option /path/to/ini section_name option_name
salt.modules.ini_manage.remove_section(file_name, section, separator='=', encoding=None)

Remove a section in an ini file. Returns the removed section as a dictionary, or None if nothing is removed.

Parameters:
  • file_name (str) -- The full path to the ini file.

  • section (str) -- A string value representing the name of the section search for.

  • separator (str) --

    The character used to separate keys and values. Standard ini files use the "=" character. The default is =.

    New in version 2016.11.0.

  • encoding (str) --

    A string value representing encoding of the target ini file. If None is passed, it uses the system default which is likely utf-8. Default is None

    New in version 3006.6.

Returns:

A dictionary containing the names and values of all items in the

section that was removed or None if nothing was removed

Return type:

dict

API Example:

import salt.client
with  salt.client.get_local_client() as sc:
    sc.cmd('target', 'ini.remove_section', [path_to_ini_file, section_name])

CLI Example:

salt '*' ini.remove_section /path/to/ini section_name
salt.modules.ini_manage.set_option(file_name, sections=None, separator='=', encoding=None)

Edit an ini file, replacing one or more sections. Returns a dictionary containing the changes made.

Parameters:
  • file_name (str) -- The full path to the ini file.

  • sections (dict) -- A dictionary representing the sections to be edited in the ini file. The keys are the section names and the values are a dictionary containing the options. If the ini file does not contain sections the keys and values represent the options. The default is None.

  • separator (str) --

    The character used to separate keys and values. Standard ini files use the "=" character. The default is =.

    New in version 2016.11.0.

  • encoding (str) --

    A string value representing encoding of the target ini file. If None is passed, it uses the system default which is likely utf-8. Default is None

    New in version 3006.6.

Returns:

A dictionary representing the changes made to the ini file

Return type:

dict

API Example:

import salt.client
with salt.client.get_local_client() as sc:
    sc.cmd(
        'target', 'ini.set_option', ['path_to_ini_file', '{"section_to_change": {"key": "value"}}']
    )

CLI Example:

salt '*' ini.set_option /path/to/ini '{section_foo: {key: value}}'