2017-02-16 15:53:03 +00:00
|
|
|
#!/usr/bin/python
|
2017-08-04 09:25:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2017, Ansible by Red Hat, inc
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
2017-08-16 04:10:36 +00:00
|
|
|
'supported_by': 'network'}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: junos_user
|
|
|
|
version_added: "2.3"
|
|
|
|
author: "Peter Sprygada (@privateip)"
|
2017-04-13 16:14:33 +00:00
|
|
|
short_description: Manage local user accounts on Juniper JUNOS devices
|
2017-02-16 15:53:03 +00:00
|
|
|
description:
|
|
|
|
- This module manages locally configured user accounts on remote
|
|
|
|
network devices running the JUNOS operating system. It provides
|
|
|
|
a set of arguments for creating, removing and updating locally
|
|
|
|
defined accounts
|
2017-03-05 08:37:01 +00:00
|
|
|
extends_documentation_fragment: junos
|
2017-02-16 15:53:03 +00:00
|
|
|
options:
|
2017-07-26 14:09:17 +00:00
|
|
|
aggregate:
|
2017-02-16 15:53:03 +00:00
|
|
|
description:
|
2017-07-26 14:09:17 +00:00
|
|
|
- The C(aggregate) argument defines a list of users to be configured
|
2017-02-16 15:53:03 +00:00
|
|
|
on the remote device. The list of users will be compared against
|
|
|
|
the current users and only changes will be added or removed from
|
|
|
|
the device configuration. This argument is mutually exclusive with
|
2018-01-16 12:18:51 +00:00
|
|
|
the name argument.
|
2017-07-26 14:09:17 +00:00
|
|
|
version_added: "2.4"
|
2018-01-16 12:18:51 +00:00
|
|
|
aliases: ['users', 'collection']
|
2017-02-16 15:53:03 +00:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The C(name) argument defines the username of the user to be created
|
|
|
|
on the system. This argument must follow appropriate usernaming
|
|
|
|
conventions for the target device running JUNOS. This argument is
|
2017-07-26 14:09:17 +00:00
|
|
|
mutually exclusive with the C(aggregate) argument.
|
2017-02-16 15:53:03 +00:00
|
|
|
full_name:
|
|
|
|
description:
|
|
|
|
- The C(full_name) argument provides the full name of the user
|
|
|
|
account to be created on the remote device. This argument accepts
|
|
|
|
any text string value.
|
|
|
|
role:
|
|
|
|
description:
|
|
|
|
- The C(role) argument defines the role of the user account on the
|
|
|
|
remote system. User accounts can have more than one role
|
|
|
|
configured.
|
|
|
|
choices: ['operator', 'read-only', 'super-user', 'unauthorized']
|
|
|
|
sshkey:
|
|
|
|
description:
|
|
|
|
- The C(sshkey) argument defines the public SSH key to be configured
|
|
|
|
for the user account on the remote system. This argument must
|
|
|
|
be a valid SSH key
|
2018-12-04 15:49:13 +00:00
|
|
|
encrypted_password:
|
|
|
|
description:
|
|
|
|
- The C(encrypted_password) argument set already hashed password
|
|
|
|
for the user account on the remote system.
|
|
|
|
version_added: "2.8"
|
2017-02-16 15:53:03 +00:00
|
|
|
purge:
|
|
|
|
description:
|
|
|
|
- The C(purge) argument instructs the module to consider the
|
|
|
|
users definition absolute. It will remove any previously configured
|
|
|
|
users on the device with the exception of the current defined
|
2017-08-31 12:16:13 +00:00
|
|
|
set of aggregate.
|
2018-03-15 21:15:24 +00:00
|
|
|
type: bool
|
|
|
|
default: 'no'
|
2017-02-16 15:53:03 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The C(state) argument configures the state of the user definitions
|
|
|
|
as it relates to the device operational configuration. When set
|
|
|
|
to I(present), the user should be configured in the device active
|
|
|
|
configuration and when set to I(absent) the user should not be
|
|
|
|
in the device active configuration
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
2017-07-06 16:27:12 +00:00
|
|
|
active:
|
|
|
|
description:
|
|
|
|
- Specifies whether or not the configuration is active or deactivated
|
2018-03-15 21:15:24 +00:00
|
|
|
type: bool
|
|
|
|
default: 'yes'
|
2017-07-06 16:27:12 +00:00
|
|
|
version_added: "2.4"
|
2017-06-22 04:04:50 +00:00
|
|
|
requirements:
|
|
|
|
- ncclient (>=v0.5.2)
|
|
|
|
notes:
|
|
|
|
- This module requires the netconf system service be enabled on
|
2017-08-14 04:47:03 +00:00
|
|
|
the remote device being managed.
|
2017-08-24 14:31:47 +00:00
|
|
|
- Tested against vSRX JUNOS version 15.1X49-D15.4, vqfx-10000 JUNOS Version 15.1X53-D60.4.
|
2018-04-17 04:57:29 +00:00
|
|
|
- Recommended connection is C(netconf). See L(the Junos OS Platform Options,../network/user_guide/platform_junos.html).
|
|
|
|
- This module also works with C(local) connections for legacy playbooks.
|
2017-02-16 15:53:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: create new user account
|
|
|
|
junos_user:
|
|
|
|
name: ansible
|
|
|
|
role: super-user
|
|
|
|
sshkey: "{{ lookup('file', '~/.ssh/ansible.pub') }}"
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: remove a user account
|
|
|
|
junos_user:
|
|
|
|
name: ansible
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: remove all user accounts except ansible
|
|
|
|
junos_user:
|
2017-08-31 12:16:13 +00:00
|
|
|
aggregate:
|
|
|
|
- name: ansible
|
2017-02-16 15:53:03 +00:00
|
|
|
purge: yes
|
2017-08-04 09:25:58 +00:00
|
|
|
|
2018-12-04 15:49:13 +00:00
|
|
|
- name: set user password
|
|
|
|
junos_user:
|
|
|
|
name: ansible
|
|
|
|
role: super-user
|
|
|
|
encrypted_password: "{{ 'my-password' | password_hash('sha512') }}"
|
|
|
|
state: present
|
|
|
|
|
2017-08-04 09:25:58 +00:00
|
|
|
- name: Create list of users
|
|
|
|
junos_user:
|
|
|
|
aggregate:
|
|
|
|
- {name: test_user1, full_name: test_user2, role: operator, state: present}
|
|
|
|
- {name: test_user2, full_name: test_user2, role: read-only, state: present}
|
|
|
|
|
|
|
|
- name: Delete list of users
|
|
|
|
junos_user:
|
|
|
|
aggregate:
|
|
|
|
- {name: test_user1, full_name: test_user2, role: operator, state: absent}
|
|
|
|
- {name: test_user2, full_name: test_user2, role: read-only, state: absent}
|
2017-02-16 15:53:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
2017-07-11 04:22:53 +00:00
|
|
|
diff.prepared:
|
2017-07-04 16:19:26 +00:00
|
|
|
description: Configuration difference before and after applying change.
|
2017-07-11 04:22:53 +00:00
|
|
|
returned: when configuration is changed and diff option is enabled.
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2017-07-04 16:19:26 +00:00
|
|
|
sample: >
|
|
|
|
[edit system login]
|
|
|
|
+ user test-user {
|
|
|
|
+ uid 2005;
|
|
|
|
+ class read-only;
|
|
|
|
+ }
|
2017-02-16 15:53:03 +00:00
|
|
|
"""
|
|
|
|
from functools import partial
|
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2018-07-30 04:54:58 +00:00
|
|
|
from ansible.module_utils._text import to_text
|
2017-02-16 15:53:03 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-07-30 04:54:58 +00:00
|
|
|
from ansible.module_utils.connection import ConnectionError
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.common.utils import remove_default_spec
|
2018-06-22 15:50:44 +00:00
|
|
|
from ansible.module_utils.network.junos.junos import junos_argument_spec, get_connection, tostring
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.junos.junos import commit_configuration, discard_changes
|
|
|
|
from ansible.module_utils.network.junos.junos import load_config, locked_config
|
2017-02-16 15:53:03 +00:00
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
|
2017-06-22 04:04:50 +00:00
|
|
|
try:
|
2018-06-22 15:50:44 +00:00
|
|
|
from lxml.etree import Element, SubElement
|
2017-06-22 04:04:50 +00:00
|
|
|
except ImportError:
|
2018-06-22 15:50:44 +00:00
|
|
|
from xml.etree.ElementTree import Element, SubElement
|
2017-06-22 04:04:50 +00:00
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
ROLES = ['operator', 'read-only', 'super-user', 'unauthorized']
|
2017-05-03 13:30:07 +00:00
|
|
|
USE_PERSISTENT_CONNECTION = True
|
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-08-31 12:16:13 +00:00
|
|
|
def handle_purge(module, want):
|
|
|
|
want_users = [item['name'] for item in want]
|
2017-05-03 13:30:07 +00:00
|
|
|
element = Element('system')
|
2017-08-31 12:16:13 +00:00
|
|
|
login = SubElement(element, 'login')
|
|
|
|
|
2017-11-24 06:34:47 +00:00
|
|
|
conn = get_connection(module)
|
2018-07-30 04:54:58 +00:00
|
|
|
try:
|
|
|
|
reply = conn.execute_rpc(tostring(Element('get-configuration')), ignore_warning=False)
|
|
|
|
except ConnectionError as exc:
|
|
|
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
|
|
|
|
2017-08-31 12:16:13 +00:00
|
|
|
users = reply.xpath('configuration/system/login/user/name')
|
|
|
|
if users:
|
|
|
|
for item in users:
|
|
|
|
name = item.text
|
|
|
|
if name not in want_users and name != 'root':
|
|
|
|
user = SubElement(login, 'user', {'operation': 'delete'})
|
|
|
|
SubElement(user, 'name').text = name
|
|
|
|
if element.xpath('/system/login/user/name'):
|
|
|
|
return element
|
|
|
|
|
|
|
|
|
|
|
|
def map_obj_to_ele(module, want):
|
|
|
|
element = Element('system')
|
|
|
|
login = SubElement(element, 'login')
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
for item in want:
|
|
|
|
if item['state'] != 'present':
|
2017-08-31 12:16:13 +00:00
|
|
|
if item['name'] == 'root':
|
|
|
|
module.fail_json(msg="cannot delete the 'root' account.")
|
2017-02-16 15:53:03 +00:00
|
|
|
operation = 'delete'
|
|
|
|
else:
|
2017-08-31 12:16:13 +00:00
|
|
|
operation = 'merge'
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-05-03 13:30:07 +00:00
|
|
|
user = SubElement(login, 'user', {'operation': operation})
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-05-03 13:30:07 +00:00
|
|
|
SubElement(user, 'name').text = item['name']
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-08-31 12:16:13 +00:00
|
|
|
if operation == 'merge':
|
2017-07-06 16:27:12 +00:00
|
|
|
if item['active']:
|
|
|
|
user.set('active', 'active')
|
|
|
|
else:
|
|
|
|
user.set('inactive', 'inactive')
|
|
|
|
|
2017-08-31 12:16:13 +00:00
|
|
|
if item['role']:
|
|
|
|
SubElement(user, 'class').text = item['role']
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
if item.get('full_name'):
|
2017-05-03 13:30:07 +00:00
|
|
|
SubElement(user, 'full-name').text = item['full_name']
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
if item.get('sshkey'):
|
2017-05-03 13:30:07 +00:00
|
|
|
auth = SubElement(user, 'authentication')
|
2018-08-06 04:50:27 +00:00
|
|
|
if 'ssh-rsa' in item['sshkey']:
|
|
|
|
ssh_rsa = SubElement(auth, 'ssh-rsa')
|
|
|
|
elif 'ssh-dss' in item['sshkey']:
|
|
|
|
ssh_rsa = SubElement(auth, 'ssh-dsa')
|
|
|
|
elif 'ecdsa-sha2' in item['sshkey']:
|
|
|
|
ssh_rsa = SubElement(auth, 'ssh-ecdsa')
|
|
|
|
elif 'ssh-ed25519' in item['sshkey']:
|
|
|
|
ssh_rsa = SubElement(auth, 'ssh-ed25519')
|
2017-05-03 13:30:07 +00:00
|
|
|
key = SubElement(ssh_rsa, 'name').text = item['sshkey']
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2018-12-04 15:49:13 +00:00
|
|
|
if item.get('encrypted_password'):
|
|
|
|
if 'auth' not in locals():
|
|
|
|
auth = SubElement(user, 'authentication')
|
|
|
|
SubElement(auth, 'encrypted-password').text = item['encrypted_password']
|
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
return element
|
|
|
|
|
2017-05-03 13:30:07 +00:00
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
def get_param_value(key, item, module):
|
|
|
|
# if key doesn't exist in the item, get it from module.params
|
|
|
|
if not item.get(key):
|
|
|
|
value = module.params[key]
|
|
|
|
|
|
|
|
# if key does exist, do a type check on it to validate it
|
|
|
|
else:
|
|
|
|
value_type = module.argument_spec[key].get('type', 'str')
|
|
|
|
type_checker = module._CHECK_ARGUMENT_TYPES_DISPATCHER[value_type]
|
|
|
|
type_checker(item[key])
|
|
|
|
value = item[key]
|
|
|
|
|
|
|
|
# validate the param value (if validator func exists)
|
|
|
|
validator = globals().get('validate_%s' % key)
|
|
|
|
if all((value, validator)):
|
|
|
|
validator(value, module)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
2017-05-03 13:30:07 +00:00
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
def map_params_to_obj(module):
|
2017-07-26 14:09:17 +00:00
|
|
|
aggregate = module.params['aggregate']
|
|
|
|
if not aggregate:
|
2017-02-16 15:53:03 +00:00
|
|
|
if not module.params['name'] and module.params['purge']:
|
|
|
|
return list()
|
2017-08-18 04:20:35 +00:00
|
|
|
elif not module.params['name']:
|
|
|
|
module.fail_json(msg='missing required argument: name')
|
2017-02-16 15:53:03 +00:00
|
|
|
else:
|
|
|
|
collection = [{'name': module.params['name']}]
|
|
|
|
else:
|
|
|
|
collection = list()
|
2017-07-26 14:09:17 +00:00
|
|
|
for item in aggregate:
|
2017-02-16 15:53:03 +00:00
|
|
|
if not isinstance(item, dict):
|
|
|
|
collection.append({'username': item})
|
2017-08-18 04:20:35 +00:00
|
|
|
elif 'name' not in item:
|
|
|
|
module.fail_json(msg='missing required argument: name')
|
2017-02-16 15:53:03 +00:00
|
|
|
else:
|
|
|
|
collection.append(item)
|
|
|
|
|
|
|
|
objects = list()
|
|
|
|
|
|
|
|
for item in collection:
|
|
|
|
get_value = partial(get_param_value, item=item, module=module)
|
|
|
|
item.update({
|
|
|
|
'full_name': get_value('full_name'),
|
|
|
|
'role': get_value('role'),
|
2018-12-04 15:49:13 +00:00
|
|
|
'encrypted_password': get_value('encrypted_password'),
|
2017-02-16 15:53:03 +00:00
|
|
|
'sshkey': get_value('sshkey'),
|
2017-07-06 16:27:12 +00:00
|
|
|
'state': get_value('state'),
|
|
|
|
'active': get_value('active')
|
2017-02-16 15:53:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for key, value in iteritems(item):
|
|
|
|
# validate the param value (if validator func exists)
|
|
|
|
validator = globals().get('validate_%s' % key)
|
|
|
|
if all((value, validator)):
|
|
|
|
validator(value, module)
|
|
|
|
|
|
|
|
objects.append(item)
|
|
|
|
|
|
|
|
return objects
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" main entry point for module execution
|
|
|
|
"""
|
2017-08-04 09:25:58 +00:00
|
|
|
element_spec = dict(
|
2017-02-16 15:53:03 +00:00
|
|
|
name=dict(),
|
|
|
|
full_name=dict(),
|
2017-08-31 12:16:13 +00:00
|
|
|
role=dict(choices=ROLES),
|
2018-12-04 15:49:13 +00:00
|
|
|
encrypted_password=dict(),
|
2017-02-16 15:53:03 +00:00
|
|
|
sshkey=dict(),
|
2017-07-06 16:27:12 +00:00
|
|
|
state=dict(choices=['present', 'absent'], default='present'),
|
2017-08-14 04:47:03 +00:00
|
|
|
active=dict(type='bool', default=True)
|
2017-02-16 15:53:03 +00:00
|
|
|
)
|
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
aggregate_spec = deepcopy(element_spec)
|
|
|
|
aggregate_spec['name'] = dict(required=True)
|
|
|
|
|
|
|
|
# remove default in aggregate spec, to handle common arguments
|
|
|
|
remove_default_spec(aggregate_spec)
|
|
|
|
|
2017-08-04 09:25:58 +00:00
|
|
|
argument_spec = dict(
|
2017-08-14 04:47:03 +00:00
|
|
|
aggregate=dict(type='list', elements='dict', options=aggregate_spec, aliases=['collection', 'users']),
|
2017-08-04 09:25:58 +00:00
|
|
|
purge=dict(default=False, type='bool')
|
|
|
|
)
|
|
|
|
|
|
|
|
argument_spec.update(element_spec)
|
|
|
|
argument_spec.update(junos_argument_spec)
|
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
mutually_exclusive = [['aggregate', 'name']]
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
mutually_exclusive=mutually_exclusive,
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
2017-05-03 13:30:07 +00:00
|
|
|
warnings = list()
|
|
|
|
result = {'changed': False, 'warnings': warnings}
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
want = map_params_to_obj(module)
|
2017-08-31 12:16:13 +00:00
|
|
|
ele = map_obj_to_ele(module, want)
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-08-31 12:16:13 +00:00
|
|
|
purge_request = None
|
2017-02-16 15:53:03 +00:00
|
|
|
if module.params['purge']:
|
2017-08-31 12:16:13 +00:00
|
|
|
purge_request = handle_purge(module, want)
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-07-11 04:22:53 +00:00
|
|
|
with locked_config(module):
|
2017-08-31 12:16:13 +00:00
|
|
|
if purge_request:
|
|
|
|
load_config(module, tostring(purge_request), warnings, action='replace')
|
|
|
|
diff = load_config(module, tostring(ele), warnings, action='merge')
|
2017-02-16 15:53:03 +00:00
|
|
|
|
2017-07-11 04:22:53 +00:00
|
|
|
commit = not module.check_mode
|
|
|
|
if diff:
|
|
|
|
if commit:
|
|
|
|
commit_configuration(module)
|
|
|
|
else:
|
|
|
|
discard_changes(module)
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module._diff:
|
|
|
|
result['diff'] = {'prepared': diff}
|
2017-02-16 15:53:03 +00:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2018-07-29 11:46:06 +00:00
|
|
|
|
2017-02-16 15:53:03 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|