def ara_config(key, env_var, default, section='ara', value_type=None): """ Wrapper around Ansible's get_config backward/forward compatibility """ # Bootstrap Ansible configuration # Ansible >=2.4 takes care of loading the configuration file itself if LooseVersion(ansible_version) < LooseVersion('2.4.0'): config, path = load_config_file() else: path = find_ini_config_file() config = configparser.ConfigParser() if path is not None: config.read(path) # >= 2.3.0.0 (NOTE: Ansible trunk versioning scheme has 3 digits, not 4) if LooseVersion(ansible_version) >= LooseVersion('2.3.0'): return get_config(config, section, key, env_var, default, value_type=value_type) # < 2.3.0.0 compatibility if value_type is None: return get_config(config, section, key, env_var, default) args = { 'boolean': dict(boolean=True), 'integer': dict(integer=True), 'list': dict(islist=True), 'tmppath': dict(istmppath=True) } return get_config(config, section, key, env_var, default, **args[value_type])
def main(): (parser, config_path) = C.load_config_file() if parser.has_option('vault', 'username'): username = parser.get('vault', 'username') else: username = getpass.getuser() if parser.has_option('vault', 'keyname'): keyname = parser.get('vault', 'keyname') else: keyname = 'ansible' if len(sys.argv) == 2 and sys.argv[1] == 'set': intro = 'Storing password in "{}" user keyring using key name: {}\n' sys.stdout.write(intro.format(username, keyname)) password = getpass.getpass() confirm = getpass.getpass('Confirm password: '******'Passwords do not match\n') sys.exit(1) else: sys.stdout.write('{}\n'.format(keyring.get_password(keyname, username))) sys.exit(0)
def __init__(self): super(CallbackModule, self).__init__() dburi = None cfg, cfgpath = load_config_file() if (cfg and cfg.has_section('sql_logger') and cfg.has_option('sql_logger', 'database')): dburi = cfg.get('sql_logger', 'database') if dburi is None: dburi = DEFAULT_DBURI self.dburi = dburi LOG.debug('using dburi %s', dburi) model.initdb(dburi) # need to reset these on task completion self._playbook = None self._play = None self._task = None self.start_run()
def read_config_file(self): allowed_items = { # name: type 'cwd': 'str', 'forks': 'int', } config = C.load_config_file() if config is not None and config.has_section('ansible-shell'): for item in config.items('ansible-shell'): if item[0] in allowed_items.keys(): value = vars(__builtins__)[allowed_items[item[0]]](item[1]) setattr(self.options, item[0], value)
def _get_defaults(self): p, cfg_path = load_config_file() defaults_file = get_config(p, DEFAULTS, 'var_defaults_file', 'ANSIBLE_VAR_DEFAULTS_FILE', None) if not defaults_file: return None ursula_env = os.environ.get('URSULA_ENV', '') defaults_path = os.path.join(ursula_env, defaults_file) if os.path.exists(defaults_path): with open(defaults_path) as fh: return yaml.safe_load(fh) return None
def load_ssh_args(): """ Read ansible.cfg and get only the the ssh_args parameter if defined :return: Output of ansible ssh_args """ config, _ = load_config_file() # It's easier to ask for forgiveness than permission try: ssh_section = config._sections['ssh_connection'] return ssh_section['ssh_args'] except KeyError: return None
def __init__(self, inventory): self.inventory = inventory self.inventory_basedir = inventory.basedir() p, _ = load_config_file() self.pre_template_enabled = get_config(p, DEFAULTS, 'var_pre_template', 'ANSIBLE_VAR_PRE_TEMPLATE', False, boolean=True) self.defaults_glob = get_config(p, DEFAULTS, 'var_defaults_glob', 'ANSIBLE_VAR_DEFAULTS_GLOB', None) self._templar = None
def __init__(self, repo_base=None): # Get repository base if repo_base is None and is_git_repo(repo_base): repo_base = get_git_root() if not is_git_repo(repo_base): raise ValueError("Not a GIT repository: %s" % repo_base) self.repo_base = repo_base # Load configuration self._config = config.AnsibleConfig(self.repo_base) # Base path of Ansible self.base = os.path.join(self.repo_base, self._config.base_dir()) if not os.path.isdir(self.base): raise IOError("Base Ansible directory doesn't exist in %s" % self.base) # Change the working directory to the root of the repository old_cwd = os.getcwd() os.chdir(self.base) self._p, _ = C.load_config_file() os.chdir(old_cwd) self._set_executables() # Buffers self._tags = None self._playbooks = None self._vaults = None # Base paths self.roles_base = paths_full(self.base, "roles") self.playbooks_base = paths_full(self.base, self._config.playbooks_dir()) self.inventory_base = self.ans_config('defaults', 'inventory', '/etc/ansible/hosts') # Others self.run_as = self._config.run_as() self.vault_file = self._config.vault_file() self.ssh_key = self._config.ssh_key_file() self.dynainv_file = self._config.dynamic_inventory_file() self.dynainv_path = paths_full('scripts/ansible', self.dynainv_file)
def main(): parser = C.load_config_file() try: username = parser.get('vault', 'username') except: sys.stderr.write('No [vault] section configured\n') sys.exit(1) if len(sys.argv) == 2 and sys.argv[1] == 'set': password = getpass.getpass() confirm = getpass.getpass('Confirm password: '******'ansible', username, password) else: sys.stderr.write('Passwords do not match\n') sys.exit(1) else: sys.stdout.write('%s\n' % keyring.get_password('ansible', username)) sys.exit(0)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from ansible.constants import get_config, load_config_file import os from flask import Flask from flask_sqlalchemy import SQLAlchemy DEFAULT_DATABASE = os.path.expanduser('~/.ara/ansible.sqlite') config, file = load_config_file() DATABASE = get_config(config, 'ara', 'database', 'ARA_DATABASE', DEFAULT_DATABASE) # TODO (dmsimard): Figure out the best place and way to initialize the # database if it hasn't been created yet. try: if not os.path.exists(os.path.dirname(DATABASE)): os.makedirs(os.path.dirname(DATABASE)) except Exception as e: raise IOError("Unable to ensure database directory exists. " + str(e)) app = Flask(__name__) app.config['DATABASE'] = DATABASE app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///{0}".format(DATABASE) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# under the License. import os from ansible.constants import get_config, load_config_file DEFAULT_ARA_DIR = os.path.expanduser('~/.ara') DEFAULT_DATABASE_PATH = os.path.join(DEFAULT_ARA_DIR, 'ansible.sqlite') DEFAULT_DATABASE = 'sqlite:///{}'.format(DEFAULT_DATABASE_PATH) DEFAULT_ARA_LOGFILE = os.path.join(DEFAULT_ARA_DIR, 'ara.log') DEFAULT_ARA_LOG_LEVEL = 'INFO' DEFAULT_ARA_LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' DEFAULT_ARA_SQL_DEBUG = False DEFAULT_ARA_PATH_MAX = 30 config, path = load_config_file() ARA_DIR = get_config( config, 'ara', 'dir', 'ARA_DIR', DEFAULT_ARA_DIR) ARA_LOG_FILE = get_config( config, 'ara', 'logfile', 'ARA_LOG_FILE', DEFAULT_ARA_LOGFILE) ARA_LOG_LEVEL = get_config( config, 'ara', 'loglevel', 'ARA_LOG_LEVEL', DEFAULT_ARA_LOG_LEVEL).upper() ARA_LOG_FORMAT = get_config( config, 'ara', 'logformat', 'ARA_LOG_FORMAT', DEFAULT_ARA_LOG_FORMAT) ARA_PATH_MAX = get_config( config, 'ara', 'path_max', 'ARA_PATH_MAX',
'ARA_AUTOCREATE_DATABASE': True, 'ARA_DIR': os.path.expanduser('~/.ara'), 'ARA_ENABLE_DEBUG_VIEW': False, 'ARA_IGNORE_MIMETYPE_WARNINGS': True, 'ARA_LOG_FORMAT': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', 'ARA_LOG_LEVEL': 'INFO', 'ARA_PATH_MAX': 40, 'ARA_PLAYBOOK_OVERRIDE': None, 'ARA_PLAYBOOK_PER_PAGE': 10, 'ARA_RESULT_PER_PAGE': 25, 'ARA_SQL_DEBUG': False, 'ARA_TMP_DIR': os.path.expanduser('~/.ansible/tmp') } # Bootstrap Ansible configuration config, path = load_config_file() # Some defaults need to be based on top of a "processed" ARA_DIR ARA_DIR = _ara_config(config, 'dir', 'ARA_DIR') database_path = os.path.join(ARA_DIR, 'ansible.sqlite') DEFAULTS.update({ 'ARA_LOG_FILE': os.path.join(ARA_DIR, 'ara.log'), 'ARA_DATABASE': 'sqlite:///{}'.format(database_path) }) ARA_AUTOCREATE_DATABASE = _ara_config(config, 'autocreate_database', 'ARA_AUTOCREATE_DATABASE', value_type='boolean') ARA_ENABLE_DEBUG_VIEW = _ara_config(config, 'enable_debug_view',
def get_config_value(key, env_var, default): """ Look up key in ansible.cfg This uses load_config_file() and get_config() from ansible.constants """ config = AC.load_config_file() return AC.get_config(config, DEFAULT_SECTION, key, env_var, default)
def _get_defaults(self): p = load_config_file() defaults_file = get_config(p, DEFAULTS, 'var_defaults_file', 'ANSIBLE_VAR_DEFAULTS_FILE', None) if defaults_file: return yaml.load(open(defaults_file))
from pysnmp.entity.rfc3413.oneliner import cmdgen from pysnmp.entity.rfc3413 import mibvar from pysnmp.entity import engine from pysnmp.proto import rfc1902 from pysnmp.proto import rfc1905 from pyasn1.type import univ from pysnmp.carrier.asynsock.dgram import udp __all__ = ['Connection', 'SnmpValue', 'OctetString', 'ObjectIdentifier', 'Integer32', 'Counter32', 'IpAddress', 'Gauge32', 'TimeTicks', 'Opaque', 'Counter64', 'SnmpClient', 'SnmpError'] _cache = dict() _snmp_engine = None p = constants.load_config_file() SNMP_AUTH_PROTOCOL = constants.get_config(p, 'snmp', 'auth_protocol', 'SNMP_AUTH_PROTOCOL', 'none').lower() SNMP_PRIV_PROTOCOL = constants.get_config(p, 'snmp', 'priv_protocol', 'SNMP_PRIV_PROTOCOL', 'none').lower() SNMP_ENGINE_ID = constants.get_config(p, 'snmp', 'engine_id', 'SNMP_ENGINE_ID', None) SNMP_COMMUNITY = constants.get_config(p, 'snmp', 'community', 'SNMP_COMMUNITY', None) SNMP_AUTH_KEY = constants.get_config(p, 'snmp', 'auth_key', 'SNMP_AUTH_KEY', None) SNMP_PRIV_KEY = constants.get_config(p, 'snmp', 'priv_key', 'SNMP_PRIV_KEY', None) class Connection(object): """ SNMP based connections """ def __init__(self, runner, host, port, *args, **kwargs): self.runner = runner self.host = host self.port = port if port else 161 self.has_pipelining = False
from pysnmp.entity import engine from pysnmp.proto import rfc1902 from pysnmp.proto import rfc1905 from pyasn1.type import univ from pysnmp.carrier.asynsock.dgram import udp __all__ = [ 'Connection', 'SnmpValue', 'OctetString', 'ObjectIdentifier', 'Integer32', 'Counter32', 'IpAddress', 'Gauge32', 'TimeTicks', 'Opaque', 'Counter64', 'SnmpClient', 'SnmpError' ] _cache = dict() _snmp_engine = None p = constants.load_config_file() SNMP_AUTH_PROTOCOL = constants.get_config(p, 'snmp', 'auth_protocol', 'SNMP_AUTH_PROTOCOL', 'none').lower() SNMP_PRIV_PROTOCOL = constants.get_config(p, 'snmp', 'priv_protocol', 'SNMP_PRIV_PROTOCOL', 'none').lower() SNMP_ENGINE_ID = constants.get_config(p, 'snmp', 'engine_id', 'SNMP_ENGINE_ID', None) SNMP_COMMUNITY = constants.get_config(p, 'snmp', 'community', 'SNMP_COMMUNITY', None) SNMP_AUTH_KEY = constants.get_config(p, 'snmp', 'auth_key', 'SNMP_AUTH_KEY', None) SNMP_PRIV_KEY = constants.get_config(p, 'snmp', 'priv_key', 'SNMP_PRIV_KEY', None)