Example #1
0
def main():
  module = AnsibleModule(
    argument_spec = dict(
      hostname  = dict(required=True),
      username  = dict(required=True),
      password  = dict(required=True, no_log=True),
      settings  = dict(required=False, type='dict'),
      parameter = dict(
        required  = True,
        choices   = ['discovery'],
        type      = 'str'
      ),
      state   = dict(
        required  = False,
        choices   = ['present', 'absent'],
        type      = 'str'
      ),
    ),
    supports_check_mode=True
  )

  idempotent_parameter = None
  params = module.params
  idempotent_parameter = 'name'


  mt_obj = MikrotikIdempotent(
    hostname         = params['hostname'],
    username         = params['username'],
    password         = params['password'],
    state            = params['state'],
    desired_params   = params['settings'],
    idempotent_param = idempotent_parameter,
    api_path         = '/ip/neighbor/' + str(params['parameter']),
    check_mode       = module.check_mode
  )

  mt_obj.sync_state()

  if mt_obj.failed:
    module.fail_json(
      msg = mt_obj.failed_msg
    )
  elif mt_obj.changed:
    module.exit_json(
      failed=False,
      changed=True,
      msg=mt_obj.changed_msg,
      diff={ "prepared": {
        "old": mt_obj.old_params,
        "new": mt_obj.new_params,
      }},
    )
  else:
    module.exit_json(
      failed=False,
      changed=False,
      #msg='',
      msg=params['settings'],
    )
Example #2
0
def main():
    module = AnsibleModule(argument_spec=dict(
        hostname=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        settings=dict(required=False, type='dict'),
        parameter=dict(required=True,
                       choices=[
                           'ntp_client', 'clock', 'identity', 'logging',
                           'routerboard_settings'
                       ],
                       type='str'),
        state=dict(required=False, choices=['present', 'absent'], type='str'),
    ),
                           supports_check_mode=True)

    params = module.params

    if params['parameter'] == 'routerboard_settings':
        params['parameter'] = 'routerboard/settings'

    if params['parameter'] == 'ntp_client':
        params['parameter'] = 'ntp/client'

    clean_params(params['settings'])
    mt_obj = MikrotikIdempotent(hostname=params['hostname'],
                                username=params['username'],
                                password=params['password'],
                                state=params['state'],
                                desired_params=params['settings'],
                                idempotent_param=None,
                                api_path='/system/' + params['parameter'],
                                check_mode=module.check_mode)

    mt_obj.sync_state()

    if mt_obj.failed:
        module.fail_json(msg=mt_obj.failed_msg)
    elif mt_obj.changed:
        module.exit_json(
            failed=False,
            changed=True,
            msg=mt_obj.changed_msg,
            diff={
                "prepared": {
                    "old": mt_obj.old_params,
                    "new": mt_obj.new_params,
                }
            },
        )
    else:
        module.exit_json(
            failed=False,
            changed=False,
            #msg='',
            msg=params['settings'],
        )
def main():

    module = AnsibleModule(argument_spec=dict(
        hostname=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        settings=dict(required=True, type='dict'),
        idempotent=dict(required=False,
                        default='address',
                        choices=['address', 'interface'],
                        type='str'),
        state=dict(required=False,
                   default="present",
                   choices=['present', 'absent'],
                   type='str'),
    ),
                           supports_check_mode=True)

    params = module.params
    mt_obj = MikrotikIdempotent(hostname=params['hostname'],
                                username=params['username'],
                                password=params['password'],
                                state=params['state'],
                                desired_params=params['settings'],
                                idempotent_param=params['idempotent'],
                                api_path='/ip/address',
                                check_mode=module.check_mode)

    # exit if login failed
    if not mt_obj.login_success:
        module.fail_json(msg=mt_obj.failed_msg)

    # add, remove or edit things
    mt_obj.sync_state()

    if mt_obj.failed:
        module.fail_json(msg=mt_obj.failed_msg)
    elif mt_obj.changed:
        module.exit_json(
            failed=False,
            changed=True,
            msg=mt_obj.changed_msg,
            diff={
                "prepared": {
                    "old": mt_obj.old_params,
                    "new": mt_obj.new_params,
                }
            },
        )
    else:
        module.exit_json(
            failed=False,
            changed=False,
            #msg='',
            msg=params['settings'],
        )
Example #4
0
def main():
    module = AnsibleModule(argument_spec=dict(
        hostname=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        settings=dict(required=True, type='dict'),
        parameter=dict(required=True,
                       choices=[
                           'ethernet', 'vlan', 'ovpn-client', 'bridge',
                           'bridge port', 'bridge settings'
                       ],
                       type='str'),
        state=dict(required=False, choices=['present', 'absent'], type='str')),
                           supports_check_mode=True)

    params = module.params
    if params['parameter'] == 'bridge port':
        params['parameter'] = 'bridge/port'
        idempotent_parameter = "interface"
    elif params['parameter'] == 'bridge settings':
        params['parameter'] = 'bridge/settings'
        idempotent_parameter = None
    else:
        idempotent_parameter = 'name'

    mt_obj = MikrotikIdempotent(hostname=params['hostname'],
                                username=params['username'],
                                password=params['password'],
                                state=params['state'],
                                desired_params=params['settings'],
                                idempotent_param=idempotent_parameter,
                                api_path='/interface/' +
                                str(params['parameter']),
                                check_mode=module.check_mode)

    # exit if login failed
    if not mt_obj.login_success:
        module.fail_json(msg=mt_obj.failed_msg)

    # add, remove or edit things
    mt_obj.sync_state()

    if mt_obj.failed:
        module.fail_json(msg=mt_obj.failed_msg)
    elif mt_obj.changed:
        module.exit_json(
            failed=False,
            changed=True,
            msg=mt_obj.changed_msg,
            diff={
                "prepared": {
                    "old": mt_obj.old_params,
                    "new": mt_obj.new_params,
                }
            },
        )
    else:
        module.exit_json(
            failed=False,
            changed=False,
            #msg='',
            msg=params['settings'],
        )