From e4e913b33131aed03123dab9932aaa44f0183c09 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Thu, 24 Mar 2016 15:49:03 -0400 Subject: [PATCH] Override params from environment variables, if set. Fix a typo while I'm in the area. Handle having None set in module.params more intelligently --- lib/ansible/module_utils/basic.py | 2 ++ lib/ansible/module_utils/eos.py | 22 +++++++++++++++---- .../utils/module_docs_fragments/eos.py | 8 ++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index e57b1943aab..f45973012bf 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1387,6 +1387,8 @@ class AnsibleModule(object): wanted = 'str' value = self.params[k] + if value is None: + continue try: type_checker = self._CHECK_ARGUMENT_TYPES_DISPATCHER[wanted] diff --git a/lib/ansible/module_utils/eos.py b/lib/ansible/module_utils/eos.py index cb1e4f41273..5167d1abc03 100644 --- a/lib/ansible/module_utils/eos.py +++ b/lib/ansible/module_utils/eos.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # +import os NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I) @@ -24,6 +25,7 @@ NET_COMMON_ARGS = dict( port=dict(type='int'), username=dict(required=True), password=dict(no_log=True), + ssh_keyfile=dict(type='path'), authorize=dict(default=False, type='bool'), auth_pass=dict(no_log=True), transport=dict(default='cli', choices=['cli', 'eapi']), @@ -31,6 +33,14 @@ NET_COMMON_ARGS = dict( provider=dict(type='dict') ) +NET_ENV_ARGS = dict( + username='ANSIBLE_NET_USERNAME', + password='ANSIBLE_NET_PASSWORD', + ssh_keyfile='ANSIBLE_NET_SSH_KEYFILE', + authorize='ANSIBLE_NET_AUTHORIZE', + auth_pass='ANSIBLE_NET_AUTH_PASS', +) + CLI_PROMPTS_RE = [ re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$") @@ -139,12 +149,13 @@ class Cli(object): username = self.module.params['username'] password = self.module.params['password'] + key_filename = self.module.params['ssh_keyfile'] try: self.shell = Shell(CLI_PROMPTS_RE, CLI_ERRORS_RE) - self.shell.open(host, port=port, username=username, password=password) + self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename) except Exception, exc: - msg = 'failed to connecto to %s:%s - %s' % (host, port, str(exc)) + msg = 'failed to connect to %s:%s - %s' % (host, port, str(exc)) self.module.fail_json(msg=msg) def authorize(self): @@ -177,9 +188,12 @@ class NetworkModule(AnsibleModule): params = super(NetworkModule, self)._load_params() provider = params.get('provider') or dict() for key, value in provider.items(): - if key in NET_COMMON_ARGS.keys(): - if not params.get(key) and value is not None: + if key in NET_COMMON_ARGS: + if params.get(key) is None and value is not None: params[key] = value + for key, env_var in NET_ENV_ARGS.items(): + if params.get(key) is None and env_var in os.environ: + params[key] = os.environ[env_var] return params def connect(self): diff --git a/lib/ansible/utils/module_docs_fragments/eos.py b/lib/ansible/utils/module_docs_fragments/eos.py index 6502710707f..d54b3940db8 100644 --- a/lib/ansible/utils/module_docs_fragments/eos.py +++ b/lib/ansible/utils/module_docs_fragments/eos.py @@ -45,11 +45,17 @@ options: required: true password: description: - - Specifies the password to use when authentication the connection to + - Specifies the password to use to authenticate the connection to the remote device. This is a common argument used for either I(cli) or I(eapi) transports. required: false default: null + ssh_keyfile: + description: + - Specifies the SSH keyfile to use to authenticate the connection to + the remote device. This argument is only used for I(cli) transports. + required: false + default: null authorize: description: - Instructs the module to enter priviledged mode on the remote device