# Using a list with lineinfile throws an undefined variable error

**URL:** <https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818>\
**Category:** Ansible Project\
**Created:** [March 21, 2014, 12:26am UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818 "2014-03-21T00:26:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Munoz](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Simon\_Munoz](https://forum.ansible.com/u/Simon_Munoz)\
**Post date:** [March 21, 2014, 12:26am UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/1 "2014-03-21T00:26:39Z")

</div>

Hi all,

I have tried all that I could think about this, but I wasn’t able to use a list inside a lineinfile call. This is my task code:

`

- name: php.ini hardening  
lineinfile:  
dest=/etc/php5/fpm/php.ini  
regexp=“{{ item.regexp }}”  
line=“{{ item.line }}”  
backrefs=yes  
with\_items:
- php\_ini\_settings  
`

And this is an example list:

`

# roles/php-fpm/vars/main.yml

---

<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:** [March 21, 2014, 12:15pm UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/2 "2014-03-21T12:15:07Z")

</div>

There are a couple of things going on here, but the end solution I believe is going to be:

with\_items: php\_ini\_settings

What you are doing is creating a list of lists, and php\_ini\_settings is being treated as a string. Since php\_ini\_settings is already a list, just use the line I referenced above.

---

<div class="post-metadata">

**Author:** ![Simon\_Munoz](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Simon\_Munoz](https://forum.ansible.com/u/Simon_Munoz)\
**Post date:** [March 21, 2014, 12:20pm UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/3 "2014-03-21T12:20:22Z")

</div>

Thanks for your answer Matt, but unfortunately it hasn’t worked :(.

`

- name: Setting log level in development  
lineinfile:  
dest=/etc/php5/fpm/php.ini  
regexp=“{{ item.regexp }}”  
line=“{{ item.line }}”  
backrefs=yes  
with\_items: php\_ini\_settings  
`

`  
fatal: [192.168.33.9] =\> One or more undefined variables: ‘str object’ has no attribute ‘regexp’

`

---

<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:** [March 21, 2014, 12:24pm UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/4 "2014-03-21T12:24:37Z")

</div>

From the sound of it, either that variable doesn’t exist or for some reason it isn’t being expanded.

You could try adding a task above that one to inspect it:

- debug: var=php\_ini\_settings

You might also try:

with\_items: “{{ php\_ini\_settings }}”

But you really shouldn’t need to. The error you are getting is saying that the data it is looping over is a string, and not a dict/hash.

---

<div class="post-metadata">

**Author:** ![Simon\_Munoz](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Simon\_Munoz](https://forum.ansible.com/u/Simon_Munoz)\
**Post date:** [March 21, 2014, 12:31pm UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/5 "2014-03-21T12:31:40Z")

</div>

Thanks a lot Matt, the debug command was enough to find the problem. After including it in the playbook I found this:

`File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 2: ordinal not in range(128)`

So, it seems that my dictionary had an invisible character there making the variable unusable. Thanks a lot Matt, you made my day ;)!!!

---

<div class="post-metadata">

**Author:** ![Fred\_Badel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Fred\_Badel](https://forum.ansible.com/u/Fred_Badel)\
**Post date:** [March 21, 2014, 2:09pm UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/6 "2014-03-21T14:09:38Z")

</div>

Hi,

… not really answering to the initial question, but why wouldn’t you use the ini\_file module?

Fred

---

<div class="post-metadata">

**Author:** ![Simon\_Munoz](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Simon\_Munoz](https://forum.ansible.com/u/Simon_Munoz)\
**Post date:** [March 22, 2014, 10:54am UTC](https://forum.ansible.com/t/using-a-list-with-lineinfile-throws-an-undefined-variable-error/14818/7 "2014-03-22T10:54:04Z")

</div>

I simply didn´t know about it, but I think I will use it for sure ;).
