Removing unicode from output

442 views
Skip to first unread message

lift...@gmail.com

unread,
Oct 27, 2022, 11:17:58 AM10/27/22
to Ansible Project
I was reading some suggestions on how to remove the u' unicode presentation from my output, but none of them work.  Here's what I'm trying:

  - name: Set List of user's information
    set_fact:
      user_info: "{{ user_show.results | json_query('[*].json.result.result.{uid: uid[0], email: mail[0], sn: sn[0], fullname: cn[0], givenName: givenname[0], telephonenumber: telephonenumber[0], homedir: homedirectory[0]}') | list | to_yaml }}"

  - name: Prepare report
    template:
      src: files/users.csv.j2
      dest: /tmp/users.csv
    delegate_to: localhost
    run_once: true

In the template file, I'm trying to output the each entry as a new line in the CSV file:

sAMAccountName,sn,FullName,givenName,email,telephoneNumber,homedir
{% for user in user_info %}
{{ user.uid,user.sn,user.fullname,user.givenName,user.email,user.telephonenumber,user.homedir }}
{% endfor %}

If I do NOT use the to_yaml (or to_nice_yaml) filter, I get the output I want, but with the u' on each string.  If I do use the filter on the variable above, I get a bunch of AnsibleUndefined as seen below:

With to_yaml:
sAMAccountName,sn,FullName,givenName,email,telephoneNumber,homedir
(AnsibleUndefined, AnsibleUndefined, AnsibleUndefined, AnsibleUndefined, AnsibleUndefined, AnsibleUndefined, AnsibleUndefined)

Without to_yaml:

sAMAccountName,sn,FullName,givenName,email,telephoneNumber,homedir
(u'user1', u'User1', u'Test User1', u'Test', u'us...@example.com', u'(609) xxx-yyyy', u'/home/user1')
(u'user2', u'User2', u'Test User2', u'Test', u'us...@example.com', u'(609) xxx-yyyy', u'/home/user2')

Any ideas?

Matt Martz

unread,
Oct 27, 2022, 11:40:35 AM10/27/22
to ansible...@googlegroups.com
The problem here is that your template is creating a python tuple, and not creating a CSV of values

You likely want this instead:

{{ user.uid }},{{ user.sn }},{{ user.fullname }},{{ user.givenName }},{{ user.email }},{{ user.telephonenumber }},{{ user.homedir }}

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c3d1bdd3-a50f-42a3-9550-001a225b58dan%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

lift...@gmail.com

unread,
Oct 27, 2022, 11:48:39 AM10/27/22
to Ansible Project
Thanks Matt!  Worked like a charm.  I knew I was overlooking something, just wasn't sure what it was.

Thanks,
Harry

Reply all
Reply to author
Forward
0 new messages