How to get newline characters to be interpreted using debug and hipchat

1,606 views
Skip to first unread message

Kathy Allen

unread,
Jan 27, 2015, 2:19:01 PM1/27/15
to ansible...@googlegroups.com
Hello. I'm trying to send output via debug and hipchat which I want to appear as multiple lines. But I'm failing. Can anyone advise? In my test below, I of course want the output to show up like this:

    i am a runon sentence
    i go on and on



Here is my test play and the results. I'm using ansible 1.8.2.

$ cat q.yml
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  tasks:
- include: "{{ playbook_dir }}/dev/debug_msg.yml"

$ cat dev/debug_msg.yml
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  vars:
     my_msg: |
       "A - i am a runon sentence
       i go on and on and on"

  tasks:
  - debug: msg={{my_msg}}
  - debug: msg="B - i am a runon sentence \n i go on and on and on"

  - name: Testify to hipchat
    hipchat: token={{ hipchat_token }} from={{ hipchat_user }} room={{ priv_hipchat_room }} msg="C - i am a runon sentence \n i go on and on and on"

  - name: Testify to hipchat again
    hipchat: token={{ hipchat_token }} from={{ hipchat_user }} room={{ priv_hipchat_room }} msg={{ my_msg }}


$ ansible-playbook -i inventor.ini q.yml




result

TASK: [debug msg={{my_msg}}] **************************************************
Tuesday 27 January 2015  19:05:10 +0000 (0:00:00.000)       0:00:00.040 *******
ok: [127.0.0.1] => {
    "msg": "A - i am a runon sentence\ni go on and on and on"
}

TASK: [debug msg="B - i am a runon sentence \n i go on and on and on"] ********
Tuesday 27 January 2015  19:05:10 +0000 (0:00:00.005)       0:00:00.045 *******
ok: [127.0.0.1] => {
    "msg": "B - i am a runon sentence \\n i go on and on and on"
}


in hipchat:

C - i am a runon sentence \n i go on and on and on
A - i am a runon sentence i go on and on and on



thanks,
kallen

Giovanni Tirloni

unread,
Jan 28, 2015, 9:38:46 AM1/28/15
to ansible...@googlegroups.com
On Tue, 27 Jan 2015 11:19 -0800, Kathy Allen <kal...@gmail.com> wrote:
> TASK: [debug msg={{my_msg}}] **************************************************
> Tuesday 27 January 2015  19:05:10 +0000 (0:00:00.000)       0:00:00.040 *******
> ok: [127.0.0.1] => {
>     "msg": "A - i am a runon sentence\ni go on and on and on"
> }

I believe this is being processed by Jinja2, which is ignoring the
newline.

> TASK: [debug msg="B - i am a runon sentence \n i go on and on and on"] ********
> Tuesday 27 January 2015  19:05:10 +0000 (0:00:00.005)       0:00:00.045 *******
> ok: [127.0.0.1] => {
>     "msg": "B - i am a runon sentence \\n i go on and on and on"
> }

Here debug is calling exit_json() which in turns calls json.dumps() [1],
which will escape the newline.

>>> json.dumps("one line\nanother line")
'"one line\\nanother line"'

You'll notice that in hipchat you don't see "\\n" but "\n". The function
send_msg() in the hipchat module is correctly calling urllib.urlencode()
to create the URL and then making the external call (no JSON involved
here). I believe this is a separate issue (i.e. hipchat not correctly
interpreting newlines).

[1] - https://docs.python.org/2/library/json.html
Reply all
Reply to author
Forward
0 new messages