-2

I have an Ansible Custom Module for performing a specific task in my playbook. I want to debug specific variables inside this module.

Is there a way we can print anything inside this custom module? In the example below, print "Hello".

Please check the following snippet from the Custom Module. I am passing a jobid as an argument to this module.

class dcsjob():
  def __init__(self, arglist):
    self.jobid = self.arglist[0]

  def checkandwaitforjob(self):
      print("Hello")

def run_module():
  module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
  )
  dcsjobobj = dcsjob([module.params['jobid']])
  output = dcsjobobj.checkandwaitforjob()
U880D
  • 8,601
  • 6
  • 24
  • 40
Akash-BLR
  • 63
  • 4
  • Sure. Exactly that use case is described in the example of [Developing modules- Creating a module](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#creating-a-module). – U880D Feb 13 '23 at 14:34
  • I had checked this. Can you share an example with a simple print message ? – Akash-BLR Feb 13 '23 at 15:15
  • "_I had checked this._" what does that mean? Are there any questions regarding the there given example? Since according "_I have an Ansible Custom Module ... I want to debug specific variables inside this module._" you have already an example module, can you share it and show on which variables you are interested in? – U880D Feb 13 '23 at 15:20
  • You may also be interested in [Debugging modules - Simple debugging](https://docs.ansible.com/ansible/latest/dev_guide/debugging.html#simple-debugging). – U880D Feb 13 '23 at 15:47
  • I have edited the question above with my custom module snippet. Please check and suggest. – Akash-BLR Feb 13 '23 at 15:55

2 Answers2

2

You can use the Display class for this:

from ansible.utils.display import Display

display = Display()

class dcsjob():
  def __init__(self, arglist):
    self.jobid = self.arglist[0]

  def checkandwaitforjob(self):
      display.v("verbosity 1")
      display.vv("verbosity 2")
      display.vvv("verbosity 3")
      display.warning("error")
      display.error("warning")

def run_module():
  module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
  )
  dcsjobobj = dcsjob([module.params['jobid']])
  output = dcsjobobj.checkandwaitforjob()

This will format and color code the messages the same way Ansible does it.

Verbose output will be hidden per default and you can increase verbosity like this: ansible-playbook --inventory inventory playbook.yml -vvv

Bastian
  • 48
  • 1
  • 5
0

Given an example module from Developing modules- Creating a module with modification

library/print.py

#!/usr/bin/python

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule

def run_module():

    module_args = dict(
        jobid=dict(type='str', required=True),
        verbose=dict(type='bool', required=False, default=False)
    )

    result = dict(
        changed=False,
        jobid=''
    )

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

    result['jobid'] = module.params['jobid']

    if module.params['verbose']:
        result['print'] = 'Hello'

    module.exit_json(**result)

def main():
    run_module()

if __name__ == '__main__':
    main()

and called from an example playbook print.yml

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    ID: 123

  tasks:

  - name: Run non-verbose
    print:
      jobid: "{{ ID }}"
      verbose: false
    register: result

  - name: Show result
    debug:
      msg: "{{ result }}"

  - name: Run verbose
    print:
      jobid: "{{ ID }}"
      verbose: true
    register: result

  - name: Show verbose result
    debug:
      msg: "{{ result }}"

it will result into an output of

TASK [Run non-verbose] **********
ok: [localhost]

TASK [Show result] **************
ok: [localhost] =>
  msg:
    changed: false
    failed: false
    jobid: '123'

TASK [Run verbose] **************
ok: [localhost]

TASK [Show verbose result] ******
ok: [localhost] =>
  msg:
    changed: false
    failed: false
    jobid: '123'
    print: Hello

printing the jobid, and Hello only when in verbose mode.

This could also be utilized with check_mode

    module_args = dict(
        jobid=dict(type='str', required=True)
    )

    result = dict(
        changed=False,
        check_mode=False,
        jobid=''
    )

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

    result['jobid'] = module.params['jobid']

    if module.check_mode:
        result['check_mode'] = True
        result['print'] = 'Hello'

which would change the behavior if --check is applied or not.

Further Documentation

U880D
  • 8,601
  • 6
  • 24
  • 40