Using a list with lineinfile throws an undefined variable error

390 views
Skip to first unread message

Simón Muñoz

unread,
Mar 20, 2014, 8:26:39 PM3/20/14
to ansible...@googlegroups.com
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
---
php_ini_settings:
 
- regexp: "memory_limit = 128M"
    line
: "{{ memory_limit }}"
 
- regex: "post_max_size = 8M"
    line
: "{{ post_max_size }}"

The fatal error: 

fatal: [192.168.33.9] => One or more undefined variables: 'str object' has no attribute 'regexp'


Anyone has a clue about this? Thanks in advance!


Matt Martz

unread,
Mar 21, 2014, 8:15:07 AM3/21/14
to ansible...@googlegroups.com, Simón Muñoz
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.

-- 
Matt Martz
ma...@sivel.net
--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/59e76ce9-a1d8-4484-b753-9143d35db46f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simón Muñoz

unread,
Mar 21, 2014, 8:20:22 AM3/21/14
to ansible...@googlegroups.com, Simón Muñoz
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'



Matt Martz

unread,
Mar 21, 2014, 8:24:37 AM3/21/14
to ansible...@googlegroups.com, Simón Muñoz, Simón Muñoz
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.
-- 
Matt Martz
ma...@sivel.net

Simón Muñoz

unread,
Mar 21, 2014, 8:31:40 AM3/21/14
to ansible...@googlegroups.com, Simón Muñoz
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 ;)!!!

Frédéric Badel

unread,
Mar 21, 2014, 10:09:38 AM3/21/14
to ansible...@googlegroups.com
Hi,

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

Fred



Simón Muñoz

unread,
Mar 22, 2014, 6:54:04 AM3/22/14
to ansible...@googlegroups.com
I simply didn´t know about it, but I think I will use it for sure ;).
Reply all
Reply to author
Forward
0 new messages