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'
value = self.params[k]
if value is None:
continue
try:
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
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
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):

View file

@ -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