2023-04-14 10:31:09 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: custom_command
|
|
|
|
short_description: Adds or removes a user with custom commands by ssh
|
|
|
|
description:
|
|
|
|
- You can add or edit users using ssh with custom commands.
|
|
|
|
|
|
|
|
options:
|
|
|
|
protocol:
|
|
|
|
default: ssh
|
|
|
|
choices: [ssh]
|
|
|
|
description:
|
|
|
|
- C(ssh) The remote asset is connected using ssh.
|
|
|
|
type: str
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the user to add or remove.
|
|
|
|
required: true
|
|
|
|
aliases: [user]
|
|
|
|
type: str
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- The password to use for the user.
|
|
|
|
type: str
|
|
|
|
aliases: [pass]
|
|
|
|
commands:
|
|
|
|
description:
|
|
|
|
- Custom change password commands.
|
|
|
|
type: list
|
|
|
|
required: true
|
|
|
|
first_conn_delay_time:
|
|
|
|
description:
|
|
|
|
- Delay for executing the command after SSH connection(unit: s)
|
|
|
|
type: float
|
|
|
|
required: false
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create user with name 'jms' and password '123456'.
|
|
|
|
custom_command:
|
|
|
|
login_host: "localhost"
|
|
|
|
login_port: 22
|
|
|
|
login_user: "admin"
|
|
|
|
login_password: "123456"
|
|
|
|
name: "jms"
|
|
|
|
password: "123456"
|
|
|
|
commands: ['passwd {username}', '{password}', '{password}']
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
name:
|
|
|
|
description: The name of the user to add.
|
|
|
|
returned: success
|
|
|
|
type: str
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
from ops.ansible.modules_utils.custom_common import (
|
2023-06-07 09:28:35 +00:00
|
|
|
SSHClient, common_argument_spec
|
2023-04-14 10:31:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_commands(module):
|
|
|
|
username = module.params['name']
|
|
|
|
password = module.params['password']
|
|
|
|
commands = module.params['commands'] or []
|
2023-04-19 06:44:06 +00:00
|
|
|
login_password = module.params['login_password']
|
2023-04-14 10:31:09 +00:00
|
|
|
for index, command in enumerate(commands):
|
|
|
|
commands[index] = command.format(
|
2023-04-19 06:44:06 +00:00
|
|
|
username=username, password=password, login_password=login_password
|
2023-04-14 10:31:09 +00:00
|
|
|
)
|
|
|
|
return commands
|
|
|
|
|
|
|
|
# =========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-06-07 09:28:35 +00:00
|
|
|
argument_spec = common_argument_spec()
|
2023-04-14 10:31:09 +00:00
|
|
|
argument_spec.update(
|
|
|
|
name=dict(required=True, aliases=['user']),
|
|
|
|
password=dict(aliases=['pass'], no_log=True),
|
|
|
|
commands=dict(type='list', required=False),
|
|
|
|
first_conn_delay_time=dict(
|
|
|
|
type='float', required=False, default=0.5
|
|
|
|
),
|
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
|
|
|
ssh_client = SSHClient(module)
|
|
|
|
commands = get_commands(module)
|
|
|
|
if not commands:
|
|
|
|
module.fail_json(
|
|
|
|
msg='No command found, please go to the platform details to add'
|
|
|
|
)
|
|
|
|
err = ssh_client.execute(commands)
|
|
|
|
if err:
|
|
|
|
module.fail_json(
|
|
|
|
msg='There was a problem executing the command: %s' % err
|
|
|
|
)
|
|
|
|
|
|
|
|
user = module.params['name']
|
|
|
|
module.exit_json(changed=True, user=user)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|