|
| 1 | +# (c) 2017, Ansible by Red Hat, Inc. |
| 2 | +# |
| 3 | +# Ansible is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# Ansible is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +# |
| 16 | +# ansible-vault is a script that encrypts/decrypts YAML files. See |
| 17 | +# http://docs.ansible.com/playbooks_vault.html for more details. |
| 18 | + |
| 19 | +from __future__ import (absolute_import, division, print_function) |
| 20 | +__metaclass__ = type |
| 21 | + |
| 22 | +import os |
| 23 | +import shlex |
| 24 | +import subprocess |
| 25 | +import sys |
| 26 | +import yaml |
| 27 | + |
| 28 | +from ansible.cli import CLI |
| 29 | +from ansible.config.data import Setting |
| 30 | +from ansible.config.manager import ConfigManager |
| 31 | +from ansible.errors import AnsibleError, AnsibleOptionsError |
| 32 | +from ansible.module_utils._text import to_native, to_text |
| 33 | +from ansible.parsing.yaml.dumper import AnsibleDumper |
| 34 | +from ansible.utils.color import stringc |
| 35 | +from ansible.utils.path import unfrackpath |
| 36 | + |
| 37 | + |
| 38 | +try: |
| 39 | + from __main__ import display |
| 40 | +except ImportError: |
| 41 | + from ansible.utils.display import Display |
| 42 | + display = Display() |
| 43 | + |
| 44 | + |
| 45 | +class ConfigCLI(CLI): |
| 46 | + """ Config command line class """ |
| 47 | + |
| 48 | + VALID_ACTIONS = ("view", "edit", "update", "dump", "list") |
| 49 | + |
| 50 | + def __init__(self, args, callback=None): |
| 51 | + |
| 52 | + self.config_file = None |
| 53 | + self.config = None |
| 54 | + super(ConfigCLI, self).__init__(args, callback) |
| 55 | + |
| 56 | + def parse(self): |
| 57 | + |
| 58 | + self.parser = CLI.base_parser( |
| 59 | + usage = "usage: %%prog [%s] [--help] [options] [ansible.cfg]" % "|".join(self.VALID_ACTIONS), |
| 60 | + epilog = "\nSee '%s <command> --help' for more information on a specific command.\n\n" % os.path.basename(sys.argv[0]) |
| 61 | + ) |
| 62 | + |
| 63 | + self.parser.add_option('-c', '--config', dest='config_file', help="path to configuration file, defaults to first file found in precedence.") |
| 64 | + |
| 65 | + self.set_action() |
| 66 | + |
| 67 | + # options specific to self.actions |
| 68 | + if self.action == "list": |
| 69 | + self.parser.set_usage("usage: %prog list [options] ") |
| 70 | + if self.action == "dump": |
| 71 | + self.parser.set_usage("usage: %prog dump [options] [-c ansible.cfg]") |
| 72 | + elif self.action == "view": |
| 73 | + self.parser.set_usage("usage: %prog view [options] [-c ansible.cfg] ") |
| 74 | + elif self.action == "edit": |
| 75 | + self.parser.set_usage("usage: %prog edit [options] [-c ansible.cfg]") |
| 76 | + elif self.action == "update": |
| 77 | + self.parser.add_option('-s', '--setting', dest='setting', help="config setting, the section defaults to 'defaults'") |
| 78 | + self.parser.set_usage("usage: %prog update [options] [-c ansible.cfg] -s '[section.]setting=value'") |
| 79 | + |
| 80 | + self.options, self.args = self.parser.parse_args() |
| 81 | + display.verbosity = self.options.verbosity |
| 82 | + |
| 83 | + def run(self): |
| 84 | + |
| 85 | + super(ConfigCLI, self).run() |
| 86 | + |
| 87 | + if self.options.config_file: |
| 88 | + self.config_file = unfrackpath(self.options.config_file, follow=False) |
| 89 | + self.config = ConfigManager(self.config_file) |
| 90 | + else: |
| 91 | + self.config = ConfigManager() |
| 92 | + self.config_file = self.config.data.get_setting('ANSIBLE_CONFIG') |
| 93 | + try: |
| 94 | + if not os.path.exists(self.config_file): |
| 95 | + raise AnsibleOptionsError("%s does not exist or is not accessible" % (self.config_file)) |
| 96 | + elif not os.path.isfile(self.config_file): |
| 97 | + raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) |
| 98 | + |
| 99 | + os.environ['ANSIBLE_CONFIG'] = self.config_file |
| 100 | + except: |
| 101 | + if self.action in ['view']: |
| 102 | + raise |
| 103 | + elif self.action in ['edit', 'update']: |
| 104 | + display.warning("File does not exist, used empty file: %s" % self.config_file) |
| 105 | + |
| 106 | + self.execute() |
| 107 | + |
| 108 | + def execute_update(self): |
| 109 | + ''' |
| 110 | + Updates a single setting in the specified ansible.cfg |
| 111 | + ''' |
| 112 | + raise AnsibleError("Option not implemented yet") |
| 113 | + |
| 114 | + if self.options.setting is None: |
| 115 | + raise AnsibleOptionsError("update option requries a setting to update") |
| 116 | + |
| 117 | + (entry, value) = self.options.setting.split('=') |
| 118 | + if '.' in entry: |
| 119 | + (section, option) = entry.split('.') |
| 120 | + else: |
| 121 | + section = 'defaults' |
| 122 | + option = entry |
| 123 | + subprocess.call([ |
| 124 | + 'ansible', |
| 125 | + '-m','ini_file', |
| 126 | + 'localhost', |
| 127 | + '-c','local', |
| 128 | + '-a','"dest=%s section=%s option=%s value=%s backup=yes"' % (self.config_file, section, option, value) |
| 129 | + ]) |
| 130 | + |
| 131 | + def execute_view(self): |
| 132 | + ''' |
| 133 | + Displays the current config file |
| 134 | + ''' |
| 135 | + try: |
| 136 | + with open(self.config_file, 'rb') as f: |
| 137 | + self.pager(to_text(f.read(), errors='surrogate_or_strict')) |
| 138 | + except Exception as e: |
| 139 | + raise AnsibleError("Failed to open config file: %s" % to_native(e)) |
| 140 | + |
| 141 | + def execute_edit(self): |
| 142 | + ''' |
| 143 | + Opens ansible.cfg in the default EDITOR |
| 144 | + ''' |
| 145 | + raise AnsibleError("Option not implemented yet") |
| 146 | + try: |
| 147 | + editor = shlex.split(os.environ.get('EDITOR','vi')) |
| 148 | + editor.append(self.config_file) |
| 149 | + subprocess.call(editor) |
| 150 | + except Exception as e: |
| 151 | + raise AnsibleError("Failed to open editor: %s" % to_native(e)) |
| 152 | + |
| 153 | + def execute_list(self): |
| 154 | + ''' |
| 155 | + list all current configs reading lib/constants.py and shows env and config file setting names |
| 156 | + ''' |
| 157 | + self.pager(to_text(yaml.dump(self.config.initial_defs, Dumper=AnsibleDumper), errors='surrogate_or_strict')) |
| 158 | + |
| 159 | + def execute_dump(self): |
| 160 | + ''' |
| 161 | + Shows the current settings, merges ansible.cfg if specified |
| 162 | + ''' |
| 163 | + text = [] |
| 164 | + defaults = self.config.initial_defs.copy() |
| 165 | + for setting in self.config.data.get_settings(): |
| 166 | + if setting.name in defaults: |
| 167 | + defaults[setting.name] = setting |
| 168 | + |
| 169 | + for setting in sorted(defaults): |
| 170 | + if isinstance(defaults[setting], Setting): |
| 171 | + if defaults[setting].origin == 'default': |
| 172 | + color = 'green' |
| 173 | + else: |
| 174 | + color = 'yellow' |
| 175 | + msg = "%s(%s) = %s" % (setting, defaults[setting].origin, defaults[setting].value) |
| 176 | + else: |
| 177 | + color = 'green' |
| 178 | + msg = "%s(%s) = %s" % (setting, 'default', defaults[setting].get('default')) |
| 179 | + text.append(stringc(msg, color)) |
| 180 | + |
| 181 | + self.pager(to_text('\n'.join(text), errors='surrogate_or_strict')) |
0 commit comments