# with\_dict expects a dict...can a dict be too large

**URL:** <https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838>\
**Category:** Ansible Project\
**Created:** [June 22, 2016, 7:55pm UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838 "2016-06-22T19:55:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 22, 2016, 7:55pm UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/1 "2016-06-22T19:55:01Z")

</div>

I messed up and deleted my first post. Sorry!

I think I need another pair of eyes here. I am attempting to run a dict of users through an ipa user-add command, but it fails with the error “with\_dict expects a dict.” I believe I have formatted my dict correctly (it’s about 40,000 lines long, generated from a python3 script):

`---`  
`students:`  
`username1:`  
`lastname: jibberish`  
`firstname: jibberish`  
`firstinitial: jibberish`  
`studentid: jibberish`  
`dob: jibberish`  
`dateexpires: 2018-06-19`  
`epochexpiry: 1529415778`  
`uid: jibberish`  
`passwordhash: "jibberish"`  
`username2:`  
`lastname: jibberish`  
`firstname: jibberish`  
`firstinitial: jibberish`  
`studentid: jibberish`  
`dob: jibberish`  
`dateexpires: 2018-06-19`  
`epochexpiry: 1529416593`  
`uid: jibberish`  
`passwordhash: "jibberish"`  
`username3:`  
`lastname: jibberish`  
`firstname: jibberish`  
`firstinitial: jibberish`  
`studentid: jibberish`  
`dob: jibberish`  
`dateexpires: 2018-06-19`  
`epochexpiry: 1529416755`  
`uid: jibberish`  
`passwordhash: "jibberish"`

And here is the play where I am trying to use it:

`

- name: Add existing student users to ipaserver  
command: “/bin/ipa user-add  
{{ item.key }}  
–first=‘{{ item.value.firstname }}’  
–last=‘{{ item.value.lastname }}’  
–cn=‘{{item.value.firstname}} {{item.value.lastname}}’  
–email={{ [item.value.name](http://item.value.name/) }}@[d](http://stu.parkland.edu/)omain  
–shell=/bin/bash  
–homedir=‘/home/students/{{ item.value.firstinitial }}/{{ item.key }}’  
–uid=‘{{ item.value.uid }}’  
–gidnumber=‘{{ item.value.uid }}’  
–skeleton=/etc/skel  
–setattr userpassword=‘{crypt}{{ item.value.passwordhash}}’  
–title=student”

with\_dict: “{{students}}”  
register: added\_students

`

And the error:

`TASK [Add existing student users to ipaserver] ********************************* fatal: [216.125.253.133]: FAILED! => {"failed": true, "msg": "with_dict expects a dict"}`  
Running it verbose only gives me the task path in addition to the above error.

Is there a maximum size a dict can be? Is there something else wrong with my dict? I have tried it as a list (with a - before each username and with\_items in the play), but got a unicode error. I’m stumped.

Thanks for taking a look!

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 23, 2016, 12:04am UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/2 "2016-06-23T00:04:03Z")

</div>

I always find with\_dict fiddly, it'll work eventually. I can't see  
anything obvious at first glance  
(  
I do think

&nbsp;&nbsp;&nbsp;--email={{ item.value.name }}@domain

should be

&nbsp;&nbsp;&nbsp;--email="{{ item.key }}@domain"

but I don't think Ansible is getting as far as even opening the dict yet  
)

When I get this kind of issue, I cut down the size of the dict to a  
little sample set, just to verify the YAML  
is formatted correctly (no whitespace gotchas?).

Also, are you sure the dict is in scope at this point?

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 23, 2016, 12:51am UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/3 "2016-06-23T00:51:18Z")

</div>

Hi Dick,

Thanks for replying! Good catch on the email address. I’ll tweak that.  
I tried even just a debug of printing each item and that had the same with\_dict error.

It’s a good idea to try a truncated dict; then at least I’ll have a better idea whether it’s the length breaking it. That’s my next step.

Thanks!  
Joanna

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 23, 2016, 1:17am UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/4 "2016-06-23T01:17:03Z")

</div>

Good question about whether the dict is in scope.  
I import the vars this way at the top of the playbook:

`

vars\_files:

- vars/studentdict.yml
- vars/staffdict.yml

`

I run the playbook with become: yes. I verified that root and the local ansible user can read the dict.

I wonder if dashes in my date fields are breaking it. I am testing quoting those entries right now.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 23, 2016, 1:19am UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/5 "2016-06-23T01:19:20Z")

</div>

I don’t have an exact answer as to why currently, but the problem is with your `dateexpires` values. That value gets converted to a python datetime object. And because of it being automatically parsed as a datetime object, the dict is transformed into a string representation of the dict. The conversion to a string representation is the part that I don’t fully understand right now. It seems to be something inside of jinja2 itself that is causing this behavior.

So effectively you end up with a list that has 1 element, and that 1 element is a string representation of your dict.

Simply quoting the value of `dateexpires` resolves the issue, such as:

username1:  
lastname: jibberish  
firstname: jibberish  
firstinitial: jibberish  
studentid: jibberish  
dob: jibberish  
dateexpires: “2018-06-19”  
epochexpiry: 1529415778  
uid: jibberish  
passwordhash: “jibberish”

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 23, 2016, 1:24am UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/6 "2016-06-23T01:24:29Z")

</div>

Yup, a test with a short dict of only three records failed. Until I quoted all the date fields containing dashes.  
Each user record contains two dates that formatted like YYYY-MM-DD, which apparently breaks yaml. It makes sense now, but hadn’t occurred to me until I was driving home a while ago.

I was using this:  
`dob: 1962-01-21 dateexpires: 2018-06-19 epochexpiry: 1529415778`

Dashes break hashes, so it fixed it to quote the values:

`dob: '1962-01-21' dateexpires: '2018-06-19' epochexpiry: 1529415778`

I have yet to tweak my 40,000 lines (about 4000 records) and try with those.

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 23, 2016, 2:24am UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-can-a-dict-be-too-large/21838/7 "2016-06-23T02:24:45Z")

</div>

It turns out I had various dashes and apostrophes (isolated single quotes spell trouble, too) smattered throughout the user’s first and last names. So, after quoting dates, password hashes, and name fields, I tried with the full large dictionary. Now my play works! Even more exciting, I have new users appearing on my IPA server with appropriate data. Thanks Dick and Matt!

If you’re ever in Champaign, IL USA, look me up. I’ll buy you a coffee.

Joanna
