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
This commit is contained in:
Nathaniel Case 2016-03-24 15:49:03 -04:00
parent eab4ce19f3
commit e4e913b331
3 changed files with 27 additions and 5 deletions

View file

@ -1387,6 +1387,8 @@ class AnsibleModule(object):
wanted = 'str' wanted = 'str'
value = self.params[k] value = self.params[k]
if value is None:
continue
try: try:
type_checker = self._CHECK_ARGUMENT_TYPES_DISPATCHER[wanted] type_checker = self._CHECK_ARGUMENT_TYPES_DISPATCHER[wanted]

View file

@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
import os
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I) NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I)
@ -24,6 +25,7 @@ NET_COMMON_ARGS = dict(
port=dict(type='int'), port=dict(type='int'),
username=dict(required=True), username=dict(required=True),
password=dict(no_log=True), password=dict(no_log=True),
ssh_keyfile=dict(type='path'),
authorize=dict(default=False, type='bool'), authorize=dict(default=False, type='bool'),
auth_pass=dict(no_log=True), auth_pass=dict(no_log=True),
transport=dict(default='cli', choices=['cli', 'eapi']), transport=dict(default='cli', choices=['cli', 'eapi']),
@ -31,6 +33,14 @@ NET_COMMON_ARGS = dict(
provider=dict(type='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 = [ CLI_PROMPTS_RE = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$") re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
@ -139,12 +149,13 @@ class Cli(object):
username = self.module.params['username'] username = self.module.params['username']
password = self.module.params['password'] password = self.module.params['password']
key_filename = self.module.params['ssh_keyfile']
try: try:
self.shell = Shell(CLI_PROMPTS_RE, CLI_ERRORS_RE) 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: 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) self.module.fail_json(msg=msg)
def authorize(self): def authorize(self):
@ -177,9 +188,12 @@ class NetworkModule(AnsibleModule):
params = super(NetworkModule, self)._load_params() params = super(NetworkModule, self)._load_params()
provider = params.get('provider') or dict() provider = params.get('provider') or dict()
for key, value in provider.items(): for key, value in provider.items():
if key in NET_COMMON_ARGS.keys(): if key in NET_COMMON_ARGS:
if not params.get(key) and value is not None: if params.get(key) is None and value is not None:
params[key] = value 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 return params
def connect(self): def connect(self):

View file

@ -45,11 +45,17 @@ options:
required: true required: true
password: password:
description: 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) the remote device. This is a common argument used for either I(cli)
or I(eapi) transports. or I(eapi) transports.
required: false required: false
default: null 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: authorize:
description: description:
- Instructs the module to enter priviledged mode on the remote device - Instructs the module to enter priviledged mode on the remote device