# Stripping YAML dashes from output

**URL:** <https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187>\
**Category:** Ansible Project\
**Created:** [November 12, 2020, 2:49pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187 "2020-11-12T14:49:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 12, 2020, 2:49pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/1 "2020-11-12T14:49:07Z")

</div>

I have a playbook where I am traversing our FreeIPA server and pulling out each user’s email address. I’m then putting that data into a text file using the template module:

- name: Set User Email fact  
set\_fact:  
user\_list: “{{ user\_find.json.result | json\_query(‘result.mail’) | list | to\_nice\_yaml }}”

- name: Prepare report  
template:  
src: /root/emails.txt.j2  
dest: /root/emails.txt  
delegate\_to: localhost  
run\_once: true

The j2 template:

User Emails:

Date generated: {{ now(false, ‘%m/%d/%Y %I:%M:%S’) }}  
{{ user\_list }}

This returns the following in the text file:

User Emails:

Date generated: 11/12/2020 09:40:26

- 
  - [user1@example.com](mailto:user1@example.com)

- 
  - [user2@example.com](mailto:user2@example.com)

How can I strip out the preceding dashes?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 12, 2020, 2:52pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/2 "2020-11-12T14:52:54Z")

</div>

> I have a playbook where I am traversing our FreeIPA server and pulling out each user's email address. I'm then putting  
> that data into a text file using the template module:
> 
> - name: Set User Email fact  
> set\_fact:  
> user\_list: "{{ user\_find.json.result | json\_query('result.mail') | list | to\_nice\_yaml }}"
> 
> - name: Prepare report  
> template:  
> src: /root/emails.txt.j2  
> dest: /root/emails.txt  
> delegate\_to: localhost  
> run\_once: true
> 
> The j2 template:
> 
> User Emails:
> 
> Date generated: {{ now(false, '%m/%d/%Y %I:%M:%S') }}  
> {{ user\_list }}
> 
> This returns the following in the text file:
> 
> User Emails:
> 
> Date generated: 11/12/2020 09:40:26  
> - - user1@example.com  
> - - user2@example.com
> 
> How can I strip out the preceding dashes?

Please post a sample of the JSON in user\_find.json.result.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 13, 2020, 12:53pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/3 "2020-11-13T12:53:10Z")

</div>

Here’s a subset of user\_find.json.result:

TASK [Print user\_find] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“count”: 1826,  
“result”: [  
{  
“dn”: “uid=harry.devine,cn=users,cn=example,dc=com”,  
“gidnumber”: [  
“99999”  
],  
“givenname”: [  
“Harry”  
],  
“homedirectory”: [  
“/home/harry.devine”  
],  
“krbcanonicalname”: [  
“[harry.devine@EXAMPLE.COM](mailto:harry.devine@EXAMPLE.COM)”  
],  
“krbprincipalname”: [  
“[harry.devine@EXAMPLE.COM](mailto:harry.devine@EXAMPLE.COM)”  
],  
“loginshell”: [  
“/bin/bash”  
],  
“mail”: [  
“[harry.devine@example.com](mailto:harry.devine@example.com)”  
],  
“nsaccountlock”: false,  
“sn”: [  
“Devine”  
],  
“telephonenumber”: [  
“(609) 867-5309”  
],  
“uid”: [  
“harry.devine”  
],  
“uidnumber”: [  
“1000”  
]  
},  
}  
],  
“summary”: “1826 users matched”,  
“truncated”: false  
}

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2020, 1:49pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/4 "2020-11-13T13:49:05Z")

</div>

that appears to be mutilated, can you post a longer piece so the  
iteration structure is intact ?

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 13, 2020, 1:53pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/5 "2020-11-13T13:53:17Z")

</div>

There are over 1800 users. That’s alot of data for me to redact. How much longer of a sample would you like to see?

Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2020, 1:56pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/6 "2020-11-13T13:56:17Z")

</div>

At least something that is valid json with at least two iterations  
should be welcome.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 13, 2020, 3:48pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/7 "2020-11-13T15:48:27Z")

</div>

Hope this works and helps:

