Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Can I print information during module execution?

Tags:

I would like to know if there is a way to print information while a module is executing -- primarily as a means to demonstrate that the process is working and has not hung. Specifically, I am trying to get feedback during the execution of the cloudformation module. I tried modifying the (Python) source code to include the following:

def debug(msg):
   print json.dumps({
      "DEBUG" : msg
   })

...

debug("The stack operation is still working...")

What this did, of course, was store all this output and only print it all after the module had finished executing. So for particularly large cloudformation templates, this means that I wait around for 5 minutes or so, and then suddenly see a large amount of text appear on the screen at the end. What I was expecting was to see "The stack operation is still working..." printed every x seconds.

It would seem that the Asynchronous Actions and Polling are what I'm looking for... but this didn't work, either. The entire task, "Launch CloudFormation for {{ stackname }}", was skipped entirely. See below for the relevant (YAML) snippet from my playbook:

- name: Launch CloudFormation for {{ stackname }}
      cloudformation: >
       stack_name="{{ stackname }}" state=present
       region="{{ region }}" disable_rollback=true
       template="{{ template }}"
      register: cloud
      args:
        template_parameters:
          KeyName: "{{ keyName }}"
          Region: "{{ region }}"
          SecurityGroup: "{{ securityGroup }}"
          BootStrapper: "{{ bootStrapper }}"
          BootStrapCommand: "powershell.exe -executionpolicy unrestricted -File C:\\{{ bootStrapper }} {{ region }}"
          S3Bucket: "{{ s3Bucket }}"
      async: 3600
      poll: 30

This tells me that async is meant for typical shell commands, and not complex modules such as cloudformation. OR -- I may have done something wrong.

Could anyone shed some light on this situation? Again, for large cloudformation tasks that take a while, I would like some periodic indication that the task is still running, and not hanging. I appreciate the help!

like image 664
Brian Avatar asked Sep 18 '14 16:09

Brian


People also ask

How do you show output in Ansible?

To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.

Where can you find documentation on Ansible modules?

As long as your module file is available locally, you can use ansible-doc -t module my_module_name to view your module documentation at the command line. Any parsing errors will be obvious - you can view details by adding -vvv to the command. You should also test the HTML output of your module documentation.

What is stdout in Ansible?

stdout. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on). This field contains the normal output of these utilities. "stdout": "foo!"


1 Answers

My approach for localhost module:

...
module.log(msg='test!!!!!!!!!!!!!!!!!')
...

Then on another window:

 $ tail -f /var/log/messages

 Nov 29 22:32:44 nfvi-ansible-xxxx python2: ansible-test-module test!!!!!!!!!!!!!!!!!
like image 81
Riccardo79 Avatar answered Nov 04 '22 13:11

Riccardo79