Discussion:
How to correctly print a multiline stdout?
Nicolas Grilly
2013-09-04 15:01:15 UTC
Permalink
Hello,

We run our database migrations using Ansible with something along these
lines:

- name: Run database migrations
command: env/bin/python migrate.py -q
chdir=$project_dir/releases/$release_date
register: result
changed_when: result.stdout

migrate.py prints the list of applied migrations to stdout.

We try to print this list because our developers want some feedback about
which migrations were applied:

- debug: msg="{{result.stdout}}"
when: result.stdout

But all new lines are escaped to \n, which makes the output quite difficult
to read.

Is there any solution to this?

In your own projects, how do you execute your database migrations? Do you
try to get some feedback as what we try to achieve here? Or do you use
another approach?

Thanks,

-- Nicolas
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Firat Arig
2013-09-05 11:18:31 UTC
Permalink
Hi there,

Printing lines separately can be done like

- name: Run database migrations
command: env/bin/python migrate.py -q
chdir=$project_dir/releases/$release_date
register: result
changed_when: result.stdout_lines

- debug: msg="{{item}}"
with_items: result.stdout_lines
when: result.stdout_lines

Cheers,

-- Firat
Post by Nicolas Grilly
Hello,
We run our database migrations using Ansible with something along these
- name: Run database migrations
command: env/bin/python migrate.py -q
chdir=$project_dir/releases/$release_date
register: result
changed_when: result.stdout
migrate.py prints the list of applied migrations to stdout.
We try to print this list because our developers want some feedback about
- debug: msg="{{result.stdout}}"
when: result.stdout
But all new lines are escaped to \n, which makes the output quite
difficult to read.
Is there any solution to this?
In your own projects, how do you execute your database migrations? Do you
try to get some feedback as what we try to achieve here? Or do you use
another approach?
Thanks,
-- Nicolas
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Nicolas Grilly
2013-09-06 14:29:39 UTC
Permalink
Post by Firat Arig
Printing lines separately can be done like
- name: Run database migrations
command: env/bin/python migrate.py -q
chdir=$project_dir/releases/$release_date
register: result
changed_when: result.stdout_lines
- debug: msg="{{item}}"
with_items: result.stdout_lines
when: result.stdout_lines
Waouh, that's very clever :)

But the output is still a bit "weird" for this use case:

ok: [host.domain.net] => (item=foo) => {"item": "foo", "msg": "foo"}
ok: [host.domain.net] => (item=bar) => {"item": "bar", "msg": "bar"}
...

Is there another way?

-- Nicolas
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael DeHaan
2013-09-06 21:01:48 UTC
Permalink
Easiest way to debug this is just to use "-v" and it will output the
previous data.
Post by Nicolas Grilly
Post by Firat Arig
Printing lines separately can be done like
- name: Run database migrations
command: env/bin/python migrate.py -q chdir=$project_dir/releases/$
**release_date
register: result
changed_when: result.stdout_lines
- debug: msg="{{item}}"
with_items: result.stdout_lines
when: result.stdout_lines
Waouh, that's very clever :)
ok: [host.domain.net] => (item=foo) => {"item": "foo", "msg": "foo"}
ok: [host.domain.net] => (item=bar) => {"item": "bar", "msg": "bar"}
...
Is there another way?
-- Nicolas
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
Michael DeHaan <***@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Nicolas Grilly
2013-09-06 23:59:07 UTC
Permalink
Post by Michael DeHaan
Easiest way to debug this is just to use "-v" and it will output the
previous data.
Yes, but if result.stdout contains new lines, they will show up escaped as
\n instead of "real" new lines, which makes it difficult to read. Any
advice on this?
--
Nicolas Grilly
Vocation City | Web Recruitment Platform
Garden | Web Software Architects
+33 1 45 72 48 78 - office
+33 6 03 00 25 34 - mobile
www.vocationcity.com
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Jesse Keating
2013-09-07 01:29:57 UTC
Permalink
Easiest way to debug this is just to use "-v" and it will output the previous data.
Yes, but if result.stdout contains new lines, they will show up escaped as \n instead of "real" new lines, which makes it difficult to read. Any advice on this?
You could try:

debug: msg="{{ print(result.stdout) }}"

The print() function should turn the \n's into new lines.

-jlk
Nicolas Grilly
2013-09-07 11:13:34 UTC
Permalink
Post by Jesse Keating
debug: msg="{{ print(result.stdout) }}"
The print() function should turn the \n's into new lines.
It fails with the following error:

TASK: [debug msg="{{print(result.stdout)}}"]
**********************************
fatal: [gardentechno.typhon.net] => One or more undefined variables:
'print' is undefined

