Ansible stdout_lines writes as list syntax [u'text1',u'text2'] instead of multiline?

3,034 views
Skip to first unread message

burn...@umn.edu

unread,
Dec 29, 2016, 12:44:55 AM12/29/16
to Ansible Project
Hello - Assistance requested with stdout_lines output.

I have some tasks that take diff results of two files and then writes the output to a log.

Code Snippet:
- name: Gather diff output of deployed changes
  command
: "diff {{ deployed_copy_output.dest }} {{ deployed_copy_output.backup_file }}"
 
register: diff_output
  failed_when
: diff_output.rc > 1

- name: Log diff changes
  blockinfile
:
    dest
: "{{ log_dir }}/{{ pb_diff_logname }}"
    content
: |
     
{{ app_log_header }}
     
Command: 'diff {{ deployed_copy_output.dest }} {{ deployed_copy_output.backup_file }}'
     
{{ diff_output.stdout_lines }}
     
{{ app_log_footer }}
    create
: yes
    insertafter
: EOF
    marker
: "{{ app_log_marker }}"

This results in output that looks like the below. 
**********
Command: 'diff /opt/wmspt/.profile /opt/wmspt/.profile.2016-12-28@23:06:41~'
[u'2a3', u'> echo hi']
**********

I would expect it to look more like the debug output:
TASK [role_config_shared_profile : debug] **************************************
ok: [ptl01a0fap006] => {
    "diff_output": {
        "changed": false, 
        "cmd": [
            "diff", 
            "/opt/wmspt/.profile", 
            "/opt/wmspt/.profile.2016-12-28@23:27:24~"
        ], 
        "delta": "0:00:00.111675", 
        "end": "2016-12-28 23:27:25.068251", 
        "failed": false, 
        "failed_when_result": false, 
        "rc": 1, 
        "start": "2016-12-28 23:27:24.956576", 
        "stderr": "", 
        "stdout": "2a3\n> echo hi", 
        "stdout_lines": [
            "2a3", 
            "> echo hi"
        ], 

        "warnings": []
    }
}  

Writing the stdout_lines has some sort of list syntax with [u'text1',u'text2'], even though debug the same info is correct multiline output. Is there something that I'm missing here?

Thanks

J Hawkesworth

unread,
Dec 29, 2016, 2:14:04 AM12/29/16
to Ansible Project
Try using join, like this

{{ results.stdout_lines | join ('\n') }}

Not tried, but hope it helps,

Jon

burn...@umn.edu

unread,
Dec 29, 2016, 11:00:23 AM12/29/16
to Ansible Project
Hello Jon - Thanks for the help!

The change you suggested did help clean it up a bit. It changed it from a list output to one line string separated by literal '\n'. Something like "2a3\n> echo hi". This was a step in the right direction.

I did some more digging around and found an answer using Jinja2 whitespace control. Uses a for loop to parse the list output via jinja2 filters.
Updated Code Snippet:
- name: Log diff changes
  blockinfile
:
    dest
: "{{ log_dir }}/{{ pb_diff_logname }}"
    content
: |
     
{{ app_log_header }}
     
Command: 'diff {{ deployed_copy_output.dest }} {{ deployed_copy_output.backup_file }}'
      {% for item in diff_output.stdout_lines %}
         
{{ item }}
     
{% endfor %}

     
{{ app_log_footer }}
    create
: yes
    insertafter
: EOF
    marker
: "{{ app_log_marker }}"

The output is then quite clean in the log file.
**********
Command: 'diff /opt/wmspt/.profile /opt/wmspt/.profile.2016-12-29@08:57:50~'
    2a3
    > echo hi

**********

Thanks!

Brian Coca

unread,
Jan 5, 2017, 4:21:50 PM1/5/17
to ansible...@googlegroups.com
Just use stdout ... which is a multiline string, stdout_lines is a split of this for people to use each line in a list.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages