Пример #1
0
    def ovs_run_module():
        module = AnsibleModule(
            argument_spec=argument_spec,
            supports_check_mode=supports_check_mode,
        )
        module._ovs_vars = {}

        state = module.params['state']
        op = ops_with_defaults[state]

        if not HAS_OVS:
            module.fail_json(
                msg='Python Open vSwitch library is not installed')

        try:
            schema_path = '{}/{}'.format(ovs.dirs.PKGDATADIR, schema_file)
            remote = 'unix:{}/{}'.format(ovs.dirs.RUNDIR, ctl)
            schema = SchemaHelper(location=schema_path)
            op['register_interest'](schema)
            idl = Idl(remote, schema)

            try:
                wait_for_db_change(idl)
            except Exception as e:
                module.fail_json(msg=str(e), exception=traceback.format_exc())

            op['prepare'](module, idl)

            if not module.check_mode:
                for i in range(3):
                    txn = Transaction(idl)
                    op['build_txn'](module, idl, txn)
                    status = txn.commit_block()
                    if status == Transaction.SUCCESS:
                        break
                    elif status != Transaction.TRY_AGAIN:
                        break

                if status == Transaction.SUCCESS:
                    changed = True
                elif status == Transaction.UNCHANGED:
                    changed = False
                else:
                    msg = op['txn_failure_msg'](module)
                    module.fail_json(msg='{}: {}'.format(msg, status))
            else:
                changed = True

            module.exit_json(changed=changed)
        except Exception as e:
            module.fail_json(msg=str(e), exception=traceback.format_exc())
        finally:
            if 'idl' in locals():
                idl.close()