-- Nicolas
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael DeHaan
2013-09-07 12:47:27 UTC
Permalink
Yep, we don't export random functions here.

"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.

You wouldn't want os.unlink() in there either! :)
Post by Nicolas Grilly
Post by Jesse Keating
debug: msg="{{ print(result.stdout) }}"
The print() function should turn the \n's into new lines.
TASK: [debug msg="{{print(result.stdout)}}"]
**********************************
'print' is undefined
-- Nicolas
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
Michael DeHaan <***@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Nicolas Grilly
2013-09-07 13:16:28 UTC
Permalink
Post by Michael DeHaan
Yep, we don't export random functions here.
"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.
You wouldn't want os.unlink() in there either! :)
Hello Michael,

Yes, I know :) It was the purpose of the small pull request James accepted
yesterday to update the documentation about Jinja2 expressions in playbooks.

This is why I was a quite surprised by Jesse suggestion. I just gave it a
try to make sure I was not missing something, and hopefully it doesn't work
:)

Any idea on how to solve the problem I stated at the start of this
discussion: We have a playbook that executes database schema migrations,
and I want to print the list of applied migrations on the console?... The
list of applied migrations is printed on stdout by the migration command.

Cheers,

Nicolas
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Firat Arig
2013-09-09 06:29:38 UTC
Permalink
I don't think what you want it is currently possible through ansible.
Polling the output of a file would be a less hacky way to address your
problem.
Post by Nicolas Grilly
Post by Michael DeHaan
Yep, we don't export random functions here.
"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.
You wouldn't want os.unlink() in there either! :)
Hello Michael,
Yes, I know :) It was the purpose of the small pull request James accepted
yesterday to update the documentation about Jinja2 expressions in playbooks.
This is why I was a quite surprised by Jesse suggestion. I just gave it a
try to make sure I was not missing something, and hopefully it doesn't work
:)
Any idea on how to solve the problem I stated at the start of this
discussion: We have a playbook that executes database schema migrations,
and I want to print the list of applied migrations on the console?... The
list of applied migrations is printed on stdout by the migration command.
Cheers,
Nicolas
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael DeHaan
2013-09-09 14:46:44 UTC
Permalink
It seems what we really want is a better way for the debug module to work
with structured and multiline data, like:

debug: var=foo.stdout_lines
Post by Firat Arig
I don't think what you want it is currently possible through ansible.
Polling the output of a file would be a less hacky way to address your
problem.
Post by Nicolas Grilly
Post by Michael DeHaan
Yep, we don't export random functions here.
"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.
You wouldn't want os.unlink() in there either! :)
Hello Michael,
Yes, I know :) It was the purpose of the small pull request James
accepted yesterday to update the documentation about Jinja2 expressions in
playbooks.
This is why I was a quite surprised by Jesse suggestion. I just gave it a
try to make sure I was not missing something, and hopefully it doesn't work
:)
Any idea on how to solve the problem I stated at the start of this
discussion: We have a playbook that executes database schema migrations,
and I want to print the list of applied migrations on the console?... The
list of applied migrations is printed on stdout by the migration command.
Cheers,
Nicolas
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
Michael DeHaan <***@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Jason Freeman
2013-09-10 21:06:39 UTC
Permalink
If you are really looking for specialized output, why bother using debug
module? Add the following task and create some simple script to parse. The
inventory_hostname will give the remote host corresponding to the stdout
(you don't want to mix those up).

- local_action: command bash /my/local/path/writer.sh
"$inventory_hostname" "{{ item }}"
with_items: result.stdout_lines
when: result.stdout

A simple bash script on your local machine (writer.sh)...

#!/bin/sh
echo $1 $2 >> out.log

I came looking for something similar, but there doesn't seem to be a great
way to format output (built in).

hope it helps
Post by Michael DeHaan
It seems what we really want is a better way for the debug module to work
debug: var=foo.stdout_lines
Post by Firat Arig
I don't think what you want it is currently possible through ansible.
Polling the output of a file would be a less hacky way to address your
problem.
Post by Nicolas Grilly
Post by Michael DeHaan
Yep, we don't export random functions here.
"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.
You wouldn't want os.unlink() in there either! :)
Hello Michael,
Yes, I know :) It was the purpose of the small pull request James
accepted yesterday to update the documentation about Jinja2 expressions in
playbooks.
This is why I was a quite surprised by Jesse suggestion. I just gave it
a try to make sure I was not missing something, and hopefully it doesn't
work :)
Any idea on how to solve the problem I stated at the start of this
discussion: We have a playbook that executes database schema migrations,
and I want to print the list of applied migrations on the console?... The
list of applied migrations is printed on stdout by the migration command.
Cheers,
Nicolas
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael DeHaan
2013-09-11 12:24:34 UTC
Permalink
Probably because it requires writing bash in the middle of your Ansible
playbook, which is something we want to avoid people having to do.

