Prakash P
Prakash P

Reputation: 4056

Building custom module Ansible

I am trying to build a custom module for our private cloud Infrastructure.

I followed this doc http://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html

I created a my_module.py module file.

When I hit ansible-playbook playbook/my_module.yml

Response:

PLAY [Create, Update, Delete VM] *********************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [localhost]

TASK [VM create] *************************************************************************************************************************
changed: [localhost]

TASK [dump test output] ***********************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "failed": false,
        "message": "goodbye",
        "original_message": "pcp_vm_ansible"
    }
}

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0  

Which means it is working fine as expected.

Module.py

from ansible.module_utils.basic import AnsibleModule

def run_module():

    module_args = dict(
        name=dict(type='str', required=True),
        new=dict(type='bool', 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
    result['original_message'] = module.params['name']
    result['message'] = 'goodbye'
    if module.params['new']:
        result['changed'] = True
    if module.params['name'] == 'fail me':
        module.fail_json(msg='You requested this to fail', **result)


    module.exit_json(**result)

def main():
    print("================== Main Called =======================") 
    run_module()

if __name__ == '__main__':
    main()

I am trying to print logs to visualize my input data using print() or even logging.

print("================== Main Called =======================")

But nothing is getting printed to console.

Upvotes: 1

Views: 615

Answers (1)

Adam Miller
Adam Miller

Reputation: 927

As per Conventions, Best Practices, and Pitfalls, "Modules must output valid JSON only. The top level return type must be a hash (dictionary) although they can be nested. Lists or simple scalar values are not supported, though they can be trivially contained inside a dictionary."

Effectively, the core runtime only communicates with the module via JSON and the core runtime controls stdout so the standard print statements from the module are suppressed. If you want or need more information out of the execution runtime then I suggest a Callback Plugin.

Upvotes: 2

Related Questions