Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: how to get output to display [duplicate]

Tags:

I have the following playbook, Playbook with output

There are currently no errors and it runs fine. However, it does not display the output to the console. I have come across this with other playbooks and got around it by adding the following task to the playbook:

-debug: var=output.stdout_lines 

and it prints the output. However, I tried to do the same thing in the above playbook and it says that the variable was undefined (code not shown because it didn't work).

Is anyone aware of a better way to get the output to print to the console without using -debug? Any ansible references would be greatly appreciated.

like image 268
Tom Haley Avatar asked Feb 17 '17 16:02

Tom Haley


People also ask

How do I display Ansible output?

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.

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!"

How do I register Ansible output?

In Ansible, you can run any shell command on your Ansible hosts, the hosts you will be configuring with Ansible. These shell commands may have outputs. By default, the output is ignored. If you want to store the output in a variable and use it later, then you can use the Ansible register module.


1 Answers

Every Ansible task when run can save its results into a variable. To do this, you have to specify which variable to save the results into. Do this with the register parameter, independently of the module used.

Once you save the results to a variable you can use it later in any of the subsequent tasks. So for example if you want to get the standard output of a specific task you can write the following:

--- - hosts: localhost   tasks:     - shell: ls       register: shell_result      - debug:         var: shell_result.stdout_lines 

Here register tells ansible to save the response of the module into the shell_result variable, and then we use the debug module to print the variable out.

An example run would look like the this:

PLAY [localhost] ***************************************************************  TASK [command] ***************************************************************** changed: [localhost]  TASK [debug] ******************************************************************* ok: [localhost] => {     "shell_result.stdout_lines": [         "play.yml"     ] } 

Responses can contain multiple fields. stdout_lines is one of the default fields you can expect from a module's response.

Not all fields are available from all modules, for example for a module which doesn't return anything to the standard out you wouldn't expect anything in the stdout or stdout_lines values, however the msg field might be filled in this case. Also there are some modules where you might find something in a non-standard variable, for these you can try to consult the module's documentation for these non-standard return values.

Alternatively you can increase the verbosity level of ansible-playbook. You can choose between different verbosity levels: -v, -vvv and -vvvv. For example when running the playbook with verbosity (-vvv) you get this:

PLAY [localhost] ***************************************************************  TASK [command] ***************************************************************** (...) changed: [localhost] => {     "changed": true,     "cmd": "ls",     "delta": "0:00:00.007621",     "end": "2017-02-17 23:04:41.912570",     "invocation": {         "module_args": {             "_raw_params": "ls",             "_uses_shell": true,             "chdir": null,             "creates": null,             "executable": null,             "removes": null,             "warn": true         },         "module_name": "command"     },     "rc": 0,     "start": "2017-02-17 23:04:41.904949",     "stderr": "",     "stdout": "play.retry\nplay.yml",     "stdout_lines": [         "play.retry",         "play.yml"     ],     "warnings": [] } 

As you can see this will print out the response of each of the modules, and all of the fields available. You can see that the stdout_lines is available, and its contents are what we expect.

To answer your main question about the jenkins_script module, if you check its documentation, you can see that it returns the output in the output field, so you might want to try the following:

tasks:   - jenkins_script:       script: (...)     register: jenkins_result    - debug:       var: jenkins_result.output 
like image 179
SztupY Avatar answered Sep 22 '22 11:09

SztupY