It would be much better to enhance the debug module as it would make the
playbook more readable without resorting to hijinks.
Post by Jason Freeman
If you are really looking for specialized output, why bother using debug
module? Add the following task and create some simple script to parse. The
inventory_hostname will give the remote host corresponding to the stdout
(you don't want to mix those up).
- local_action: command bash /my/local/path/writer.sh
"$inventory_hostname" "{{ item }}"
with_items: result.stdout_lines
when: result.stdout
A simple bash script on your local machine (writer.sh)...
#!/bin/sh
echo $1 $2 >> out.log
I came looking for something similar, but there doesn't seem to be a great
way to format output (built in).
hope it helps
Post by Michael DeHaan
It seems what we really want is a better way for the debug module to work
debug: var=foo.stdout_lines
Post by Firat Arig
I don't think what you want it is currently possible through ansible.
Polling the output of a file would be a less hacky way to address your
problem.
Post by Nicolas Grilly
Post by Michael DeHaan
Yep, we don't export random functions here.
"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.
You wouldn't want os.unlink() in there either! :)
Hello Michael,
Yes, I know :) It was the purpose of the small pull request James
accepted yesterday to update the documentation about Jinja2 expressions in
playbooks.
This is why I was a quite surprised by Jesse suggestion. I just gave it
a try to make sure I was not missing something, and hopefully it doesn't
work :)
Any idea on how to solve the problem I stated at the start of this
discussion: We have a playbook that executes database schema migrations,
and I want to print the list of applied migrations on the console?... The
list of applied migrations is printed on stdout by the migration command.
Cheers,
Nicolas
--
You received this message because you are subscribed to the Google
Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
.
--
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
Michael DeHaan <***@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Brad Smith
2015-05-05 17:16:58 UTC
Permalink
Did anything ever come of this? I would really like to be able to print
friendly "Provisioning succeeded! Here's what to do next..." messages at
the end of some of my playbooks.

--Brad
Post by Michael DeHaan
Probably because it requires writing bash in the middle of your Ansible
playbook, which is something we want to avoid people having to do.
It would be much better to enhance the debug module as it would make the
playbook more readable without resorting to hijinks.
Post by Jason Freeman
If you are really looking for specialized output, why bother using debug
module? Add the following task and create some simple script to parse. The
inventory_hostname will give the remote host corresponding to the stdout
(you don't want to mix those up).
- local_action: command bash /my/local/path/writer.sh
"$inventory_hostname" "{{ item }}"
with_items: result.stdout_lines
when: result.stdout
A simple bash script on your local machine (writer.sh)...
#!/bin/sh
echo $1 $2 >> out.log
I came looking for something similar, but there doesn't seem to be a
great way to format output (built in).
hope it helps
Post by Michael DeHaan
It seems what we really want is a better way for the debug module to
debug: var=foo.stdout_lines
Post by Firat Arig
I don't think what you want it is currently possible through ansible.
Polling the output of a file would be a less hacky way to address your
problem.
On Sat, Sep 7, 2013 at 2:47 PM, Michael DeHaan <
Post by Michael DeHaan
Yep, we don't export random functions here.
"{{ }}" is a call for string templating, it is not a place to insert
arbitrary python code.
You wouldn't want os.unlink() in there either! :)
Hello Michael,
Yes, I know :) It was the purpose of the small pull request James
accepted yesterday to update the documentation about Jinja2 expressions in
playbooks.
This is why I was a quite surprised by Jesse suggestion. I just gave
it a try to make sure I was not missing something, and hopefully it doesn't
work :)
Any idea on how to solve the problem I stated at the start of this
discussion: We have a playbook that executes database schema migrations,
and I want to print the list of applied migrations on the console?... The
list of applied migrations is printed on stdout by the migration command.
Cheers,
Nicolas
--
You received this message because you are subscribed to the Google
Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/groups/opt_out.
--
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/412046d8-02c2-4b6c-8899-56c1c78a0a8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nicolas Grilly
2015-05-05 19:17:34 UTC
Permalink
Post by Brad Smith
Did anything ever come of this? I would really like to be able to print
friendly "Provisioning succeeded! Here's what to do next..." messages at
the end of some of my playbooks.
Not to my knowledge. I'd like to have a solution :-)
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e23abf07-6c8d-4ee7-a832-8c2ce7210826%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-05-05 19:45:53 UTC
Permalink
everything that has stdout, shoudl have stdout_lines, which is already
multiline, for those that don't just use split('/n').
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8nJa%3DfggrZ4wXYt6%3Djy%3Dud01Hz4SCzPkJ3nkx132S6mgQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...