title: How to join a list of strings in Ansible?
body: <p>In Ansible, I have a list of strings that I want to join with newline characters to create a string, that when written to a file, becomes a series of lines. However, when I use the join() filter, it works on the inner list, the characters in the strings, and not on the outer list, the strings themselves. Here's my sample code:</p>
  
  <pre><code># Usage: ansible-playbook tst3.yaml --limit &lt;GRP&gt;
  ---
  - hosts: all
    remote_user: root
  
    tasks:
  
    - name: Create the list
      set_fact:
          my_item: "{{ item }}"
      with_items:
        - "One fish"
        - "Two fish"
        - "Red fish"
        - "Blue fish"
      register: my_item_result
  
    - name: Extract items and turn into a list
      set_fact:
          my_list: "{{ my_item_result.results | map(attribute='ansible_facts.my_item') | list }}"
  
    - name: Examine the list
      debug:
          msg: "{{ my_list }}"
  
    - name: Concatenate the public keys
      set_fact:
          my_joined_list: "{{ item | join('\n') }}"
      with_items:
        - "{{ my_list }}"
  
    - name: Examine the joined string
      debug:
          msg: "{{ my_joined_list }}"
  </code></pre>
  
  <p>I want to get output that looks like:</p>
  
  <pre><code>One fish
  Two fish
  Red fish
  Blue Fish
  </code></pre>
  
  <p>What I get instead is:</p>
  
  <pre><code>TASK: [Examine the joined string] *********************************************
  ok: [hana-np-11.cisco.com] =&gt; {
      "msg": "B\nl\nu\ne\n \nf\ni\ns\nh"
  }
  ok: [hana-np-12.cisco.com] =&gt; {
      "msg": "B\nl\nu\ne\n \nf\ni\ns\nh"
  }
  ok: [hana-np-13.cisco.com] =&gt; {
      "msg": "B\nl\nu\ne\n \nf\ni\ns\nh"
  }
  ok: [hana-np-14.cisco.com] =&gt; {
      "msg": "B\nl\nu\ne\n \nf\ni\ns\nh"
  }
  ok: [hana-np-15.cisco.com] =&gt; {
      "msg": "B\nl\nu\ne\n \nf\ni\ns\nh"
  }
  </code></pre>
  
  <p>How do I properly concatenate a list of strings with the newline character?</p>