# Ansible Modules

**URL:** <https://forum.ansible.com/t/ansible-modules/29807>\
**Category:** Ansible Developer\
**Created:** [March 1, 2019, 7:21pm UTC](https://forum.ansible.com/t/ansible-modules/29807 "2019-03-01T19:21:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Zenobio](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Sebastian\_Zenobio](https://forum.ansible.com/u/Sebastian_Zenobio)\
**Post date:** [March 1, 2019, 7:21pm UTC](https://forum.ansible.com/t/ansible-modules/29807/1 "2019-03-01T19:21:57Z")

</div>

Hi guys!

I wrote a small code inside modules and all is good. The only problem that I have, is when I want to print some message on the tasks (informational).

For example:

if I have a for inside my code I want that on each loop print some like

print (still running loop")

I tried a lot of things but still don’t see any message.

This is my code: (PRINT XXX is where I want to print)

```auto

import requests
import json
import time
from ansible.module_utils.basic import AnsibleModule

def run_module():
    module_args = dict(
        url=dict(url='str', required=True),
        loop=dict(loop='int', required=False, default=False),
        loopTime=dict(loop='int', required=False, default=False))

    result = dict(changed=False,original_message='',message='')
    module = AnsibleModule(argument_spec=module_args,supports_check_mode=True)

    if module.check_mode:
        return result

    for i in range (1,int(module.params['loop'])):
        try:
            r = requests.get(module.params['url'])
            json_response = json.loads(r.text)

            if json_response['measurements'][0]['value'] == 1:
                 module.fail_json(changed=True, msg="Circuit open!!!")

```

```auto
                 PRINT XXXXXXX
        except Exception as e:
            module.fail_json(msg=e)

        PRINT XXXXX

```

```auto

        time.sleep(float(module.params['loopTime']))
    module.exit_json(changed=False, msg='Circuit is stable')

def main():
    run_module()

if __name__ == ' __main__':
    main()

```

```auto

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Alb0t](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Alb0t](https://forum.ansible.com/u/Alb0t)\
**Post date:** [March 1, 2019, 8:23pm UTC](https://forum.ansible.com/t/ansible-modules/29807/2 "2019-03-01T20:23:58Z")

</div>

If the process ‘running’ is external, it’s probably best to just use an until loop. That’s what I did with my module.

---

<div class="post-metadata">

**Author:** ![Alb0t](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Alb0t](https://forum.ansible.com/u/Alb0t)\
**Post date:** [March 1, 2019, 8:24pm UTC](https://forum.ansible.com/t/ansible-modules/29807/3 "2019-03-01T20:24:57Z")

</div>

By until loop, i mean have ansible just re-run the module until it gets success.  
like this;

- name: wait for aerospike migrations  
some\_module:  
some\_param: foo  
register: mymodule  
until: mymodule is succeeded  
delay: 60  
retries: 120

---

<div class="post-metadata">

**Author:** ![Andreas\_Harpfer](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andreas\_Harpfer](https://forum.ansible.com/u/Andreas_Harpfer)\
**Post date:** [March 2, 2019, 1:23pm UTC](https://forum.ansible.com/t/ansible-modules/29807/4 "2019-03-02T13:23:56Z")

</div>

> Hi guys!
> 
> I wrote a small code inside modules and all is good. The only problem that I have, is when I want to print some message on the tasks (informational).

[…]

In most cases print() in a module will not work as expected because  
the module code is copied to the remote host before it is executed.  
So usually the print does not happen on the same host where your  
playbook is running.

As already suggested asynchronous execution [1] might be the better  
approach here.

[1] [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_async.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html)