TASK [Print user\_find] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“cache\_control”: “no-cache, private”,  
“changed”: false,  
“connection”: “close”,  
“content\_security\_policy”: “frame-ancestors ‘none’”,  
“content\_type”: “application/json; charset=utf-8”,  
“cookies”: {},  
“cookies\_string”: “”,  
“date”: “Fri, 13 Nov 2020 15:41:31 GMT”,  
“elapsed”: 2,  
“failed”: false,  
“json”: {  
“error”: null,  
“id”: null,  
“principal”: “[admin@EXAMPLE.COM](mailto:admin@EXAMPLE.COM)”,  
“result”: {  
“count”: 1822,  
“result”: [  
{  
“dn”: “uid=user1,cn=users,cn=accounts,dc=example,dc=com”,  
“gidnumber”: [  
“10000”  
],  
“givenname”: [  
“User1”  
],  
“homedirectory”: [  
“/home/user1”  
],  
“krbcanonicalname”: [  
“[user1@EXAMPLE.COM](mailto:user1@EXAMPLE.COM)”  
],  
“krbprincipalname”: [  
“[user1@EXAMPLE.COM](mailto:user1@EXAMPLE.COM)”  
],  
“loginshell”: [  
“/bin/bash”  
],  
“mail”: [  
“[user1@example.com](mailto:user1@example.com)”  
],  
“nsaccountlock”: false,  
“sn”: [  
“User”  
],  
“telephonenumber”: [  
“(555) 555-5555”  
],  
“uid”: [  
“user1”  
],  
“uidnumber”: [  
“1111”  
]  
},  
{  
“dn”: “uid=user2,cn=users,cn=accounts,dc=example,dc=com”,  
“gidnumber”: [  
“10000”  
],  
“givenname”: [  
“User2”  
],  
“homedirectory”: [  
“/home/user2”  
],  
“krbcanonicalname”: [  
“[user2@EXAMPLE.COM](mailto:user2@EXAMPLE.COM)”  
],  
“krbprincipalname”: [  
“[user2@EXAMPLE.COM](mailto:user2@EXAMPLE.COM)”  
],  
“loginshell”: [  
“/bin/bash”  
],  
“mail”: [  
“[user2@example.com](mailto:user2@example.com)”  
],  
“nsaccountlock”: false,  
“sn”: [  
“User”  
],  
“telephonenumber”: [  
“(555)555-5556”  
],  
“uid”: [  
“user2”  
],  
“uidnumber”: [  
“2222”  
]  
},  
}  
],  
“summary”: “1822 users matched”,  
“truncated”: false  
},  
“version”: “4.6.8”  
},  
“msg”: “OK (unknown bytes)”,  
“redirected”: false,  
“server”: “Apache/2.4.6 (Red Hat Enterprise Linux) mod\_auth\_gssapi/1.5.1 mod\_nss/1.0.14 NSS/3.28.4 mod\_wsgi/3.4 Python/2.7.5”,  
“status”: 200,  
“transfer\_encoding”: “chunked”,  
“url”: “[https://auth.example.com/ipa/session/json](https://auth.example.com/ipa/session/json)”,  
“vary”: “Accept-Encoding”,  
“x\_frame\_options”: “DENY”  
}  
}

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2020, 4:12pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/8 "2020-11-13T16:12:51Z")

</div>

Try adding a pipe expression to the query, and leave out the  
'list|to\_nice\_yaml':

set\_fact:  
&nbsp;&nbsp;user\_list: "{{ user\_find.json.result | json\_query('result.mail') }}"

Dick

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 13, 2020, 4:27pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/9 "2020-11-13T16:27:46Z")

</div>

So now I’m getting this:

User Emails:

Date generated: 11/13/2020 11:23:32  
[u’user1@example.com’, u’user2@example.com’]

Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2020, 5:14pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/10 "2020-11-13T17:14:37Z")

</div>

This is a simple list, which is what you wanted.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 13, 2020, 5:28pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/11 "2020-11-13T17:28:55Z")

</div>

So how do I get rid of the u? What I ultimately wanted was JUST the email addresses.

Harry

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 13, 2020, 5:44pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/12 "2020-11-13T17:44:02Z")

</div>

> So how do I get rid of the u? What I ultimately wanted was JUST the email addresses.
> 
> Harry

Join the list members into a string: | join('\n')

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 13, 2020, 5:50pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/13 "2020-11-13T17:50:52Z")

</div>

Perfect! Exactly what I needed. Ultimately I’d like to figure out how to add a semicolon after each entry, then separate them into batches of 500 emails each, but I can worry about that later. This works for now.

Thanks  
Harry

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [November 13, 2020, 11:05pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/14 "2020-11-13T23:05:25Z")

</div>

> Ultimately I’d like to figure out how to add a semicolon after each entry,

This should do that; join with a semicolon not a newline: | join('; ')

> then separate them into batches of 500 emails each

Since you have the list already, you could use a `for` loop with an `if` test to insert a new-line to get the batches:

This should work assuming your full list of addresses is in `list_of_addresses`:

{% for email\_item in list\_of\_addresses %}{{ email\_item }}{% if loop.index is divisibleby 500 %}{{ ‘\n’ -}}{% else %};{% endif %}{% endfor %}  
(note, all on one line)

All you need to do is replace the `list_of_addresses` with your YAML list of addresses and you should get your list broken into 500 element lines.

You can check your Jinja syntax by using this site: [https://cryptic-cliffs-32040.herokuapp.com/](https://cryptic-cliffs-32040.herokuapp.com/)

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 14, 2020, 8:02am UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/15 "2020-11-14T08:02:01Z")

</div>

> > Ultimately I'd like to figure out how to add a semicolon after each entry,
> 
> This should do that; join with a semicolon not a newline: | join('; ')
> 
> > then separate them into batches of 500 emails each
> 
> Since you have the list already, you could use a `for` loop with an `if` test to insert a new-line to get the batches:
> 
> This should work assuming your full list of addresses is in `list_of_addresses`:  
> {% for email\_item in list\_of\_addresses %}{{ email\_item }}{% if loop.index is divisibleby 500 %}{{ '\n' -}}{% else %};{%  
> endif %}{% endfor %}  
> (note, all on one line)
> 
> All you need to do is replace the `list_of_addresses` with your YAML list of addresses and you should get your list  
> broken into 500 element lines.

That is indeed a nice piece of Jinja.

Thanks, Dan!

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 16, 2020, 5:56pm UTC](https://forum.ansible.com/t/stripping-yaml-dashes-from-output/34187/16 "2020-11-16T17:56:10Z")

</div>

Worked exactly as I wanted/needed! Great tip! I thank everyone for their help on this!

Thanks,  
Harry
