# Small (tiny?) example module and service which use a httpapi connection?

**URL:** <https://forum.ansible.com/t/small-tiny-example-module-and-service-which-use-a-httpapi-connection/2868>\
**Category:** Get Help\
**Tags:** api, module\
**Created:** [December 12, 2023, 1:18pm UTC](https://forum.ansible.com/t/small-tiny-example-module-and-service-which-use-a-httpapi-connection/2868 "2023-12-12T13:18:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [December 12, 2023, 1:18pm UTC](https://forum.ansible.com/t/small-tiny-example-module-and-service-which-use-a-httpapi-connection/2868/1 "2023-12-12T13:18:52Z")

</div>

Does anybody have a very basic (demo-style) implementation of an Ansible _module_ and associated Python back-end service which use a `connection: httpapi`?

During trainings I’d like to be able to demonstrate a play with a task or two which speak `httpapi` to a backend, such as when Fortigate or VMware tasks are used. It should be as basic as possible.

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@atul\_mohan](https://forum.ansible.com/u/atul_mohan)\
**Post date:** [January 21, 2025, 6:50am UTC](https://forum.ansible.com/t/small-tiny-example-module-and-service-which-use-a-httpapi-connection/2868/2 "2025-01-21T06:50:44Z")

</div>

```auto
#!/usr/bin/python

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list

def run_module():
    module_args = dict(
        name=dict(type='str', required=True),
        state=dict(type='str', default='present', choices=['present', 'absent'])
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    name = module.params['name']
    state = module.params['state']

    result = dict(
        changed=False,
        original_message='',
        message=''
    )

    # This is where you would implement the logic to interact with your httpapi backend
    if module.check_mode:
        module.exit_json(**result)

    # Example of calling the service
    from ansible.module_utils.connection import Connection

    connection = Connection(module._socket_path)
    if state == 'present':
        response = connection.send_request('POST', '/api/v1/resources', {'name': name})
        if response['status'] == 'success':
            result['changed'] = True
            result['message'] = f"Resource {name} added"
    elif state == 'absent':
        response = connection.send_request('DELETE', f'/api/v1/resources/{name}')
        if response['status'] == 'success':
            result['changed'] = True
            result['message'] = f"Resource {name} removed"

    if 'error' in response:
        module.fail_json(msg=f"Error: {response['error']}")

    module.exit_json(**result)

def main():
    run_module()

if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

**Author:** ![L3D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/l3d/32/911_2.png) [@L3D](https://forum.ansible.com/u/L3D)\
**Post date:** [January 21, 2025, 10:42am UTC](https://forum.ansible.com/t/small-tiny-example-module-and-service-which-use-a-httpapi-connection/2868/3 "2025-01-21T10:42:46Z")

</div>

Hey @jpmens

Maybe the [ansible.builtin.uri](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html) module is, what you are looking for?

But be warned, the ansible.builtin.uri module do not support the ansible check mode.
