<p>That's still a XY problem IMHO, why do you want an output in  a single line at all ? what's the need for it ? </p>

<p>If you can't explain more and wish to footgun yourself by replacing newlines with spaces the use <code>command | tr '\n' ' '</code> (you'll probably end up loosing the end of the log). </p>

<p>If your goal if to parse the last line then </p>

<p><code>command | awk '/ok=/ { for (i=3; i &lt; 7; i++) { split($i,a,"="); printf a[1]":"a[2]" " } }'</code> </p>

<p>will give <code>ok:3 changed:1 unreachable:0 failed:0</code> </p>

<p>This select the lines containing <code>ok=</code> and split each <code>key=value</code> field (3 to 6) on the <code>=</code> char, thus giving the key as first element of array <code>a</code> and the value as the second element.</p>

<p>use <code>print</code> instead of <code>printf</code> to get each state on separate lines.</p>

CC BY-SA 3.0
2017-08-28T08:40:47.913Z
2017-08-28T08:40:47.913Z
Tensibai
<p>I am not sure if that is possible or not but you can write same command in bash script</p>

<pre><code>#!/bin/bash

ansible-playbook playbookname.yaml &gt; output.txt

# Then grep from output.txt what you want

grep -A 5 "PLAY" output.txt
</code></pre>

<p>May not be good way but will achieve what you want.</p>

CC BY-SA 3.0
2018-04-12T09:48:35.530Z
Would work only if there's less than 6 lines after the PLAY keyword...
Tensibai
CC BY-SA 3.0
2018-04-12T11:21:56.493Z
(well would work, but not answering the question as it would output the same thing as in the question, which is not what the question is about)
Tensibai
CC BY-SA 3.0
2018-04-12T11:45:18.587Z
just an example s/he can grep anything s/he like
Prakash
CC BY-SA 3.0
2018-04-12T11:59:23.830Z
I don't get the point of showing how to grep multiple lines where the question is how to get the output in one line ... For sure it does not achieve what the OP is after
Tensibai
CC BY-SA 3.0
2018-04-12T09:42:27.470Z
2018-04-12T09:42:27.470Z
Prakash
<p>In Ansible we are running a playbook using <code>ansible-playbook playbookname.yaml</code>. The output will be multiple lines (like below). I need output in single line.  How can I get that?</p>

<pre><code>PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0
</code></pre>

CC BY-SA 3.0
2017-08-23T12:21:13.177Z
Why and what did you try?
030
CC BY-SA 3.0
2017-08-24T11:43:08.880Z
Hi. As @030 asked, please add in more info. about what do you mean by not wanting output in multiple lines. In the current state, this qn is very unclear.  We shall consider reopening the post once you clear things up :)
Dawny33
CC BY-SA 3.0
2017-08-26T18:10:20.180Z
You probably want to look into callback plugins.
LEI
CC BY-SA 3.0
2017-09-30T10:47:09.167Z
You could literally translate the newlines by piping to `tr '\n' ' '`  But a custom callback plugin is the way to do this.
Zlemini 
<ansible>
How to get Ansible playbook ouput in one line?
venkat
# How to get Ansible playbook ouput in one line?

(Asked by: username_0 on 2017-08-23)

In Ansible we are running a playbook using ansible-playbook playbookname.yaml. The output will be multiple lines (like below). I need output in single line.  How can I get that?
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

**Tags:** ansible

> **Comments:**
> **username_1**: Why and what did you try?  
> **username_2**: Hi. As @username_1 asked, please add in more info. about what do you mean by not wanting output in multiple lines. In the current state, this qn is very unclear.  We shall consider reopening the post once you clear things up :)  
> **username_3**: You probably want to look into callback plugins.  
> **username_4**: You could literally translate the newlines by piping to `tr '\n' ' '`  But a custom callback plugin is the way to do this.  

---

## 2 answers

### Answer by username_5 on 2017-08-28. Score: 5

That's still a XY problem IMHO, why do you want an output in a single line at all ? what's the need for it ?

If you can't explain more and wish to footgun yourself by replacing newlines with spaces the use `command | tr '\n' ' '` (you'll probably end up loosing the end of the log).

If your goal if to parse the last line then

`command | awk '/ok=/ { for (i=3; i < 7; i++) { split($i,a,"="); printf a[1]":"a[2]" " } }'`

will give `ok:3 changed:1 unreachable:0 failed:0`

This select the lines containing `ok=` and split each `key=value` field (3 to 6) on the `=` char, thus giving the key as first element of array `a` and the value as the second element.

use `print` instead of `printf` to get each state on separate lines.


---

### Answer by username_6 on 2018-04-12. Score: -1

I am not sure if that is possible or not but you can write same command in bash script
#!/bin/bash

ansible-playbook playbookname.yaml > output.txt

# Then grep from output.txt what you want

grep -A 5 "PLAY" output.txt

May not be good way but will achieve what you want.

> **Comments:**
> **username_5**: Would work only if there's less than 6 lines after the PLAY keyword...  
> **username_5**: (well would work, but not answering the question as it would output the same thing as in the question, which is not what the question is about)  
> **username_6**: just an example s/he can grep anything s/he like  
> **username_5**: I don't get the point of showing how to grep multiple lines where the question is how to get the output in one line ... For sure it does not achieve what the OP is after  

---

